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