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() {
132 * Method declared on SourceViewerConfiguration
134 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
135 return new PHPAnnotationHover();
139 * Method declared on SourceViewerConfiguration
141 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
142 return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
146 * Method declared on SourceViewerConfiguration
148 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer)
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
164 * Method declared on SourceViewerConfiguration
166 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
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);
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)));
183 * Method declared on SourceViewerConfiguration
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$
191 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
194 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
195 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
199 * Method declared on SourceViewerConfiguration
201 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
202 return new PHPDoubleClickSelector();
206 * Method declared on SourceViewerConfiguration
208 // public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
209 // return new String[] { "\t", " " }; //$NON-NLS-1$ //$NON-NLS-2$
213 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
215 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
217 Vector vector= new Vector();
219 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
221 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
223 int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
224 boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
226 for (int i= 0; i <= tabWidth; i++) {
227 StringBuffer prefix= new StringBuffer();
230 for (int j= 0; j + i < tabWidth; j++)
236 for (int j= 0; j < i; j++)
243 vector.add(prefix.toString());
246 vector.add(""); //$NON-NLS-1$
248 return (String[]) vector.toArray(new String[vector.size()]);
251 * Method declared on SourceViewerConfiguration
253 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
255 PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
256 PresentationReconciler reconciler = new PresentationReconciler();
258 DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
259 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
260 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
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);
266 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
267 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
268 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
270 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPDocCodeScanner());
271 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
272 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
274 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
275 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
276 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
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);
286 * Method declared on SourceViewerConfiguration
288 public int getTabWidth(ISourceViewer sourceViewer) {
293 * Method declared on SourceViewerConfiguration
295 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
296 return new PHPTextHover();