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;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
26 import org.eclipse.jface.text.IAutoIndentStrategy;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.ITextDoubleClickStrategy;
29 import org.eclipse.jface.text.ITextHover;
30 import org.eclipse.jface.text.TextAttribute;
31 import org.eclipse.jface.text.contentassist.ContentAssistant;
32 import org.eclipse.jface.text.contentassist.IContentAssistant;
33 import org.eclipse.jface.text.presentation.IPresentationReconciler;
34 import org.eclipse.jface.text.presentation.PresentationReconciler;
35 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
36 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
37 import org.eclipse.jface.text.rules.Token;
38 import org.eclipse.jface.text.source.IAnnotationHover;
39 import org.eclipse.jface.text.source.ISourceViewer;
40 import org.eclipse.jface.text.source.SourceViewerConfiguration;
41 import org.eclipse.swt.graphics.RGB;
44 * Configuration for an <code>SourceViewer</code> which shows PHP code.
46 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
49 * Single token scanner.
51 static class SingleTokenScanner extends BufferedRuleBasedScanner {
52 public SingleTokenScanner(TextAttribute attribute) {
53 setDefaultReturnToken(new Token(attribute));
58 * Default constructor.
60 public PHPSourceViewerConfiguration() {
64 * Method declared on SourceViewerConfiguration
66 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
67 return new PHPAnnotationHover();
71 * Method declared on SourceViewerConfiguration
73 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
74 return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
78 * Method declared on SourceViewerConfiguration
80 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
81 return new String[] { IDocument.DEFAULT_CONTENT_TYPE, PHPPartitionScanner.PHP,
82 // PHPPartitionScanner.JAVA_DOC,
83 PHPPartitionScanner.HTML_MULTILINE_COMMENT };
87 * Method declared on SourceViewerConfiguration
89 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
91 ContentAssistant assistant = new ContentAssistant();
92 assistant.setContentAssistProcessor(new HTMLCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
93 assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.PHP);
94 //assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.HTML);
96 assistant.enableAutoActivation(true);
97 assistant.setAutoActivationDelay(500);
98 assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
99 assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
100 assistant.setContextInformationPopupBackground(PHPEditorEnvironment.getPHPColorProvider().getColor(new RGB(150, 150, 0)));
106 * Method declared on SourceViewerConfiguration
108 // public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
109 // return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
110 // // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
114 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
117 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
118 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
122 * Method declared on SourceViewerConfiguration
124 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
125 return new PHPDoubleClickSelector();
129 * Method declared on SourceViewerConfiguration
131 // public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
132 // return new String[] { "\t", " " }; //$NON-NLS-1$ //$NON-NLS-2$
136 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
138 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
140 Vector vector= new Vector();
142 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
144 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
146 int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
147 boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
149 for (int i= 0; i <= tabWidth; i++) {
150 StringBuffer prefix= new StringBuffer();
153 for (int j= 0; j + i < tabWidth; j++)
159 for (int j= 0; j < i; j++)
166 vector.add(prefix.toString());
169 vector.add(""); //$NON-NLS-1$
171 return (String[]) vector.toArray(new String[vector.size()]);
174 * Method declared on SourceViewerConfiguration
176 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
178 PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
179 PresentationReconciler reconciler = new PresentationReconciler();
181 DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
182 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
183 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
185 // dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(provider.HTML_DEFAULT))));
186 // reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
187 // reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
189 dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
190 reconciler.setDamager(dr, PHPPartitionScanner.PHP);
191 reconciler.setRepairer(dr, PHPPartitionScanner.PHP);
193 // dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
194 // reconciler.setDamager(dr, PHPPartitionScanner.HTML);
195 // reconciler.setRepairer(dr, PHPPartitionScanner.HTML);
197 dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
198 reconciler.setDamager(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
199 reconciler.setRepairer(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
205 * Method declared on SourceViewerConfiguration
207 public int getTabWidth(ISourceViewer sourceViewer) {
212 * Method declared on SourceViewerConfiguration
214 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
215 return new PHPTextHover();