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= PreferenceConstants.EDITOR_TAB_WIDTH;
69 * Preference key for inserting spaces rather than tabs.
73 public final static String SPACES_FOR_TABS= PreferenceConstants.EDITOR_SPACES_FOR_TABS;
76 // public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
77 //IDocument.DEFAULT_CONTENT_TYPE;
79 private JavaTextTools fJavaTextTools;
80 private PHPEditor fEditor;
82 private ContentFormatter fFormatter;
83 private HTMLFormattingStrategy fFormattingStrategy;
85 * Single token scanner.
87 static class SingleTokenScanner extends BufferedRuleBasedScanner {
88 public SingleTokenScanner(TextAttribute attribute) {
89 setDefaultReturnToken(new Token(attribute));
94 * Default constructor.
96 public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
97 fJavaTextTools = textTools;
102 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
104 public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
105 // if (fFormatter == null) {
106 // fFormatter = new ContentFormatter();
107 // fFormattingStrategy = new HTMLFormattingStrategy(this, sourceViewer);
108 // fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
109 // fFormatter.enablePartitionAwareFormatting(false);
110 // fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
112 // return fFormatter;
114 if (fFormatter == null) {
116 fFormatter = new ContentFormatter();
117 IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
119 fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
120 fFormatter.enablePartitionAwareFormatting(false);
121 fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
127 * Returns the names of the document position categories used by the document
128 * partitioners created by this object to manage their partition information.
129 * If the partitioners don't use document position categories, the returned
130 * result is <code>null</code>.
132 * @return the partition managing position categories or <code>null</code>
135 public String[] getPartitionManagingPositionCategories() {
136 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
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>
145 // * if there is none
147 // private String[] getPartitionManagingPositionCategories() {
148 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
151 public PHPEditor getEditor() {
156 * Returns the preference store used by this configuration to initialize
157 * the individual bits and pieces.
159 * @return the preference store used to initialize this configuration
163 protected IPreferenceStore getPreferenceStore() {
164 return PHPeclipsePlugin.getDefault().getPreferenceStore();
168 * Method declared on SourceViewerConfiguration
170 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
171 return new PHPAnnotationHover();
175 * Method declared on SourceViewerConfiguration
177 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
178 return (IPHPPartitionScannerConstants.PHP.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
182 * Returns the PHP source code scanner for this configuration.
184 * @return the PHP source code scanner
186 protected RuleBasedScanner getCodeScanner() {
187 return fJavaTextTools.getCodeScanner();
191 * Returns the HTML source code scanner for this configuration.
193 * @return the HTML source code scanner
195 protected RuleBasedScanner getHTMLScanner() {
196 return fJavaTextTools.getHTMLScanner();
200 * Returns the PHPDoc source code scanner for this configuration.
202 * @return the PHPDoc source code scanner
204 protected RuleBasedScanner getPHPDocScanner() {
205 return fJavaTextTools.getJavaDocScanner();
209 * Method declared on SourceViewerConfiguration
211 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
212 return new String[] {
213 IPHPPartitionScannerConstants.HTML,
214 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
215 IPHPPartitionScannerConstants.PHP,
216 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
217 IPHPPartitionScannerConstants.CSS,
218 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
219 IPHPPartitionScannerConstants.JAVASCRIPT,
220 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT,
221 IDocument.DEFAULT_CONTENT_TYPE };
225 * Method declared on SourceViewerConfiguration
227 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
229 ContentAssistant assistant = new ContentAssistant();
230 IContentAssistProcessor processor = new HTMLCompletionProcessor();
231 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML);
232 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
233 assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
234 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS);
235 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
236 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JAVASCRIPT);
237 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
239 assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
241 assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
243 // assistant.enableAutoActivation(true);
244 // assistant.setAutoActivationDelay(500);
245 // assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
246 // ContentAssistPreference.configure(assistant, getPreferenceStore());
247 // assistant.setContextInformationPopupOrientation(
248 // ContentAssistant.CONTEXT_INFO_ABOVE);
249 // assistant.setContextInformationPopupBackground(
250 // PHPEditorEnvironment.getPHPColorProvider().getColor(
251 // new RGB(150, 150, 0)));
252 ContentAssistPreference.configure(assistant, getPreferenceStore());
254 assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
255 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
261 * Method declared on SourceViewerConfiguration
263 // public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
264 // return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
265 // // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
269 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
272 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
273 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
277 * Method declared on SourceViewerConfiguration
279 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
280 return new PHPDoubleClickSelector();
284 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
286 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
288 Vector vector = new Vector();
290 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
292 final IPreferenceStore preferences= PHPeclipsePlugin.getDefault().getPreferenceStore();
293 int tabWidth= preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
294 boolean useSpaces= getPreferenceStore().getBoolean(SPACES_FOR_TABS);
296 for (int i = 0; i <= tabWidth; i++) {
297 StringBuffer prefix = new StringBuffer();
300 for (int j = 0; j + i < tabWidth; j++)
306 for (int j = 0; j < i; j++)
313 vector.add(prefix.toString());
316 vector.add(""); //$NON-NLS-1$
318 return (String[]) vector.toArray(new String[vector.size()]);
321 * Method declared on SourceViewerConfiguration
323 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
324 // PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
325 JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
326 PresentationReconciler reconciler = new PresentationReconciler();
328 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
329 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
330 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
332 dr = new DefaultDamagerRepairer(getHTMLScanner());
333 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
334 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
335 dr = new DefaultDamagerRepairer(getHTMLScanner());
336 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
337 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
338 dr = new DefaultDamagerRepairer(getHTMLScanner());
339 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
340 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
341 dr = new DefaultDamagerRepairer(getHTMLScanner());
342 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
343 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
344 dr = new DefaultDamagerRepairer(getHTMLScanner());
345 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
346 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
348 new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
349 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
350 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
352 dr = new DefaultDamagerRepairer(getCodeScanner());
353 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
354 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
356 dr = new DefaultDamagerRepairer(getPHPDocScanner());
357 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
358 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
364 * Method declared on SourceViewerConfiguration
366 public int getTabWidth(ISourceViewer sourceViewer) {
367 return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
371 * Method declared on SourceViewerConfiguration
373 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
374 return new PHPTextHover();