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.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;
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;
53 * Configuration for an <code>SourceViewer</code> which shows PHP code.
55 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
57 public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
58 //IDocument.DEFAULT_CONTENT_TYPE;
60 private JavaTextTools fJavaTextTools;
61 private PHPEditor fEditor;
63 private ContentFormatter fFormatter;
64 private HTMLFormattingStrategy fFormattingStrategy;
66 * Single token scanner.
68 static class SingleTokenScanner extends BufferedRuleBasedScanner {
69 public SingleTokenScanner(TextAttribute attribute) {
70 setDefaultReturnToken(new Token(attribute));
75 * Default constructor.
77 public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
78 fJavaTextTools = textTools;
83 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
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));
95 if (fFormatter == null) {
97 fFormatter = new ContentFormatter();
98 IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
100 fFormatter.setFormattingStrategy(
102 IDocument.DEFAULT_CONTENT_TYPE);
103 fFormatter.enablePartitionAwareFormatting(false);
104 fFormatter.setPartitionManagingPositionCategories(
105 getPartitionManagingPositionCategories());
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>.
116 * @return the partition managing position categories or <code>null</code>
119 public String[] getPartitionManagingPositionCategories() {
120 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
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>.
128 // * @return the partition managing position categories or <code>null</code>
129 // * if there is none
131 // private String[] getPartitionManagingPositionCategories() {
132 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
135 public PHPEditor getEditor() {
140 * Method declared on SourceViewerConfiguration
142 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
143 return new PHPAnnotationHover();
147 * Method declared on SourceViewerConfiguration
149 public IAutoIndentStrategy getAutoIndentStrategy(
150 ISourceViewer sourceViewer,
151 String contentType) {
153 IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)
154 ? new PHPAutoIndentStrategy()
155 : new DefaultAutoIndentStrategy());
159 * Method declared on SourceViewerConfiguration
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 };
175 * Method declared on SourceViewerConfiguration
177 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
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);
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)));
203 * Method declared on SourceViewerConfiguration
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$
211 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
214 public String[] getDefaultPrefixes(
215 ISourceViewer sourceViewer,
216 String contentType) {
217 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
221 * Method declared on SourceViewerConfiguration
223 public ITextDoubleClickStrategy getDoubleClickStrategy(
224 ISourceViewer sourceViewer,
225 String contentType) {
226 return new PHPDoubleClickSelector();
230 * Method declared on SourceViewerConfiguration
232 // public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
233 // return new String[] { "\t", " " }; //$NON-NLS-1$ //$NON-NLS-2$
237 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
239 public String[] getIndentPrefixes(
240 ISourceViewer sourceViewer,
241 String contentType) {
243 Vector vector = new Vector();
245 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
247 final IPreferenceStore store =
248 PHPeclipsePlugin.getDefault().getPreferenceStore();
250 int tabWidth = store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
251 boolean useSpaces = store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
253 for (int i = 0; i <= tabWidth; i++) {
254 StringBuffer prefix = new StringBuffer();
257 for (int j = 0; j + i < tabWidth; j++)
263 for (int j = 0; j < i; j++)
270 vector.add(prefix.toString());
273 vector.add(""); //$NON-NLS-1$
275 return (String[]) vector.toArray(new String[vector.size()]);
278 * Method declared on SourceViewerConfiguration
280 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
282 // PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
283 JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
284 PresentationReconciler reconciler = new PresentationReconciler();
286 DefaultDamagerRepairer dr =
287 new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
288 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
289 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
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);
295 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
296 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
297 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
300 new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPDocCodeScanner());
301 reconciler.setDamager(
303 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
304 reconciler.setRepairer(
306 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
308 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
309 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
310 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
313 new DefaultDamagerRepairer(
314 new SingleTokenScanner(
316 provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
317 reconciler.setDamager(
319 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
320 reconciler.setRepairer(
322 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
328 * Method declared on SourceViewerConfiguration
330 public int getTabWidth(ISourceViewer sourceViewer) {
335 * Method declared on SourceViewerConfiguration
337 public ITextHover getTextHover(
338 ISourceViewer sourceViewer,
339 String contentType) {
340 return new PHPTextHover();