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
9 IBM Corporation - Initial implementation
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
14 import java.util.Vector;
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;
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;
51 * Configuration for an <code>SourceViewer</code> which shows PHP code.
53 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
55 public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML; //IDocument.DEFAULT_CONTENT_TYPE;
57 private PHPEditor fEditor;
59 private ContentFormatter fFormatter;
60 private HTMLFormattingStrategy fFormattingStrategy;
62 * Single token scanner.
64 static class SingleTokenScanner extends BufferedRuleBasedScanner {
65 public SingleTokenScanner(TextAttribute attribute) {
66 setDefaultReturnToken(new Token(attribute));
71 * Default constructor.
73 public PHPSourceViewerConfiguration(PHPEditor editor) {
78 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
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));
90 if (fFormatter == null) {
92 fFormatter= new ContentFormatter();
93 IFormattingStrategy strategy= new JavaFormattingStrategy(sourceViewer);
95 fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
96 fFormatter.enablePartitionAwareFormatting(false);
97 fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
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>.
108 * @return the partition managing position categories or <code>null</code>
111 public String[] getPartitionManagingPositionCategories() {
112 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
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>.
120 // * @return the partition managing position categories or <code>null</code>
121 // * if there is none
123 // private String[] getPartitionManagingPositionCategories() {
124 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
127 public PHPEditor getEditor() {
131 * Method declared on SourceViewerConfiguration
133 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
134 return new PHPAnnotationHover();
138 * Method declared on SourceViewerConfiguration
140 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
141 return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
145 * Method declared on SourceViewerConfiguration
147 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer)
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
163 * Method declared on SourceViewerConfiguration
165 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
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);
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)));
182 * Method declared on SourceViewerConfiguration
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$
190 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
193 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
194 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
198 * Method declared on SourceViewerConfiguration
200 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
201 return new PHPDoubleClickSelector();
205 * Method declared on SourceViewerConfiguration
207 // public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
208 // return new String[] { "\t", " " }; //$NON-NLS-1$ //$NON-NLS-2$
212 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
214 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
216 Vector vector= new Vector();
218 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
220 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
222 int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
223 boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
225 for (int i= 0; i <= tabWidth; i++) {
226 StringBuffer prefix= new StringBuffer();
229 for (int j= 0; j + i < tabWidth; j++)
235 for (int j= 0; j < i; j++)
242 vector.add(prefix.toString());
245 vector.add(""); //$NON-NLS-1$
247 return (String[]) vector.toArray(new String[vector.size()]);
250 * Method declared on SourceViewerConfiguration
252 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
254 PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
255 PresentationReconciler reconciler = new PresentationReconciler();
257 DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
258 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
259 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
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);
265 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
266 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
267 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
269 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPDocCodeScanner());
270 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
271 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
273 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
274 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
275 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
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);
285 * Method declared on SourceViewerConfiguration
287 public int getTabWidth(ISourceViewer sourceViewer) {
292 * Method declared on SourceViewerConfiguration
294 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
295 return new PHPTextHover();