7da54c2e5ac463148f85c95b18f6baf945c35d07
[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.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;
30
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;
56
57 /**
58  * Configuration for an <code>SourceViewer</code> which shows PHP code. 
59  */
60 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
61
62   /** 
63    * Preference key used to look up display tab width.
64    * 
65    * @since 2.0
66    */
67   public final static String PREFERENCE_TAB_WIDTH = PreferenceConstants.EDITOR_TAB_WIDTH;
68   /** 
69    * Preference key for inserting spaces rather than tabs.
70    * 
71    * @since 2.0
72    */
73   public final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
74
75   //  public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
76   //IDocument.DEFAULT_CONTENT_TYPE;
77
78   private JavaTextTools fJavaTextTools;
79   private PHPEditor fEditor;
80
81   private ContentFormatter fFormatter;
82   private HTMLFormattingStrategy fFormattingStrategy;
83   /**
84    * Single token scanner.
85    */
86   static class SingleTokenScanner extends BufferedRuleBasedScanner {
87     public SingleTokenScanner(TextAttribute attribute) {
88       setDefaultReturnToken(new Token(attribute));
89     }
90   };
91
92   /**
93    * Default constructor.
94    */
95   public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
96     fJavaTextTools = textTools;
97     fEditor = editor;
98   }
99
100   /*
101    * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
102    */
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));
110     //    }
111     //    return fFormatter;
112
113     if (fFormatter == null) {
114       //ContentFormatter 
115       fFormatter = new ContentFormatter();
116       IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
117
118       fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
119       fFormatter.enablePartitionAwareFormatting(false);
120       fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
121     }
122     return fFormatter;
123   }
124
125   /**
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>.
130    *
131    * @return the partition managing position categories or <code>null</code> 
132    *                    if there is none
133    */
134   public String[] getPartitionManagingPositionCategories() {
135     return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
136   }
137   //  /** 
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>.
142   //   *
143   //   * @return the partition managing position categories or <code>null</code> 
144   //   *                        if there is none
145   //   */
146   //  private String[] getPartitionManagingPositionCategories() {
147   //    return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
148   //  } 
149
150   public PHPEditor getEditor() {
151     return fEditor;
152   }
153
154   /**
155    * Returns the preference store used by this configuration to initialize
156    * the individual bits and pieces.
157    * 
158    * @return the preference store used to initialize this configuration
159    * 
160    * @since 2.0
161    */
162   protected IPreferenceStore getPreferenceStore() {
163     return PHPeclipsePlugin.getDefault().getPreferenceStore();
164   }
165
166   /* (non-Javadoc)
167    * Method declared on SourceViewerConfiguration
168    */
169   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
170     return new PHPAnnotationHover();
171   }
172
173   /* (non-Javadoc)
174    * Method declared on SourceViewerConfiguration
175    */
176   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
177     return (IPHPPartitionScannerConstants.PHP.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
178   }
179
180   /**
181    * Returns the PHP source code scanner for this configuration.
182    *
183    * @return the PHP source code scanner
184    */
185   protected RuleBasedScanner getCodeScanner() {
186     return fJavaTextTools.getCodeScanner();
187   }
188
189   /**
190    * Returns the HTML source code scanner for this configuration.
191    *
192    * @return the HTML source code scanner
193    */
194   protected RuleBasedScanner getHTMLScanner() {
195     return fJavaTextTools.getHTMLScanner();
196   }
197
198   /**
199    * Returns the PHPDoc source code scanner for this configuration.
200    *
201    * @return the PHPDoc source code scanner
202    */
203   protected RuleBasedScanner getPHPDocScanner() {
204     return fJavaTextTools.getJavaDocScanner();
205   }
206
207   /* (non-Javadoc)
208    * Method declared on SourceViewerConfiguration
209    */
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 };
221   }
222
223   /* (non-Javadoc) 
224    * Method declared on SourceViewerConfiguration
225    */
226   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
227
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);
237
238     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
239
240     assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
241
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());
252
253     assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
254     assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
255
256     return assistant;
257   }
258
259   /* (non-Javadoc)
260    * Method declared on SourceViewerConfiguration
261    */
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$
265   //  }
266
267   /*
268    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
269    * @since 2.0
270    */
271   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
272     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
273   }
274
275   /* (non-Javadoc)
276    * Method declared on SourceViewerConfiguration
277    */
278   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
279     return new PHPDoubleClickSelector();
280   }
281
282   /*
283    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
284    */
285   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
286
287     Vector vector = new Vector();
288
289     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
290
291     final IPreferenceStore preferences = PHPeclipsePlugin.getDefault().getPreferenceStore();
292     int tabWidth = preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
293     boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
294
295     for (int i = 0; i <= tabWidth; i++) {
296       StringBuffer prefix = new StringBuffer();
297
298       if (useSpaces) {
299         for (int j = 0; j + i < tabWidth; j++)
300           prefix.append(' ');
301
302         if (i != 0)
303           prefix.append('\t');
304       } else {
305         for (int j = 0; j < i; j++)
306           prefix.append(' ');
307
308         if (i != tabWidth)
309           prefix.append('\t');
310       }
311
312       vector.add(prefix.toString());
313     }
314
315     vector.add(""); //$NON-NLS-1$
316
317     return (String[]) vector.toArray(new String[vector.size()]);
318   }
319   /* (non-Javadoc)
320    * Method declared on SourceViewerConfiguration
321    */
322   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
323     //  PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
324     //    JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
325     PresentationReconciler reconciler = new PresentationReconciler();
326
327     DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
328     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
329     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
330
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);
346     dr =
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);
351
352     dr = new DefaultDamagerRepairer(getCodeScanner());
353     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
354     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
355
356     dr = new DefaultDamagerRepairer(getPHPDocScanner());
357     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
358     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
359
360     return reconciler;
361   }
362
363   /* (non-Javadoc)
364    * Method declared on SourceViewerConfiguration
365    */
366   public int getTabWidth(ISourceViewer sourceViewer) {
367     return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
368   }
369
370   /* (non-Javadoc)
371    * Method declared on SourceViewerConfiguration
372    */
373   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
374     IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
375     return new PHPTextHover(f.getProject());
376   }
377 }