Added auto activation for HTML characters (i.e. <&#) in the Preference Page
[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.JavaColorManager;
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.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
56 /**
57  * Configuration for an <code>SourceViewer</code> which shows PHP code. 
58  */
59 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
60
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
76 //  public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
77   //IDocument.DEFAULT_CONTENT_TYPE;
78
79   private JavaTextTools fJavaTextTools;
80   private PHPEditor fEditor;
81
82   private ContentFormatter fFormatter;
83   private HTMLFormattingStrategy fFormattingStrategy;
84   /**
85    * Single token scanner.
86    */
87   static class SingleTokenScanner extends BufferedRuleBasedScanner {
88     public SingleTokenScanner(TextAttribute attribute) {
89       setDefaultReturnToken(new Token(attribute));
90     }
91   };
92
93   /**
94    * Default constructor.
95    */
96   public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
97     fJavaTextTools = textTools;
98     fEditor = editor;
99   }
100
101   /*
102    * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
103    */
104   public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
105     //    if (fFormatter == null) {
106     //      fFormatter = new ContentFormatter();
107     //      fFormattingStrategy = new HTMLFormattingStrategy(this, sourceViewer);
108     //      fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
109     //      fFormatter.enablePartitionAwareFormatting(false);
110     //      fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
111     //    }
112     //    return fFormatter;
113
114     if (fFormatter == null) {
115       //ContentFormatter 
116       fFormatter = new ContentFormatter();
117       IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
118
119       fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
120       fFormatter.enablePartitionAwareFormatting(false);
121       fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
122     }
123     return fFormatter;
124   }
125
126   /**
127    * Returns the names of the document position categories used by the document
128    * partitioners created by this object to manage their partition information.
129    * If the partitioners don't use document position categories, the returned
130    * result is <code>null</code>.
131    *
132    * @return the partition managing position categories or <code>null</code> 
133    *                    if there is none
134    */
135   public String[] getPartitionManagingPositionCategories() {
136     return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
137   }
138   //  /** 
139   //   * Returns the names of the document position categories used by the document
140   //   * partitioners created by this object to manage their partition information.
141   //   * If the partitioners don't use document position categories, the returned
142   //   * result is <code>null</code>.
143   //   *
144   //   * @return the partition managing position categories or <code>null</code> 
145   //   *                        if there is none
146   //   */
147   //  private String[] getPartitionManagingPositionCategories() {
148   //    return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
149   //  } 
150
151   public PHPEditor getEditor() {
152     return fEditor;
153   }
154
155   /**
156    * Returns the preference store used by this configuration to initialize
157    * the individual bits and pieces.
158    * 
159    * @return the preference store used to initialize this configuration
160    * 
161    * @since 2.0
162    */
163   protected IPreferenceStore getPreferenceStore() {
164     return PHPeclipsePlugin.getDefault().getPreferenceStore();
165   }
166
167   /* (non-Javadoc)
168    * Method declared on SourceViewerConfiguration
169    */
170   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
171     return new PHPAnnotationHover();
172   }
173
174   /* (non-Javadoc)
175    * Method declared on SourceViewerConfiguration
176    */
177   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
178     return (IPHPPartitionScannerConstants.PHP.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
179   }
180
181   /**
182    * Returns the PHP source code scanner for this configuration.
183    *
184    * @return the PHP source code scanner
185    */
186   protected RuleBasedScanner getCodeScanner() {
187     return fJavaTextTools.getCodeScanner();
188   }
189   
190   /**
191    * Returns the HTML source code scanner for this configuration.
192    *
193    * @return the HTML source code scanner
194    */
195   protected RuleBasedScanner getHTMLScanner() {
196     return fJavaTextTools.getHTMLScanner();
197   }
198   
199   /**
200    * Returns the PHPDoc source code scanner for this configuration.
201    *
202    * @return the PHPDoc source code scanner
203    */
204   protected RuleBasedScanner getPHPDocScanner() {
205     return fJavaTextTools.getJavaDocScanner();
206   }
207   
208   /* (non-Javadoc)
209    * Method declared on SourceViewerConfiguration
210    */
211   public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
212     return new String[] {
213       IPHPPartitionScannerConstants.HTML,
214         IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
215         IPHPPartitionScannerConstants.PHP,
216         IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
217         IPHPPartitionScannerConstants.CSS,
218         IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
219         IPHPPartitionScannerConstants.JAVASCRIPT,
220         IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT,
221       IDocument.DEFAULT_CONTENT_TYPE };
222   }
223
224   /* (non-Javadoc) 
225    * Method declared on SourceViewerConfiguration
226    */
227   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
228
229     ContentAssistant assistant = new ContentAssistant();
230     IContentAssistProcessor processor = new HTMLCompletionProcessor();
231     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML);
232     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
233     assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
234     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS);
235     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
236     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JAVASCRIPT);
237     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
238
239     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
240
241     assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
242
243     //    assistant.enableAutoActivation(true);
244     //    assistant.setAutoActivationDelay(500);
245     //    assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
246     //    ContentAssistPreference.configure(assistant, getPreferenceStore());
247     //    assistant.setContextInformationPopupOrientation(
248     //      ContentAssistant.CONTEXT_INFO_ABOVE);
249     //    assistant.setContextInformationPopupBackground(
250     //      PHPEditorEnvironment.getPHPColorProvider().getColor(
251     //        new RGB(150, 150, 0)));
252     ContentAssistPreference.configure(assistant, getPreferenceStore());
253
254     assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
255     assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
256
257     return assistant;
258   }
259
260   /* (non-Javadoc)
261    * Method declared on SourceViewerConfiguration
262    */
263   //  public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
264   //    return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
265   //    // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
266   //  }
267
268   /*
269    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
270    * @since 2.0
271    */
272   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
273     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
274   }
275
276   /* (non-Javadoc)
277    * Method declared on SourceViewerConfiguration
278    */
279   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
280     return new PHPDoubleClickSelector();
281   }
282
283   /*
284    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
285    */
286   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
287
288     Vector vector = new Vector();
289
290     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
291
292     final IPreferenceStore preferences= PHPeclipsePlugin.getDefault().getPreferenceStore();
293     int tabWidth= preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
294     boolean useSpaces= getPreferenceStore().getBoolean(SPACES_FOR_TABS);
295     
296     for (int i = 0; i <= tabWidth; i++) {
297       StringBuffer prefix = new StringBuffer();
298
299       if (useSpaces) {
300         for (int j = 0; j + i < tabWidth; j++)
301           prefix.append(' ');
302
303         if (i != 0)
304           prefix.append('\t');
305       } else {
306         for (int j = 0; j < i; j++)
307           prefix.append(' ');
308
309         if (i != tabWidth)
310           prefix.append('\t');
311       }
312
313       vector.add(prefix.toString());
314     }
315
316     vector.add(""); //$NON-NLS-1$
317
318     return (String[]) vector.toArray(new String[vector.size()]);
319   }
320   /* (non-Javadoc)
321    * Method declared on SourceViewerConfiguration
322    */
323   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
324     //  PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
325     JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
326     PresentationReconciler reconciler = new PresentationReconciler();
327
328     DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
329     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
330     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
331
332     dr = new DefaultDamagerRepairer(getHTMLScanner());
333     reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
334     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
335     dr = new DefaultDamagerRepairer(getHTMLScanner());
336     reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
337     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
338     dr = new DefaultDamagerRepairer(getHTMLScanner());
339     reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
340     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
341     dr = new DefaultDamagerRepairer(getHTMLScanner());
342     reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
343     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
344     dr = new DefaultDamagerRepairer(getHTMLScanner());
345     reconciler.setDamager(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
346     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
347     dr =
348       new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.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     return new PHPTextHover();
375   }
376 }