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