Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPSourceViewerConfiguration.java
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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
13
14 import java.util.Vector;
15
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;
32
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;
63
64 /**
65  * Configuration for an <code>SourceViewer</code> which shows PHP code. 
66  */
67 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
68
69   /** 
70    * Preference key used to look up display tab width.
71    * 
72    * @since 2.0
73    */
74   public final static String PREFERENCE_TAB_WIDTH = PreferenceConstants.EDITOR_TAB_WIDTH;
75   /** 
76    * Preference key for inserting spaces rather than tabs.
77    * 
78    * @since 2.0
79    */
80   public final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
81
82   //  public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
83   //IDocument.DEFAULT_CONTENT_TYPE;
84
85   private JavaTextTools fJavaTextTools;
86   private PHPEditor fEditor;
87
88   private ContentFormatter fFormatter;
89   private HTMLFormattingStrategy fFormattingStrategy;
90   /**
91    * Single token scanner.
92    */
93   static class SingleTokenScanner extends BufferedRuleBasedScanner {
94     public SingleTokenScanner(TextAttribute attribute) {
95       setDefaultReturnToken(new Token(attribute));
96     }
97   };
98
99   /**
100    * Default constructor.
101    */
102   public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
103     fJavaTextTools = textTools;
104     fEditor = editor;
105   }
106
107   /*
108    * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
109    */
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));
117     //    }
118     //    return fFormatter;
119
120     if (fFormatter == null) {
121       //ContentFormatter 
122       fFormatter = new ContentFormatter();
123       IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
124
125       fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
126       fFormatter.enablePartitionAwareFormatting(false);
127       fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
128     }
129     return fFormatter;
130   }
131
132   /**
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>.
137    *
138    * @return the partition managing position categories or <code>null</code> 
139    *                    if there is none
140    */
141   public String[] getPartitionManagingPositionCategories() {
142     return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
143   }
144   //  /** 
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>.
149   //   *
150   //   * @return the partition managing position categories or <code>null</code> 
151   //   *                        if there is none
152   //   */
153   //  private String[] getPartitionManagingPositionCategories() {
154   //    return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
155   //  } 
156
157   public PHPEditor getEditor() {
158     return fEditor;
159   }
160
161   /**
162    * Returns the preference store used by this configuration to initialize
163    * the individual bits and pieces.
164    * 
165    * @return the preference store used to initialize this configuration
166    * 
167    * @since 2.0
168    */
169   protected IPreferenceStore getPreferenceStore() {
170     return PHPeclipsePlugin.getDefault().getPreferenceStore();
171   }
172
173   //  /* (non-Javadoc)
174   //   * Method declared on SourceViewerConfiguration
175   //   */
176   //  public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
177   //    return new PHPAnnotationHover();
178   //  }
179   /*
180    * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
181    */
182   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
183     return new PHPAnnotationHover();
184   }
185
186   /* (non-Javadoc)
187    * Method declared on SourceViewerConfiguration
188    */
189   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
190     return (IPHPPartitionScannerConstants.PHP.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
191   }
192
193   /**
194    * Returns the PHP source code scanner for this configuration.
195    *
196    * @return the PHP source code scanner
197    */
198   protected RuleBasedScanner getCodeScanner() {
199     return fJavaTextTools.getCodeScanner();
200   }
201
202   /**
203    * Returns the HTML source code scanner for this configuration.
204    *
205    * @return the HTML source code scanner
206    */
207   protected RuleBasedScanner getHTMLScanner() {
208     return fJavaTextTools.getHTMLScanner();
209   }
210
211         /**
212          * Returns the Smarty source code scanner for this configuration.
213          *
214          * @return the Smarty source code scanner
215          */
216         protected RuleBasedScanner getSmartyScanner() {
217                 return fJavaTextTools.getSmartyScanner();
218         }
219         
220         /**
221          * Returns the SmartyDoc source code scanner for this configuration.
222          *
223          * @return the SmartyDoc source code scanner
224          */
225         protected RuleBasedScanner getSmartyDocScanner() {
226                 return fJavaTextTools.getSmartyDocScanner();
227         }
228         
229                 
230   /**
231    * Returns the PHPDoc source code scanner for this configuration.
232    *
233    * @return the PHPDoc source code scanner
234    */
235   protected RuleBasedScanner getPHPDocScanner() {
236     return fJavaTextTools.getJavaDocScanner();
237   }
238
239   /* (non-Javadoc)
240    * Method declared on SourceViewerConfiguration
241    */
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 };
255   }
256
257   /* (non-Javadoc) 
258    * Method declared on SourceViewerConfiguration
259    */
260   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
261
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);
274
275     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
276
277     assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
278
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());
289
290     assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
291     assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
292
293     return assistant;
294   }
295
296   /* (non-Javadoc)
297    * Method declared on SourceViewerConfiguration
298    */
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$
302   //  }
303
304   /*
305    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
306    * @since 2.0
307    */
308   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
309     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
310   }
311
312   /* (non-Javadoc)
313    * Method declared on SourceViewerConfiguration
314    */
315   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
316     return new PHPDoubleClickSelector();
317   }
318
319   /*
320    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
321    */
322   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
323
324     Vector vector = new Vector();
325
326     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
327
328     final IPreferenceStore preferences = PHPeclipsePlugin.getDefault().getPreferenceStore();
329     int tabWidth = preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
330     boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
331
332     for (int i = 0; i <= tabWidth; i++) {
333       StringBuffer prefix = new StringBuffer();
334
335       if (useSpaces) {
336         for (int j = 0; j + i < tabWidth; j++)
337           prefix.append(' ');
338
339         if (i != 0)
340           prefix.append('\t');
341       } else {
342         for (int j = 0; j < i; j++)
343           prefix.append(' ');
344
345         if (i != tabWidth)
346           prefix.append('\t');
347       }
348
349       vector.add(prefix.toString());
350     }
351
352     vector.add(""); //$NON-NLS-1$
353
354     return (String[]) vector.toArray(new String[vector.size()]);
355   }
356   /* (non-Javadoc)
357    * Method declared on SourceViewerConfiguration
358    */
359   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
360     //  PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
361     //    JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
362     PresentationReconciler reconciler = new PresentationReconciler();
363
364     DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
365     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
366     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
367
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);
383     
384                 dr = new DefaultDamagerRepairer(getSmartyScanner());
385                                 reconciler.setDamager(dr, IPHPPartitionScannerConstants.SMARTY);
386                                 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.SMARTY);
387                                 
388                 dr = new DefaultDamagerRepairer(getSmartyDocScanner());
389                                 reconciler.setDamager(dr, IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT);
390                                 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT);
391                                 
392     dr =
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);
397
398     dr = new DefaultDamagerRepairer(getCodeScanner());
399     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
400     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
401
402     dr = new DefaultDamagerRepairer(getPHPDocScanner());
403     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
404     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
405
406     return reconciler;
407   }
408
409   /* (non-Javadoc)
410    * Method declared on SourceViewerConfiguration
411    */
412   public int getTabWidth(ISourceViewer sourceViewer) {
413     return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
414   }
415
416   /* (non-Javadoc)
417    * Method declared on SourceViewerConfiguration
418    */
419   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
420     try {
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 !
425     }
426     return new PHPTextHover(null);
427   }
428
429   /*
430    * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
431    * @since 2.0
432    */
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);
438       }
439     };
440   }
441
442   /*
443    * @see SourceViewerConfiguration#getInformationPresenter(ISourceViewer)
444    * @since 2.0
445    */
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);               
452   //            return presenter;
453   //    }
454
455   /**
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.
459    * 
460    * @param sourceViewer the source viewer to be configured by this configuration
461    * @return an information control creator
462    * @since 2.1
463    */
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);
471       }
472     };
473   }
474 }