87a028937851344abc57dbd0fae1822160f44c17
[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 = IPHPPartitionScannerConstants.HTML; //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   
131   /* (non-Javadoc)
132    * Method declared on SourceViewerConfiguration
133    */
134   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
135     return new PHPAnnotationHover();
136   }
137
138   /* (non-Javadoc)
139    * Method declared on SourceViewerConfiguration
140    */
141   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
142     return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
143   }
144
145 /* (non-Javadoc)
146  * Method declared on SourceViewerConfiguration
147  */
148 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer)
149 {
150     return new String[] {
151         IDocument.DEFAULT_CONTENT_TYPE,
152         IPHPPartitionScannerConstants.PHP,
153         IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
154         IPHPPartitionScannerConstants.HTML,
155         IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
156         IPHPPartitionScannerConstants.CSS,
157         IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
158         IPHPPartitionScannerConstants.JAVASCRIPT,
159         IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT
160         };
161   }
162
163   /* (non-Javadoc) 
164    * Method declared on SourceViewerConfiguration
165    */
166   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
167
168     ContentAssistant assistant = new ContentAssistant();
169     assistant.setContentAssistProcessor(new HTMLCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
170     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
171     assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
172     
173     assistant.enableAutoActivation(true);
174     assistant.setAutoActivationDelay(500);
175     assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
176     assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
177     assistant.setContextInformationPopupBackground(PHPEditorEnvironment.getPHPColorProvider().getColor(new RGB(150, 150, 0)));
178
179     return assistant;
180   }
181
182   /* (non-Javadoc)
183    * Method declared on SourceViewerConfiguration
184    */
185 //  public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
186 //    return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
187 //    // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
188 //  }
189
190   /*
191    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
192    * @since 2.0
193    */
194   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
195     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
196   }
197   
198   /* (non-Javadoc)
199    * Method declared on SourceViewerConfiguration
200    */
201   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
202     return new PHPDoubleClickSelector();
203   }
204
205   /* (non-Javadoc)
206    * Method declared on SourceViewerConfiguration
207    */
208 //  public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
209 //    return new String[] { "\t", "    " }; //$NON-NLS-1$ //$NON-NLS-2$
210 //  }
211
212   /*
213    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
214    */
215   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
216
217     Vector vector= new Vector();
218
219     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
220         
221     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
222
223     int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
224     boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
225     
226     for (int i= 0; i <= tabWidth; i++) {
227         StringBuffer prefix= new StringBuffer();
228
229       if (useSpaces) {
230           for (int j= 0; j + i < tabWidth; j++)
231             prefix.append(' ');
232           
233         if (i != 0)
234             prefix.append('\t');        
235       } else {    
236           for (int j= 0; j < i; j++)
237             prefix.append(' ');
238           
239         if (i != tabWidth)
240             prefix.append('\t');
241       }
242       
243       vector.add(prefix.toString());
244     }
245
246     vector.add(""); //$NON-NLS-1$
247     
248     return (String[]) vector.toArray(new String[vector.size()]);
249   }
250   /* (non-Javadoc)
251    * Method declared on SourceViewerConfiguration
252    */
253   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
254
255     PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
256     PresentationReconciler reconciler = new PresentationReconciler();
257     
258     DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
259     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
260     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
261     
262 //    dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(provider.HTML_DEFAULT))));
263 //    reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
264 //    reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
265
266         dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
267     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
268     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
269
270     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPDocCodeScanner());
271     reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
272     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
273     
274     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
275     reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
276     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
277
278     dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
279     reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
280     reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
281
282     return reconciler;
283   }
284
285   /* (non-Javadoc)
286    * Method declared on SourceViewerConfiguration
287    */
288   public int getTabWidth(ISourceViewer sourceViewer) {
289     return 4;
290   }
291
292   /* (non-Javadoc)
293    * Method declared on SourceViewerConfiguration
294    */
295   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
296     return new PHPTextHover();
297   }
298 }