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.phpeclipse.PHPeclipsePlugin;
17 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
18 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
19 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
20 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
21 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
25 import org.eclipse.jface.text.IAutoIndentStrategy;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.ITextDoubleClickStrategy;
28 import org.eclipse.jface.text.ITextHover;
29 import org.eclipse.jface.text.TextAttribute;
30 import org.eclipse.jface.text.contentassist.ContentAssistant;
31 import org.eclipse.jface.text.contentassist.IContentAssistant;
32 import org.eclipse.jface.text.presentation.IPresentationReconciler;
33 import org.eclipse.jface.text.presentation.PresentationReconciler;
34 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
35 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
36 import org.eclipse.jface.text.rules.Token;
37 import org.eclipse.jface.text.source.IAnnotationHover;
38 import org.eclipse.jface.text.source.ISourceViewer;
39 import org.eclipse.jface.text.source.SourceViewerConfiguration;
40 import org.eclipse.swt.graphics.RGB;
43 * Configuration for an <code>SourceViewer</code> which shows PHP code.
45 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
48 * Single token scanner.
50 static class SingleTokenScanner extends BufferedRuleBasedScanner {
51 public SingleTokenScanner(TextAttribute attribute) {
52 setDefaultReturnToken(new Token(attribute));
57 * Default constructor.
59 public PHPSourceViewerConfiguration() {
63 * Method declared on SourceViewerConfiguration
65 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
66 return new PHPAnnotationHover();
70 * Method declared on SourceViewerConfiguration
72 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
73 return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
77 * Method declared on SourceViewerConfiguration
79 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
80 return new String[] { IDocument.DEFAULT_CONTENT_TYPE, PHPPartitionScanner.PHP,
81 // PHPPartitionScanner.JAVA_DOC,
82 PHPPartitionScanner.HTML_MULTILINE_COMMENT };
86 * Method declared on SourceViewerConfiguration
88 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
90 ContentAssistant assistant = new ContentAssistant();
91 assistant.setContentAssistProcessor(new HTMLCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
92 assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.PHP);
93 //assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.HTML);
95 assistant.enableAutoActivation(true);
96 assistant.setAutoActivationDelay(500);
97 assistant.setProposalPopupOrientation(assistant.PROPOSAL_OVERLAY);
98 assistant.setContextInformationPopupOrientation(assistant.CONTEXT_INFO_ABOVE);
99 assistant.setContextInformationPopupBackground(PHPEditorEnvironment.getPHPColorProvider().getColor(new RGB(150, 150, 0)));
105 * Method declared on SourceViewerConfiguration
107 // public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
108 // return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
109 // // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
113 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
116 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
117 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
121 * Method declared on SourceViewerConfiguration
123 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
124 return new PHPDoubleClickSelector();
128 * Method declared on SourceViewerConfiguration
130 // public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
131 // return new String[] { "\t", " " }; //$NON-NLS-1$ //$NON-NLS-2$
135 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
137 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
139 Vector vector= new Vector();
141 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
143 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
145 int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
146 boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
148 for (int i= 0; i <= tabWidth; i++) {
149 StringBuffer prefix= new StringBuffer();
152 for (int j= 0; j + i < tabWidth; j++)
158 for (int j= 0; j < i; j++)
165 vector.add(prefix.toString());
168 vector.add(""); //$NON-NLS-1$
170 return (String[]) vector.toArray(new String[vector.size()]);
173 * Method declared on SourceViewerConfiguration
175 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
177 PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
178 PresentationReconciler reconciler = new PresentationReconciler();
180 DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
181 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
182 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
184 // dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(provider.HTML_DEFAULT))));
185 // reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
186 // reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
188 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
189 reconciler.setDamager(dr, PHPPartitionScanner.PHP);
190 reconciler.setRepairer(dr, PHPPartitionScanner.PHP);
192 // dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
193 // reconciler.setDamager(dr, PHPPartitionScanner.HTML);
194 // reconciler.setRepairer(dr, PHPPartitionScanner.HTML);
196 dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(provider.MULTI_LINE_COMMENT))));
197 reconciler.setDamager(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
198 reconciler.setRepairer(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
204 * Method declared on SourceViewerConfiguration
206 public int getTabWidth(ISourceViewer sourceViewer) {
211 * Method declared on SourceViewerConfiguration
213 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
214 return new PHPTextHover();