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.IFileEditorInput;
65 * Configuration for an <code>SourceViewer</code> which shows PHP code.
67 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
70 * Preference key used to look up display tab width.
74 public final static String PREFERENCE_TAB_WIDTH = PreferenceConstants.EDITOR_TAB_WIDTH;
76 * Preference key for inserting spaces rather than tabs.
80 public final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
82 // public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
83 //IDocument.DEFAULT_CONTENT_TYPE;
85 private JavaTextTools fJavaTextTools;
86 private PHPEditor fEditor;
88 private ContentFormatter fFormatter;
89 private HTMLFormattingStrategy fFormattingStrategy;
91 * Single token scanner.
93 static class SingleTokenScanner extends BufferedRuleBasedScanner {
94 public SingleTokenScanner(TextAttribute attribute) {
95 setDefaultReturnToken(new Token(attribute));
100 * Default constructor.
102 public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
103 fJavaTextTools = textTools;
108 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
110 public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
111 // if (fFormatter == null) {
112 // fFormatter = new ContentFormatter();
113 // fFormattingStrategy = new HTMLFormattingStrategy(this, sourceViewer);
114 // fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
115 // fFormatter.enablePartitionAwareFormatting(false);
116 // fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
118 // return fFormatter;
120 if (fFormatter == null) {
122 fFormatter = new ContentFormatter();
123 IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
125 fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
126 fFormatter.enablePartitionAwareFormatting(false);
127 fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
133 * Returns the names of the document position categories used by the document
134 * partitioners created by this object to manage their partition information.
135 * If the partitioners don't use document position categories, the returned
136 * result is <code>null</code>.
138 * @return the partition managing position categories or <code>null</code>
141 public String[] getPartitionManagingPositionCategories() {
142 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
145 // * Returns the names of the document position categories used by the document
146 // * partitioners created by this object to manage their partition information.
147 // * If the partitioners don't use document position categories, the returned
148 // * result is <code>null</code>.
150 // * @return the partition managing position categories or <code>null</code>
151 // * if there is none
153 // private String[] getPartitionManagingPositionCategories() {
154 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
157 public PHPEditor getEditor() {
162 * Returns the preference store used by this configuration to initialize
163 * the individual bits and pieces.
165 * @return the preference store used to initialize this configuration
169 protected IPreferenceStore getPreferenceStore() {
170 return PHPeclipsePlugin.getDefault().getPreferenceStore();
174 // * Method declared on SourceViewerConfiguration
176 // public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
177 // return new PHPAnnotationHover();
180 * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
182 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
183 return new PHPAnnotationHover();
187 * Method declared on SourceViewerConfiguration
189 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
190 return (IPHPPartitionScannerConstants.PHP.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
194 * Returns the PHP source code scanner for this configuration.
196 * @return the PHP source code scanner
198 protected RuleBasedScanner getCodeScanner() {
199 return fJavaTextTools.getCodeScanner();
203 * Returns the HTML source code scanner for this configuration.
205 * @return the HTML source code scanner
207 protected RuleBasedScanner getHTMLScanner() {
208 return fJavaTextTools.getHTMLScanner();
212 * Returns the Smarty source code scanner for this configuration.
214 * @return the Smarty source code scanner
216 protected RuleBasedScanner getSmartyScanner() {
217 return fJavaTextTools.getSmartyScanner();
221 * Returns the SmartyDoc source code scanner for this configuration.
223 * @return the SmartyDoc source code scanner
225 protected RuleBasedScanner getSmartyDocScanner() {
226 return fJavaTextTools.getSmartyDocScanner();
231 * Returns the PHPDoc source code scanner for this configuration.
233 * @return the PHPDoc source code scanner
235 protected RuleBasedScanner getPHPDocScanner() {
236 return fJavaTextTools.getJavaDocScanner();
240 * Method declared on SourceViewerConfiguration
242 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
243 return new String[] {
244 IPHPPartitionScannerConstants.HTML,
245 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
246 IPHPPartitionScannerConstants.PHP,
247 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
248 IPHPPartitionScannerConstants.CSS,
249 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
250 IPHPPartitionScannerConstants.JAVASCRIPT,
251 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT,
252 IPHPPartitionScannerConstants.SMARTY,
253 IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT,
254 IDocument.DEFAULT_CONTENT_TYPE };
258 * Method declared on SourceViewerConfiguration
260 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
262 ContentAssistant assistant = new ContentAssistant();
263 IContentAssistProcessor processor = new HTMLCompletionProcessor();
264 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML);
265 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
266 assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
267 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS);
268 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
269 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JAVASCRIPT);
270 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
271 // TODO define special smarty partition content assist
272 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.SMARTY);
273 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT);
275 assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
277 assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
279 // assistant.enableAutoActivation(true);
280 // assistant.setAutoActivationDelay(500);
281 // assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
282 // ContentAssistPreference.configure(assistant, getPreferenceStore());
283 // assistant.setContextInformationPopupOrientation(
284 // ContentAssistant.CONTEXT_INFO_ABOVE);
285 // assistant.setContextInformationPopupBackground(
286 // PHPEditorEnvironment.getPHPColorProvider().getColor(
287 // new RGB(150, 150, 0)));
288 ContentAssistPreference.configure(assistant, getPreferenceStore());
290 assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
291 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
297 * Method declared on SourceViewerConfiguration
299 // public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
300 // return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
301 // // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
305 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
308 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
309 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
313 * Method declared on SourceViewerConfiguration
315 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
316 return new PHPDoubleClickSelector();
320 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
322 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
324 Vector vector = new Vector();
326 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
328 final IPreferenceStore preferences = PHPeclipsePlugin.getDefault().getPreferenceStore();
329 int tabWidth = preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
330 boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
332 for (int i = 0; i <= tabWidth; i++) {
333 StringBuffer prefix = new StringBuffer();
336 for (int j = 0; j + i < tabWidth; j++)
342 for (int j = 0; j < i; j++)
349 vector.add(prefix.toString());
352 vector.add(""); //$NON-NLS-1$
354 return (String[]) vector.toArray(new String[vector.size()]);
357 * Method declared on SourceViewerConfiguration
359 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
360 // PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
361 // JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
362 PresentationReconciler reconciler = new PresentationReconciler();
364 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
365 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
366 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
368 dr = new DefaultDamagerRepairer(getHTMLScanner());
369 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
370 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
371 dr = new DefaultDamagerRepairer(getHTMLScanner());
372 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
373 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
374 dr = new DefaultDamagerRepairer(getHTMLScanner());
375 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
376 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
377 dr = new DefaultDamagerRepairer(getHTMLScanner());
378 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
379 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
380 dr = new DefaultDamagerRepairer(getHTMLScanner());
381 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
382 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
384 dr = new DefaultDamagerRepairer(getSmartyScanner());
385 reconciler.setDamager(dr, IPHPPartitionScannerConstants.SMARTY);
386 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.SMARTY);
388 dr = new DefaultDamagerRepairer(getSmartyDocScanner());
389 reconciler.setDamager(dr, IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT);
390 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT);
393 new DefaultDamagerRepairer(
394 new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
395 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
396 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
398 dr = new DefaultDamagerRepairer(getCodeScanner());
399 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
400 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
402 dr = new DefaultDamagerRepairer(getPHPDocScanner());
403 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
404 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
410 * Method declared on SourceViewerConfiguration
412 public int getTabWidth(ISourceViewer sourceViewer) {
413 return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
417 * Method declared on SourceViewerConfiguration
419 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
421 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
422 return new PHPTextHover(f.getProject());
423 } catch (NullPointerException e) {
424 // this exception occurs, if getTextHover is called by preference pages !
426 return new PHPTextHover(null);
430 * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
433 public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
434 return new IInformationControlCreator() {
435 public IInformationControl createInformationControl(Shell parent) {
436 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
437 // return new HoverBrowserControl(parent);
443 * @see SourceViewerConfiguration#getInformationPresenter(ISourceViewer)
446 // public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) {
447 // InformationPresenter presenter= new InformationPresenter(getInformationPresenterControlCreator(sourceViewer));
448 // IInformationProvider provider= new JavaInformationProvider(getEditor());
449 // presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE);
450 // presenter.setInformationProvider(provider, IJavaPartitions.JAVA_DOC);
451 // presenter.setSizeConstraints(60, 10, true, true);
456 * Returns the information presenter control creator. The creator is a factory creating the
457 * presenter controls for the given source viewer. This implementation always returns a creator
458 * for <code>DefaultInformationControl</code> instances.
460 * @param sourceViewer the source viewer to be configured by this configuration
461 * @return an information control creator
464 private IInformationControlCreator getInformationPresenterControlCreator(ISourceViewer sourceViewer) {
465 return new IInformationControlCreator() {
466 public IInformationControl createInformationControl(Shell parent) {
467 int shellStyle = SWT.RESIZE;
468 int style = SWT.V_SCROLL | SWT.H_SCROLL;
469 return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false));
470 // return new HoverBrowserControl(parent);