1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
9 IBM Corporation - Initial implementation
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
14 import java.util.Vector;
16 import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference;
17 import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
18 import net.sourceforge.phpdt.internal.ui.text.java.JavaFormattingStrategy;
19 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCompletionProcessor;
20 import net.sourceforge.phpdt.ui.PreferenceConstants;
21 import net.sourceforge.phpdt.ui.text.JavaTextTools;
22 import net.sourceforge.phpeclipse.PHPCore;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import net.sourceforge.phpeclipse.phpeditor.html.HTMLFormattingStrategy;
25 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
26 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
27 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
28 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
29 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
30 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
32 import org.eclipse.jface.preference.IPreferenceStore;
33 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
34 import org.eclipse.jface.text.IAutoIndentStrategy;
35 import org.eclipse.jface.text.IDocument;
36 import org.eclipse.jface.text.ITextDoubleClickStrategy;
37 import org.eclipse.jface.text.ITextHover;
38 import org.eclipse.jface.text.TextAttribute;
39 import org.eclipse.jface.text.contentassist.ContentAssistant;
40 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
41 import org.eclipse.jface.text.contentassist.IContentAssistant;
42 import org.eclipse.jface.text.formatter.ContentFormatter;
43 import org.eclipse.jface.text.formatter.IContentFormatter;
44 import org.eclipse.jface.text.formatter.IFormattingStrategy;
45 import org.eclipse.jface.text.presentation.IPresentationReconciler;
46 import org.eclipse.jface.text.presentation.PresentationReconciler;
47 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
48 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
49 import org.eclipse.jface.text.rules.DefaultPartitioner;
50 import org.eclipse.jface.text.rules.RuleBasedScanner;
51 import org.eclipse.jface.text.rules.Token;
52 import org.eclipse.jface.text.source.IAnnotationHover;
53 import org.eclipse.jface.text.source.ISourceViewer;
54 import org.eclipse.jface.text.source.SourceViewerConfiguration;
57 * Configuration for an <code>SourceViewer</code> which shows PHP code.
59 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration
63 * Preference key used to look up display tab width.
67 public final static String PREFERENCE_TAB_WIDTH =
68 PreferenceConstants.EDITOR_TAB_WIDTH;
70 * Preference key for inserting spaces rather than tabs.
74 public final static String SPACES_FOR_TABS =
75 PreferenceConstants.EDITOR_SPACES_FOR_TABS;
77 // public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
78 //IDocument.DEFAULT_CONTENT_TYPE;
80 private JavaTextTools fJavaTextTools;
81 private PHPEditor fEditor;
83 private ContentFormatter fFormatter;
84 private HTMLFormattingStrategy fFormattingStrategy;
86 * Single token scanner.
88 static class SingleTokenScanner extends BufferedRuleBasedScanner
90 public SingleTokenScanner(TextAttribute attribute)
92 setDefaultReturnToken(new Token(attribute));
97 * Default constructor.
99 public PHPSourceViewerConfiguration(
100 JavaTextTools textTools,
103 fJavaTextTools = textTools;
108 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
110 public IContentFormatter getContentFormatter(ISourceViewer sourceViewer)
112 // if (fFormatter == null) {
113 // fFormatter = new ContentFormatter();
114 // fFormattingStrategy = new HTMLFormattingStrategy(this, sourceViewer);
115 // fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
116 // fFormatter.enablePartitionAwareFormatting(false);
117 // fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
119 // return fFormatter;
121 if (fFormatter == null)
124 fFormatter = new ContentFormatter();
125 IFormattingStrategy strategy =
126 new JavaFormattingStrategy(sourceViewer);
128 fFormatter.setFormattingStrategy(
130 IDocument.DEFAULT_CONTENT_TYPE);
131 fFormatter.enablePartitionAwareFormatting(false);
132 fFormatter.setPartitionManagingPositionCategories(
133 getPartitionManagingPositionCategories());
139 * Returns the names of the document position categories used by the document
140 * partitioners created by this object to manage their partition information.
141 * If the partitioners don't use document position categories, the returned
142 * result is <code>null</code>.
144 * @return the partition managing position categories or <code>null</code>
147 public String[] getPartitionManagingPositionCategories()
149 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
152 // * Returns the names of the document position categories used by the document
153 // * partitioners created by this object to manage their partition information.
154 // * If the partitioners don't use document position categories, the returned
155 // * result is <code>null</code>.
157 // * @return the partition managing position categories or <code>null</code>
158 // * if there is none
160 // private String[] getPartitionManagingPositionCategories() {
161 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
164 public PHPEditor getEditor()
170 * Returns the preference store used by this configuration to initialize
171 * the individual bits and pieces.
173 * @return the preference store used to initialize this configuration
177 protected IPreferenceStore getPreferenceStore()
179 return PHPeclipsePlugin.getDefault().getPreferenceStore();
183 * Method declared on SourceViewerConfiguration
185 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer)
187 return new PHPAnnotationHover();
191 * Method declared on SourceViewerConfiguration
193 public IAutoIndentStrategy getAutoIndentStrategy(
194 ISourceViewer sourceViewer,
198 IPHPPartitionScannerConstants.PHP.equals(contentType)
199 ? new PHPAutoIndentStrategy()
200 : new DefaultAutoIndentStrategy());
204 * Returns the PHP source code scanner for this configuration.
206 * @return the PHP source code scanner
208 protected RuleBasedScanner getCodeScanner()
210 return fJavaTextTools.getCodeScanner();
214 * Returns the HTML source code scanner for this configuration.
216 * @return the HTML source code scanner
218 protected RuleBasedScanner getHTMLScanner()
220 return fJavaTextTools.getHTMLScanner();
224 * Returns the PHPDoc source code scanner for this configuration.
226 * @return the PHPDoc source code scanner
228 protected RuleBasedScanner getPHPDocScanner()
230 return fJavaTextTools.getJavaDocScanner();
234 * Method declared on SourceViewerConfiguration
236 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer)
238 return new String[] {
239 IPHPPartitionScannerConstants.HTML,
240 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
241 IPHPPartitionScannerConstants.PHP,
242 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
243 IPHPPartitionScannerConstants.CSS,
244 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
245 IPHPPartitionScannerConstants.JAVASCRIPT,
246 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT,
247 IDocument.DEFAULT_CONTENT_TYPE };
251 * Method declared on SourceViewerConfiguration
253 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer)
256 ContentAssistant assistant = new ContentAssistant();
257 IContentAssistProcessor processor = new HTMLCompletionProcessor();
258 assistant.setContentAssistProcessor(
260 IPHPPartitionScannerConstants.HTML);
261 assistant.setContentAssistProcessor(
263 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
264 assistant.setContentAssistProcessor(
266 IDocument.DEFAULT_CONTENT_TYPE);
267 assistant.setContentAssistProcessor(
269 IPHPPartitionScannerConstants.CSS);
270 assistant.setContentAssistProcessor(
272 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
273 assistant.setContentAssistProcessor(
275 IPHPPartitionScannerConstants.JAVASCRIPT);
276 assistant.setContentAssistProcessor(
278 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
280 assistant.setContentAssistProcessor(
281 new PHPCompletionProcessor(),
282 IPHPPartitionScannerConstants.PHP);
284 assistant.setContentAssistProcessor(
285 new PHPDocCompletionProcessor(),
286 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
288 // assistant.enableAutoActivation(true);
289 // assistant.setAutoActivationDelay(500);
290 // assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
291 // ContentAssistPreference.configure(assistant, getPreferenceStore());
292 // assistant.setContextInformationPopupOrientation(
293 // ContentAssistant.CONTEXT_INFO_ABOVE);
294 // assistant.setContextInformationPopupBackground(
295 // PHPEditorEnvironment.getPHPColorProvider().getColor(
296 // new RGB(150, 150, 0)));
297 ContentAssistPreference.configure(assistant, getPreferenceStore());
299 assistant.setContextInformationPopupOrientation(
300 ContentAssistant.CONTEXT_INFO_ABOVE);
301 assistant.setInformationControlCreator(
302 getInformationControlCreator(sourceViewer));
308 * Method declared on SourceViewerConfiguration
310 // public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
311 // return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
312 // // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
316 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
319 public String[] getDefaultPrefixes(
320 ISourceViewer sourceViewer,
323 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
327 * Method declared on SourceViewerConfiguration
329 public ITextDoubleClickStrategy getDoubleClickStrategy(
330 ISourceViewer sourceViewer,
333 return new PHPDoubleClickSelector();
337 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
339 public String[] getIndentPrefixes(
340 ISourceViewer sourceViewer,
344 Vector vector = new Vector();
346 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
348 final IPreferenceStore preferences =
349 PHPeclipsePlugin.getDefault().getPreferenceStore();
350 int tabWidth = preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
351 boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
353 for (int i = 0; i <= tabWidth; i++)
355 StringBuffer prefix = new StringBuffer();
359 for (int j = 0; j + i < tabWidth; j++)
367 for (int j = 0; j < i; j++)
374 vector.add(prefix.toString());
377 vector.add(""); //$NON-NLS-1$
379 return (String[]) vector.toArray(new String[vector.size()]);
382 * Method declared on SourceViewerConfiguration
384 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer)
386 // PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
387 JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
388 PresentationReconciler reconciler = new PresentationReconciler();
390 DefaultDamagerRepairer dr =
391 new DefaultDamagerRepairer(getHTMLScanner());
392 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
393 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
395 dr = new DefaultDamagerRepairer(getHTMLScanner());
396 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
397 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
398 dr = new DefaultDamagerRepairer(getHTMLScanner());
399 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
400 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
401 dr = new DefaultDamagerRepairer(getHTMLScanner());
402 reconciler.setDamager(
404 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
405 reconciler.setRepairer(
407 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
408 dr = new DefaultDamagerRepairer(getHTMLScanner());
409 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
410 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
411 dr = new DefaultDamagerRepairer(getHTMLScanner());
412 reconciler.setDamager(
414 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
415 reconciler.setRepairer(
417 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
418 dr = new DefaultDamagerRepairer(
419 new SingleTokenScanner(
421 fJavaTextTools.getColorManager().getColor(
422 PHPColorProvider.MULTI_LINE_COMMENT))));
423 reconciler.setDamager(
425 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
426 reconciler.setRepairer(
428 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
430 dr = new DefaultDamagerRepairer(getCodeScanner());
431 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
432 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
434 dr = new DefaultDamagerRepairer(getPHPDocScanner());
435 reconciler.setDamager(
437 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
438 reconciler.setRepairer(
440 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
446 * Method declared on SourceViewerConfiguration
448 public int getTabWidth(ISourceViewer sourceViewer)
450 return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
454 * Method declared on SourceViewerConfiguration
456 public ITextHover getTextHover(
457 ISourceViewer sourceViewer,
460 return new PHPTextHover();