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