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.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;
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;
50 * Configuration for an <code>SourceViewer</code> which shows PHP code.
52 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
54 public static final String HTML_DEFAULT = IDocument.DEFAULT_CONTENT_TYPE;
56 private PHPEditor fEditor;
58 private ContentFormatter fFormatter;
59 private HTMLFormattingStrategy fFormattingStrategy;
61 * Single token scanner.
63 static class SingleTokenScanner extends BufferedRuleBasedScanner {
64 public SingleTokenScanner(TextAttribute attribute) {
65 setDefaultReturnToken(new Token(attribute));
70 * Default constructor.
72 public PHPSourceViewerConfiguration(PHPEditor editor) {
77 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
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));
89 if (fFormatter == null) {
91 fFormatter= new ContentFormatter();
92 IFormattingStrategy strategy= new JavaFormattingStrategy(sourceViewer);
94 fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
95 fFormatter.enablePartitionAwareFormatting(false);
96 fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
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>.
107 * @return the partition managing position categories or <code>null</code>
110 public String[] getPartitionManagingPositionCategories() {
111 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
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>.
119 // * @return the partition managing position categories or <code>null</code>
120 // * if there is none
122 // private String[] getPartitionManagingPositionCategories() {
123 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
126 public PHPEditor getEditor() {
130 * Method declared on SourceViewerConfiguration
132 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
133 return new PHPAnnotationHover();
137 * Method declared on SourceViewerConfiguration
139 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
140 return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
144 * Method declared on SourceViewerConfiguration
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 };
153 * Method declared on SourceViewerConfiguration
155 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
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);
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)));
172 * Method declared on SourceViewerConfiguration
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$
180 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
183 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
184 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
188 * Method declared on SourceViewerConfiguration
190 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
191 return new PHPDoubleClickSelector();
195 * Method declared on SourceViewerConfiguration
197 // public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
198 // return new String[] { "\t", " " }; //$NON-NLS-1$ //$NON-NLS-2$
202 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
204 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
206 Vector vector= new Vector();
208 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
210 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
212 int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
213 boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
215 for (int i= 0; i <= tabWidth; i++) {
216 StringBuffer prefix= new StringBuffer();
219 for (int j= 0; j + i < tabWidth; j++)
225 for (int j= 0; j < i; j++)
232 vector.add(prefix.toString());
235 vector.add(""); //$NON-NLS-1$
237 return (String[]) vector.toArray(new String[vector.size()]);
240 * Method declared on SourceViewerConfiguration
242 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
244 PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
245 PresentationReconciler reconciler = new PresentationReconciler();
247 DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
248 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
249 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
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);
255 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
256 reconciler.setDamager(dr, PHPPartitionScanner.PHP);
257 reconciler.setRepairer(dr, PHPPartitionScanner.PHP);
259 // dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
260 // reconciler.setDamager(dr, PHPPartitionScanner.HTML);
261 // reconciler.setRepairer(dr, PHPPartitionScanner.HTML);
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);
271 * Method declared on SourceViewerConfiguration
273 public int getTabWidth(ISourceViewer sourceViewer) {
278 * Method declared on SourceViewerConfiguration
280 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
281 return new PHPTextHover();