45ee0367e58307d3fb835db0763e91def7fdbfd7
[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.core.runtime.Preferences;
33 import org.eclipse.jface.preference.IPreferenceStore;
34 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
35 import org.eclipse.jface.text.IAutoIndentStrategy;
36 import org.eclipse.jface.text.IDocument;
37 import org.eclipse.jface.text.ITextDoubleClickStrategy;
38 import org.eclipse.jface.text.ITextHover;
39 import org.eclipse.jface.text.TextAttribute;
40 import org.eclipse.jface.text.contentassist.ContentAssistant;
41 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
42 import org.eclipse.jface.text.contentassist.IContentAssistant;
43 import org.eclipse.jface.text.formatter.ContentFormatter;
44 import org.eclipse.jface.text.formatter.IContentFormatter;
45 import org.eclipse.jface.text.formatter.IFormattingStrategy;
46 import org.eclipse.jface.text.presentation.IPresentationReconciler;
47 import org.eclipse.jface.text.presentation.PresentationReconciler;
48 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
49 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
50 import org.eclipse.jface.text.rules.DefaultPartitioner;
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   /* (non-Javadoc)
182    * Method declared on SourceViewerConfiguration
183    */
184   public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
185     return new String[] {
186       //    IDocument.DEFAULT_CONTENT_TYPE,
187       IPHPPartitionScannerConstants.HTML,
188         IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
189         IPHPPartitionScannerConstants.PHP,
190         IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
191         IPHPPartitionScannerConstants.CSS,
192         IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
193         IPHPPartitionScannerConstants.JAVASCRIPT,
194         IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT };
195   }
196
197   /* (non-Javadoc) 
198    * Method declared on SourceViewerConfiguration
199    */
200   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
201
202     ContentAssistant assistant = new ContentAssistant();
203     IContentAssistProcessor processor = new HTMLCompletionProcessor();
204     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML);
205     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
206     assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
207     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS);
208     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
209     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JAVASCRIPT);
210     assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
211
212     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
213
214     assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
215
216     //    assistant.enableAutoActivation(true);
217     //    assistant.setAutoActivationDelay(500);
218     //    assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
219     //    ContentAssistPreference.configure(assistant, getPreferenceStore());
220     //    assistant.setContextInformationPopupOrientation(
221     //      ContentAssistant.CONTEXT_INFO_ABOVE);
222     //    assistant.setContextInformationPopupBackground(
223     //      PHPEditorEnvironment.getPHPColorProvider().getColor(
224     //        new RGB(150, 150, 0)));
225     ContentAssistPreference.configure(assistant, getPreferenceStore());
226
227     assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
228     assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
229
230     return assistant;
231   }
232
233   /* (non-Javadoc)
234    * Method declared on SourceViewerConfiguration
235    */
236   //  public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
237   //    return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
238   //    // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
239   //  }
240
241   /*
242    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
243    * @since 2.0
244    */
245   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
246     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
247   }
248
249   /* (non-Javadoc)
250    * Method declared on SourceViewerConfiguration
251    */
252   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
253     return new PHPDoubleClickSelector();
254   }
255
256   /*
257    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
258    */
259   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
260
261     Vector vector = new Vector();
262
263     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
264
265     final IPreferenceStore preferences= PHPeclipsePlugin.getDefault().getPreferenceStore();
266     int tabWidth= preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
267     boolean useSpaces= getPreferenceStore().getBoolean(SPACES_FOR_TABS);
268     
269     for (int i = 0; i <= tabWidth; i++) {
270       StringBuffer prefix = new StringBuffer();
271
272       if (useSpaces) {
273         for (int j = 0; j + i < tabWidth; j++)
274           prefix.append(' ');
275
276         if (i != 0)
277           prefix.append('\t');
278       } else {
279         for (int j = 0; j < i; j++)
280           prefix.append(' ');
281
282         if (i != tabWidth)
283           prefix.append('\t');
284       }
285
286       vector.add(prefix.toString());
287     }
288
289     vector.add(""); //$NON-NLS-1$
290
291     return (String[]) vector.toArray(new String[vector.size()]);
292   }
293   /* (non-Javadoc)
294    * Method declared on SourceViewerConfiguration
295    */
296   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
297     //  PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
298     JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
299     PresentationReconciler reconciler = new PresentationReconciler();
300
301     DefaultDamagerRepairer dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
302     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
303     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
304
305     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
306     reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
307     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
308     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
309     reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
310     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
311     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
312     reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
313     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
314     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
315     reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
316     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
317     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
318     reconciler.setDamager(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
319     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
320     dr =
321       new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
322     reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
323     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
324
325     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
326     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
327     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
328
329     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPDocCodeScanner());
330     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
331     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
332
333     return reconciler;
334   }
335
336   /* (non-Javadoc)
337    * Method declared on SourceViewerConfiguration
338    */
339   public int getTabWidth(ISourceViewer sourceViewer) {
340     return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
341   }
342
343   /* (non-Javadoc)
344    * Method declared on SourceViewerConfiguration
345    */
346   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
347     return new PHPTextHover();
348   }
349 }