ab128ff6572880b57f50a19a6a2897282fb5e97a
[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.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;
31
32 import org.eclipse.core.resources.IFile;
33 import org.eclipse.jface.preference.IPreferenceStore;
34 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
35 import org.eclipse.jface.text.DefaultInformationControl;
36 import org.eclipse.jface.text.IAutoIndentStrategy;
37 import org.eclipse.jface.text.IDocument;
38 import org.eclipse.jface.text.IInformationControl;
39 import org.eclipse.jface.text.IInformationControlCreator;
40 import org.eclipse.jface.text.ITextDoubleClickStrategy;
41 import org.eclipse.jface.text.ITextHover;
42 import org.eclipse.jface.text.TextAttribute;
43 import org.eclipse.jface.text.contentassist.ContentAssistant;
44 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
45 import org.eclipse.jface.text.contentassist.IContentAssistant;
46 import org.eclipse.jface.text.formatter.ContentFormatter;
47 import org.eclipse.jface.text.formatter.IContentFormatter;
48 import org.eclipse.jface.text.formatter.IFormattingStrategy;
49 import org.eclipse.jface.text.presentation.IPresentationReconciler;
50 import org.eclipse.jface.text.presentation.PresentationReconciler;
51 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
52 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
53 import org.eclipse.jface.text.rules.DefaultPartitioner;
54 import org.eclipse.jface.text.rules.RuleBasedScanner;
55 import org.eclipse.jface.text.rules.Token;
56 import org.eclipse.jface.text.source.IAnnotationHover;
57 import org.eclipse.jface.text.source.ISourceViewer;
58 import org.eclipse.jface.text.source.SourceViewerConfiguration;
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.widgets.Shell;
61 import org.eclipse.ui.IFileEditorInput;
62
63 /**
64  * Configuration for an <code>SourceViewer</code> which shows PHP code. 
65  */
66 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
67
68   /** 
69    * Preference key used to look up display tab width.
70    * 
71    * @since 2.0
72    */
73   public final static String PREFERENCE_TAB_WIDTH = PreferenceConstants.EDITOR_TAB_WIDTH;
74   /** 
75    * Preference key for inserting spaces rather than tabs.
76    * 
77    * @since 2.0
78    */
79   public final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
80
81   //  public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
82   //IDocument.DEFAULT_CONTENT_TYPE;
83
84   private JavaTextTools fJavaTextTools;
85   private PHPEditor fEditor;
86
87   private ContentFormatter fFormatter;
88   private HTMLFormattingStrategy fFormattingStrategy;
89   /**
90    * Single token scanner.
91    */
92   static class SingleTokenScanner extends BufferedRuleBasedScanner {
93     public SingleTokenScanner(TextAttribute attribute) {
94       setDefaultReturnToken(new Token(attribute));
95     }
96   };
97
98   /**
99    * Default constructor.
100    */
101   public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
102     fJavaTextTools = textTools;
103     fEditor = editor;
104   }
105
106   /*
107    * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
108    */
109   public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
110     //    if (fFormatter == null) {
111     //      fFormatter = new ContentFormatter();
112     //      fFormattingStrategy = new HTMLFormattingStrategy(this, sourceViewer);
113     //      fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
114     //      fFormatter.enablePartitionAwareFormatting(false);
115     //      fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
116     //    }
117     //    return fFormatter;
118
119     if (fFormatter == null) {
120       //ContentFormatter 
121       fFormatter = new ContentFormatter();
122       IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
123
124       fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
125       fFormatter.enablePartitionAwareFormatting(false);
126       fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
127     }
128     return fFormatter;
129   }
130
131   /**
132    * Returns the names of the document position categories used by the document
133    * partitioners created by this object to manage their partition information.
134    * If the partitioners don't use document position categories, the returned
135    * result is <code>null</code>.
136    *
137    * @return the partition managing position categories or <code>null</code> 
138    *                    if there is none
139    */
140   public String[] getPartitionManagingPositionCategories() {
141     return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
142   }
143   //  /** 
144   //   * Returns the names of the document position categories used by the document
145   //   * partitioners created by this object to manage their partition information.
146   //   * If the partitioners don't use document position categories, the returned
147   //   * result is <code>null</code>.
148   //   *
149   //   * @return the partition managing position categories or <code>null</code> 
150   //   *                        if there is none
151   //   */
152   //  private String[] getPartitionManagingPositionCategories() {
153   //    return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
154   //  } 
155
156   public PHPEditor getEditor() {
157     return fEditor;
158   }
159
160   /**
161    * Returns the preference store used by this configuration to initialize
162    * the individual bits and pieces.
163    * 
164    * @return the preference store used to initialize this configuration
165    * 
166    * @since 2.0
167    */
168   protected IPreferenceStore getPreferenceStore() {
169     return PHPeclipsePlugin.getDefault().getPreferenceStore();
170   }
171
172   /* (non-Javadoc)
173    * Method declared on SourceViewerConfiguration
174    */
175   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
176     return new PHPAnnotationHover();
177   }
178
179   /* (non-Javadoc)
180    * Method declared on SourceViewerConfiguration
181    */
182   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
183     return (IPHPPartitionScannerConstants.PHP.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
184   }
185
186   /**
187    * Returns the PHP source code scanner for this configuration.
188    *
189    * @return the PHP source code scanner
190    */
191   protected RuleBasedScanner getCodeScanner() {
192     return fJavaTextTools.getCodeScanner();
193   }
194
195   /**
196    * Returns the HTML source code scanner for this configuration.
197    *
198    * @return the HTML source code scanner
199    */
200   protected RuleBasedScanner getHTMLScanner() {
201     return fJavaTextTools.getHTMLScanner();
202   }
203
204   /**
205    * Returns the PHPDoc source code scanner for this configuration.
206    *
207    * @return the PHPDoc source code scanner
208    */
209   protected RuleBasedScanner getPHPDocScanner() {
210     return fJavaTextTools.getJavaDocScanner();
211   }
212
213   /* (non-Javadoc)
214    * Method declared on SourceViewerConfiguration
215    */
216   public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
217     return new String[] {
218       IPHPPartitionScannerConstants.HTML,
219       IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
220       IPHPPartitionScannerConstants.PHP,
221       IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
222       IPHPPartitionScannerConstants.CSS,
223       IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
224       IPHPPartitionScannerConstants.JAVASCRIPT,
225       IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT,
226       IDocument.DEFAULT_CONTENT_TYPE };
227   }
228
229   /* (non-Javadoc) 
230    * Method declared on SourceViewerConfiguration
231    */
232   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
233
234     ContentAssistant assistant = new ContentAssistant();
235     IContentAssistProcessor processor = new HTMLCompletionProcessor();
236     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML);
237     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
238     assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
239     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS);
240     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
241     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JAVASCRIPT);
242     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
243
244     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
245
246     assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
247
248     //    assistant.enableAutoActivation(true);
249     //    assistant.setAutoActivationDelay(500);
250     //    assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
251     //    ContentAssistPreference.configure(assistant, getPreferenceStore());
252     //    assistant.setContextInformationPopupOrientation(
253     //      ContentAssistant.CONTEXT_INFO_ABOVE);
254     //    assistant.setContextInformationPopupBackground(
255     //      PHPEditorEnvironment.getPHPColorProvider().getColor(
256     //        new RGB(150, 150, 0)));
257     ContentAssistPreference.configure(assistant, getPreferenceStore());
258
259     assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
260     assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
261
262     return assistant;
263   }
264
265   /* (non-Javadoc)
266    * Method declared on SourceViewerConfiguration
267    */
268   //  public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
269   //    return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
270   //    // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
271   //  }
272
273   /*
274    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
275    * @since 2.0
276    */
277   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
278     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
279   }
280
281   /* (non-Javadoc)
282    * Method declared on SourceViewerConfiguration
283    */
284   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
285     return new PHPDoubleClickSelector();
286   }
287
288   /*
289    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
290    */
291   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
292
293     Vector vector = new Vector();
294
295     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
296
297     final IPreferenceStore preferences = PHPeclipsePlugin.getDefault().getPreferenceStore();
298     int tabWidth = preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
299     boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
300
301     for (int i = 0; i <= tabWidth; i++) {
302       StringBuffer prefix = new StringBuffer();
303
304       if (useSpaces) {
305         for (int j = 0; j + i < tabWidth; j++)
306           prefix.append(' ');
307
308         if (i != 0)
309           prefix.append('\t');
310       } else {
311         for (int j = 0; j < i; j++)
312           prefix.append(' ');
313
314         if (i != tabWidth)
315           prefix.append('\t');
316       }
317
318       vector.add(prefix.toString());
319     }
320
321     vector.add(""); //$NON-NLS-1$
322
323     return (String[]) vector.toArray(new String[vector.size()]);
324   }
325   /* (non-Javadoc)
326    * Method declared on SourceViewerConfiguration
327    */
328   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
329     //  PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
330     //    JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
331     PresentationReconciler reconciler = new PresentationReconciler();
332
333     DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
334     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
335     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
336
337     dr = new DefaultDamagerRepairer(getHTMLScanner());
338     reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
339     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
340     dr = new DefaultDamagerRepairer(getHTMLScanner());
341     reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
342     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
343     dr = new DefaultDamagerRepairer(getHTMLScanner());
344     reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
345     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
346     dr = new DefaultDamagerRepairer(getHTMLScanner());
347     reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
348     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
349     dr = new DefaultDamagerRepairer(getHTMLScanner());
350     reconciler.setDamager(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
351     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
352     dr =
353       new DefaultDamagerRepairer(
354         new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
355     reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
356     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
357
358     dr = new DefaultDamagerRepairer(getCodeScanner());
359     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
360     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
361
362     dr = new DefaultDamagerRepairer(getPHPDocScanner());
363     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
364     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
365
366     return reconciler;
367   }
368
369   /* (non-Javadoc)
370    * Method declared on SourceViewerConfiguration
371    */
372   public int getTabWidth(ISourceViewer sourceViewer) {
373     return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
374   }
375
376   /* (non-Javadoc)
377    * Method declared on SourceViewerConfiguration
378    */
379   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
380     try {
381       IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
382       return new PHPTextHover(f.getProject());
383     } catch (NullPointerException e) {
384         // this exception occurs, if getTextHover is called by preference pages !
385     }
386     return new PHPTextHover(null);
387   }
388   
389         /*
390          * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
391          * @since 2.0
392          */
393         public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
394                 return new IInformationControlCreator() {
395                         public IInformationControl createInformationControl(Shell parent) {
396                                 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
397                                 // return new HoverBrowserControl(parent);
398                         }
399                 };
400         }
401 }