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.HTMLTextPresenter;
18 import net.sourceforge.phpdt.internal.ui.text.PHPAnnotationHover;
19 import net.sourceforge.phpdt.internal.ui.text.java.JavaFormattingStrategy;
20 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCompletionProcessor;
21 import net.sourceforge.phpdt.ui.PreferenceConstants;
22 import net.sourceforge.phpdt.ui.text.JavaTextTools;
23 import net.sourceforge.phpeclipse.PHPCore;
24 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
25 import net.sourceforge.phpeclipse.phpeditor.html.HTMLFormattingStrategy;
26 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
27 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
28 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
29 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
30 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
31 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
33 import org.eclipse.core.resources.IFile;
34 import org.eclipse.jface.preference.IPreferenceStore;
35 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
36 import org.eclipse.jface.text.DefaultInformationControl;
37 import org.eclipse.jface.text.IAutoIndentStrategy;
38 import org.eclipse.jface.text.IDocument;
39 import org.eclipse.jface.text.IInformationControl;
40 import org.eclipse.jface.text.IInformationControlCreator;
41 import org.eclipse.jface.text.ITextDoubleClickStrategy;
42 import org.eclipse.jface.text.ITextHover;
43 import org.eclipse.jface.text.TextAttribute;
44 import org.eclipse.jface.text.contentassist.ContentAssistant;
45 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
46 import org.eclipse.jface.text.contentassist.IContentAssistant;
47 import org.eclipse.jface.text.formatter.ContentFormatter;
48 import org.eclipse.jface.text.formatter.IContentFormatter;
49 import org.eclipse.jface.text.formatter.IFormattingStrategy;
50 import org.eclipse.jface.text.presentation.IPresentationReconciler;
51 import org.eclipse.jface.text.presentation.PresentationReconciler;
52 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
53 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
54 import org.eclipse.jface.text.rules.DefaultPartitioner;
55 import org.eclipse.jface.text.rules.RuleBasedScanner;
56 import org.eclipse.jface.text.rules.Token;
57 import org.eclipse.jface.text.source.IAnnotationHover;
58 import org.eclipse.jface.text.source.ISourceViewer;
59 import org.eclipse.jface.text.source.SourceViewerConfiguration;
60 import org.eclipse.swt.SWT;
61 import org.eclipse.swt.widgets.Shell;
62 import org.eclipse.ui.IEditorInput;
63 import org.eclipse.ui.IFileEditorInput;
66 * Configuration for an <code>SourceViewer</code> which shows PHP code.
68 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
71 * Preference key used to look up display tab width.
75 public final static String PREFERENCE_TAB_WIDTH = PreferenceConstants.EDITOR_TAB_WIDTH;
77 * Preference key for inserting spaces rather than tabs.
81 public final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
83 // public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
84 //IDocument.DEFAULT_CONTENT_TYPE;
86 private JavaTextTools fJavaTextTools;
87 private PHPEditor fEditor;
89 private ContentFormatter fFormatter;
90 private HTMLFormattingStrategy fFormattingStrategy;
92 * Single token scanner.
94 static class SingleTokenScanner extends BufferedRuleBasedScanner {
95 public SingleTokenScanner(TextAttribute attribute) {
96 setDefaultReturnToken(new Token(attribute));
101 * Default constructor.
103 public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
104 fJavaTextTools = textTools;
109 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
111 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) {
123 fFormatter = new ContentFormatter();
124 IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
126 fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
127 fFormatter.enablePartitionAwareFormatting(false);
128 fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
134 * Returns the names of the document position categories used by the document
135 * partitioners created by this object to manage their partition information.
136 * If the partitioners don't use document position categories, the returned
137 * result is <code>null</code>.
139 * @return the partition managing position categories or <code>null</code>
142 public String[] getPartitionManagingPositionCategories() {
143 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
146 // * Returns the names of the document position categories used by the document
147 // * partitioners created by this object to manage their partition information.
148 // * If the partitioners don't use document position categories, the returned
149 // * result is <code>null</code>.
151 // * @return the partition managing position categories or <code>null</code>
152 // * if there is none
154 // private String[] getPartitionManagingPositionCategories() {
155 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
158 public PHPEditor getEditor() {
163 * Returns the preference store used by this configuration to initialize
164 * the individual bits and pieces.
166 * @return the preference store used to initialize this configuration
170 protected IPreferenceStore getPreferenceStore() {
171 return PHPeclipsePlugin.getDefault().getPreferenceStore();
175 // * Method declared on SourceViewerConfiguration
177 // public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
178 // return new PHPAnnotationHover();
181 * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
183 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
184 return new PHPAnnotationHover();
188 * Method declared on SourceViewerConfiguration
190 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
191 return (IPHPPartitionScannerConstants.PHP.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
195 * Returns the PHP source code scanner for this configuration.
197 * @return the PHP source code scanner
199 protected RuleBasedScanner getCodeScanner() {
200 return fJavaTextTools.getCodeScanner();
204 * Returns the HTML source code scanner for this configuration.
206 * @return the HTML source code scanner
208 protected RuleBasedScanner getHTMLScanner() {
209 return fJavaTextTools.getHTMLScanner();
213 * Returns the Smarty source code scanner for this configuration.
215 * @return the Smarty source code scanner
217 protected RuleBasedScanner getSmartyScanner() {
218 return fJavaTextTools.getSmartyScanner();
222 * Returns the SmartyDoc source code scanner for this configuration.
224 * @return the SmartyDoc source code scanner
226 protected RuleBasedScanner getSmartyDocScanner() {
227 return fJavaTextTools.getSmartyDocScanner();
232 * Returns the PHPDoc source code scanner for this configuration.
234 * @return the PHPDoc source code scanner
236 protected RuleBasedScanner getPHPDocScanner() {
237 return fJavaTextTools.getJavaDocScanner();
241 * Method declared on SourceViewerConfiguration
243 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
244 return new String[] {
245 IPHPPartitionScannerConstants.HTML,
246 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
247 IPHPPartitionScannerConstants.PHP,
248 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
249 IPHPPartitionScannerConstants.CSS,
250 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
251 IPHPPartitionScannerConstants.JAVASCRIPT,
252 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT,
253 IPHPPartitionScannerConstants.SMARTY,
254 IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT,
255 IDocument.DEFAULT_CONTENT_TYPE };
259 * Method declared on SourceViewerConfiguration
261 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
263 ContentAssistant assistant = new ContentAssistant();
264 IContentAssistProcessor processor = new HTMLCompletionProcessor();
265 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML);
266 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
267 assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
268 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS);
269 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
270 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JAVASCRIPT);
271 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
272 // TODO define special smarty partition content assist
273 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.SMARTY);
274 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT);
276 assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
278 assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
280 // assistant.enableAutoActivation(true);
281 // assistant.setAutoActivationDelay(500);
282 // assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
283 // ContentAssistPreference.configure(assistant, getPreferenceStore());
284 // assistant.setContextInformationPopupOrientation(
285 // ContentAssistant.CONTEXT_INFO_ABOVE);
286 // assistant.setContextInformationPopupBackground(
287 // PHPEditorEnvironment.getPHPColorProvider().getColor(
288 // new RGB(150, 150, 0)));
289 ContentAssistPreference.configure(assistant, getPreferenceStore());
291 assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
292 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
298 * Method declared on SourceViewerConfiguration
300 // public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
301 // return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
302 // // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
306 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
309 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
310 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
314 * Method declared on SourceViewerConfiguration
316 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
317 return new PHPDoubleClickSelector();
321 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
323 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
325 Vector vector = new Vector();
327 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
329 final IPreferenceStore preferences = PHPeclipsePlugin.getDefault().getPreferenceStore();
330 int tabWidth = preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
331 boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
333 for (int i = 0; i <= tabWidth; i++) {
334 StringBuffer prefix = new StringBuffer();
337 for (int j = 0; j + i < tabWidth; j++)
343 for (int j = 0; j < i; j++)
350 vector.add(prefix.toString());
353 vector.add(""); //$NON-NLS-1$
355 return (String[]) vector.toArray(new String[vector.size()]);
358 * Method declared on SourceViewerConfiguration
360 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
361 // PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
362 // JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
363 PresentationReconciler reconciler = new PresentationReconciler();
365 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
366 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
367 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
369 dr = new DefaultDamagerRepairer(getHTMLScanner());
370 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
371 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
372 dr = new DefaultDamagerRepairer(getHTMLScanner());
373 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
374 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
375 dr = new DefaultDamagerRepairer(getHTMLScanner());
376 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
377 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
378 dr = new DefaultDamagerRepairer(getHTMLScanner());
379 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
380 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
381 dr = new DefaultDamagerRepairer(getHTMLScanner());
382 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
383 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
385 dr = new DefaultDamagerRepairer(getSmartyScanner());
386 reconciler.setDamager(dr, IPHPPartitionScannerConstants.SMARTY);
387 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.SMARTY);
389 dr = new DefaultDamagerRepairer(getSmartyDocScanner());
390 reconciler.setDamager(dr, IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT);
391 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT);
394 new DefaultDamagerRepairer(
395 new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
396 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
397 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
399 dr = new DefaultDamagerRepairer(getCodeScanner());
400 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
401 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
403 dr = new DefaultDamagerRepairer(getPHPDocScanner());
404 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
405 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
411 * Method declared on SourceViewerConfiguration
413 public int getTabWidth(ISourceViewer sourceViewer) {
414 return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
418 * Method declared on SourceViewerConfiguration
420 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
421 IEditorInput editorInput = fEditor.getEditorInput();
422 if (editorInput instanceof IFileEditorInput) {
424 IFile f = ((IFileEditorInput) editorInput).getFile();
425 return new PHPTextHover(f.getProject());
426 } catch (NullPointerException e) {
427 // this exception occurs, if getTextHover is called by preference pages !
430 return new PHPTextHover(null);
434 * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
437 public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
438 return new IInformationControlCreator() {
439 public IInformationControl createInformationControl(Shell parent) {
440 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
441 // return new HoverBrowserControl(parent);
447 * @see SourceViewerConfiguration#getInformationPresenter(ISourceViewer)
450 // public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) {
451 // InformationPresenter presenter= new InformationPresenter(getInformationPresenterControlCreator(sourceViewer));
452 // IInformationProvider provider= new JavaInformationProvider(getEditor());
453 // presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE);
454 // presenter.setInformationProvider(provider, IJavaPartitions.JAVA_DOC);
455 // presenter.setSizeConstraints(60, 10, true, true);
460 * Returns the information presenter control creator. The creator is a factory creating the
461 * presenter controls for the given source viewer. This implementation always returns a creator
462 * for <code>DefaultInformationControl</code> instances.
464 * @param sourceViewer the source viewer to be configured by this configuration
465 * @return an information control creator
468 private IInformationControlCreator getInformationPresenterControlCreator(ISourceViewer sourceViewer) {
469 return new IInformationControlCreator() {
470 public IInformationControl createInformationControl(Shell parent) {
471 int shellStyle = SWT.RESIZE;
472 int style = SWT.V_SCROLL | SWT.H_SCROLL;
473 return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false));
474 // return new HoverBrowserControl(parent);