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.java.JavaFormattingStrategy;
18 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCompletionProcessor;
19 import net.sourceforge.phpdt.ui.PreferenceConstants;
20 import net.sourceforge.phpdt.ui.text.JavaTextTools;
21 import net.sourceforge.phpeclipse.PHPCore;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import net.sourceforge.phpeclipse.phpeditor.html.HTMLFormattingStrategy;
24 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
25 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
26 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
27 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
28 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
29 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
31 import org.eclipse.core.resources.IFile;
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;
55 import org.eclipse.ui.IFileEditorInput;
58 * Configuration for an <code>SourceViewer</code> which shows PHP code.
60 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;
75 // public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
76 //IDocument.DEFAULT_CONTENT_TYPE;
78 private JavaTextTools fJavaTextTools;
79 private PHPEditor fEditor;
81 private ContentFormatter fFormatter;
82 private HTMLFormattingStrategy fFormattingStrategy;
84 * Single token scanner.
86 static class SingleTokenScanner extends BufferedRuleBasedScanner {
87 public SingleTokenScanner(TextAttribute attribute) {
88 setDefaultReturnToken(new Token(attribute));
93 * Default constructor.
95 public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
96 fJavaTextTools = textTools;
101 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
103 public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
104 // if (fFormatter == null) {
105 // fFormatter = new ContentFormatter();
106 // fFormattingStrategy = new HTMLFormattingStrategy(this, sourceViewer);
107 // fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
108 // fFormatter.enablePartitionAwareFormatting(false);
109 // fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
111 // return fFormatter;
113 if (fFormatter == null) {
115 fFormatter = new ContentFormatter();
116 IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
118 fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
119 fFormatter.enablePartitionAwareFormatting(false);
120 fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
126 * Returns the names of the document position categories used by the document
127 * partitioners created by this object to manage their partition information.
128 * If the partitioners don't use document position categories, the returned
129 * result is <code>null</code>.
131 * @return the partition managing position categories or <code>null</code>
134 public String[] getPartitionManagingPositionCategories() {
135 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
138 // * Returns the names of the document position categories used by the document
139 // * partitioners created by this object to manage their partition information.
140 // * If the partitioners don't use document position categories, the returned
141 // * result is <code>null</code>.
143 // * @return the partition managing position categories or <code>null</code>
144 // * if there is none
146 // private String[] getPartitionManagingPositionCategories() {
147 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
150 public PHPEditor getEditor() {
155 * Returns the preference store used by this configuration to initialize
156 * the individual bits and pieces.
158 * @return the preference store used to initialize this configuration
162 protected IPreferenceStore getPreferenceStore() {
163 return PHPeclipsePlugin.getDefault().getPreferenceStore();
167 * Method declared on SourceViewerConfiguration
169 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
170 return new PHPAnnotationHover();
174 * Method declared on SourceViewerConfiguration
176 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
177 return (IPHPPartitionScannerConstants.PHP.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
181 * Returns the PHP source code scanner for this configuration.
183 * @return the PHP source code scanner
185 protected RuleBasedScanner getCodeScanner() {
186 return fJavaTextTools.getCodeScanner();
190 * Returns the HTML source code scanner for this configuration.
192 * @return the HTML source code scanner
194 protected RuleBasedScanner getHTMLScanner() {
195 return fJavaTextTools.getHTMLScanner();
199 * Returns the PHPDoc source code scanner for this configuration.
201 * @return the PHPDoc source code scanner
203 protected RuleBasedScanner getPHPDocScanner() {
204 return fJavaTextTools.getJavaDocScanner();
208 * Method declared on SourceViewerConfiguration
210 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
211 return new String[] {
212 IPHPPartitionScannerConstants.HTML,
213 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
214 IPHPPartitionScannerConstants.PHP,
215 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
216 IPHPPartitionScannerConstants.CSS,
217 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
218 IPHPPartitionScannerConstants.JAVASCRIPT,
219 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT,
220 IDocument.DEFAULT_CONTENT_TYPE };
224 * Method declared on SourceViewerConfiguration
226 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
228 ContentAssistant assistant = new ContentAssistant();
229 IContentAssistProcessor processor = new HTMLCompletionProcessor();
230 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML);
231 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
232 assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
233 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS);
234 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
235 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JAVASCRIPT);
236 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
238 assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
240 assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
242 // assistant.enableAutoActivation(true);
243 // assistant.setAutoActivationDelay(500);
244 // assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
245 // ContentAssistPreference.configure(assistant, getPreferenceStore());
246 // assistant.setContextInformationPopupOrientation(
247 // ContentAssistant.CONTEXT_INFO_ABOVE);
248 // assistant.setContextInformationPopupBackground(
249 // PHPEditorEnvironment.getPHPColorProvider().getColor(
250 // new RGB(150, 150, 0)));
251 ContentAssistPreference.configure(assistant, getPreferenceStore());
253 assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
254 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
260 * Method declared on SourceViewerConfiguration
262 // public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
263 // return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
264 // // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
268 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
271 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
272 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
276 * Method declared on SourceViewerConfiguration
278 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
279 return new PHPDoubleClickSelector();
283 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
285 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
287 Vector vector = new Vector();
289 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
291 final IPreferenceStore preferences = PHPeclipsePlugin.getDefault().getPreferenceStore();
292 int tabWidth = preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
293 boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
295 for (int i = 0; i <= tabWidth; i++) {
296 StringBuffer prefix = new StringBuffer();
299 for (int j = 0; j + i < tabWidth; j++)
305 for (int j = 0; j < i; j++)
312 vector.add(prefix.toString());
315 vector.add(""); //$NON-NLS-1$
317 return (String[]) vector.toArray(new String[vector.size()]);
320 * Method declared on SourceViewerConfiguration
322 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
323 // PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
324 // JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
325 PresentationReconciler reconciler = new PresentationReconciler();
327 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
328 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
329 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
331 dr = new DefaultDamagerRepairer(getHTMLScanner());
332 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
333 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
334 dr = new DefaultDamagerRepairer(getHTMLScanner());
335 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
336 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
337 dr = new DefaultDamagerRepairer(getHTMLScanner());
338 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
339 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
340 dr = new DefaultDamagerRepairer(getHTMLScanner());
341 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
342 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
343 dr = new DefaultDamagerRepairer(getHTMLScanner());
344 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
345 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
347 new DefaultDamagerRepairer(
348 new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().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) {
375 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
376 return new PHPTextHover(f.getProject());
377 } catch (NullPointerException e) {
378 // this exception occurs, if getTextHover is called by preference pages !
380 return new PHPTextHover(null);