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.core.runtime.Preferences;
33 import org.eclipse.jface.preference.IPreferenceStore;
34 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
35 import org.eclipse.jface.text.IAutoIndentStrategy;
36 import org.eclipse.jface.text.IDocument;
37 import org.eclipse.jface.text.ITextDoubleClickStrategy;
38 import org.eclipse.jface.text.ITextHover;
39 import org.eclipse.jface.text.TextAttribute;
40 import org.eclipse.jface.text.contentassist.ContentAssistant;
41 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
42 import org.eclipse.jface.text.contentassist.IContentAssistant;
43 import org.eclipse.jface.text.formatter.ContentFormatter;
44 import org.eclipse.jface.text.formatter.IContentFormatter;
45 import org.eclipse.jface.text.formatter.IFormattingStrategy;
46 import org.eclipse.jface.text.presentation.IPresentationReconciler;
47 import org.eclipse.jface.text.presentation.PresentationReconciler;
48 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
49 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
50 import org.eclipse.jface.text.rules.DefaultPartitioner;
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 * Method declared on SourceViewerConfiguration
184 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
185 return new String[] {
186 // IDocument.DEFAULT_CONTENT_TYPE,
187 IPHPPartitionScannerConstants.HTML,
188 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
189 IPHPPartitionScannerConstants.PHP,
190 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
191 IPHPPartitionScannerConstants.CSS,
192 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
193 IPHPPartitionScannerConstants.JAVASCRIPT,
194 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT };
198 * Method declared on SourceViewerConfiguration
200 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
202 ContentAssistant assistant = new ContentAssistant();
203 IContentAssistProcessor processor = new HTMLCompletionProcessor();
204 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML);
205 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
206 assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
207 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS);
208 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
209 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JAVASCRIPT);
210 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
212 assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
214 assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
216 // assistant.enableAutoActivation(true);
217 // assistant.setAutoActivationDelay(500);
218 // assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
219 // ContentAssistPreference.configure(assistant, getPreferenceStore());
220 // assistant.setContextInformationPopupOrientation(
221 // ContentAssistant.CONTEXT_INFO_ABOVE);
222 // assistant.setContextInformationPopupBackground(
223 // PHPEditorEnvironment.getPHPColorProvider().getColor(
224 // new RGB(150, 150, 0)));
225 ContentAssistPreference.configure(assistant, getPreferenceStore());
227 assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
228 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
234 * Method declared on SourceViewerConfiguration
236 // public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
237 // return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
238 // // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
242 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
245 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
246 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
250 * Method declared on SourceViewerConfiguration
252 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
253 return new PHPDoubleClickSelector();
257 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
259 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
261 Vector vector = new Vector();
263 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
265 final IPreferenceStore preferences= PHPeclipsePlugin.getDefault().getPreferenceStore();
266 int tabWidth= preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
267 boolean useSpaces= getPreferenceStore().getBoolean(SPACES_FOR_TABS);
269 for (int i = 0; i <= tabWidth; i++) {
270 StringBuffer prefix = new StringBuffer();
273 for (int j = 0; j + i < tabWidth; j++)
279 for (int j = 0; j < i; j++)
286 vector.add(prefix.toString());
289 vector.add(""); //$NON-NLS-1$
291 return (String[]) vector.toArray(new String[vector.size()]);
294 * Method declared on SourceViewerConfiguration
296 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
297 // PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
298 JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
299 PresentationReconciler reconciler = new PresentationReconciler();
301 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
302 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
303 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
305 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
306 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
307 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
308 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
309 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
310 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
311 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
312 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
313 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
314 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
315 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
316 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
317 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
318 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
319 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
321 new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
322 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
323 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
325 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
326 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
327 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
329 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPDocCodeScanner());
330 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
331 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
337 * Method declared on SourceViewerConfiguration
339 public int getTabWidth(ISourceViewer sourceViewer) {
340 return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
344 * Method declared on SourceViewerConfiguration
346 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
347 return new PHPTextHover();