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.ContentAssistPreference;
17 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
18 import net.sourceforge.phpdt.internal.ui.text.java.JavaFormattingStrategy;
19 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCompletionProcessor;
20 import net.sourceforge.phpdt.ui.PreferenceConstants;
21 import net.sourceforge.phpdt.ui.text.JavaTextTools;
22 import net.sourceforge.phpeclipse.PHPCore;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import net.sourceforge.phpeclipse.phpeditor.html.HTMLFormattingStrategy;
25 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
26 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
27 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
28 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
29 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
30 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
32 import org.eclipse.core.resources.IFile;
33 import org.eclipse.jface.preference.IPreferenceStore;
34 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
35 import org.eclipse.jface.text.DefaultInformationControl;
36 import org.eclipse.jface.text.IAutoIndentStrategy;
37 import org.eclipse.jface.text.IDocument;
38 import org.eclipse.jface.text.IInformationControl;
39 import org.eclipse.jface.text.IInformationControlCreator;
40 import org.eclipse.jface.text.ITextDoubleClickStrategy;
41 import org.eclipse.jface.text.ITextHover;
42 import org.eclipse.jface.text.TextAttribute;
43 import org.eclipse.jface.text.contentassist.ContentAssistant;
44 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
45 import org.eclipse.jface.text.contentassist.IContentAssistant;
46 import org.eclipse.jface.text.formatter.ContentFormatter;
47 import org.eclipse.jface.text.formatter.IContentFormatter;
48 import org.eclipse.jface.text.formatter.IFormattingStrategy;
49 import org.eclipse.jface.text.presentation.IPresentationReconciler;
50 import org.eclipse.jface.text.presentation.PresentationReconciler;
51 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
52 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
53 import org.eclipse.jface.text.rules.DefaultPartitioner;
54 import org.eclipse.jface.text.rules.RuleBasedScanner;
55 import org.eclipse.jface.text.rules.Token;
56 import org.eclipse.jface.text.source.IAnnotationHover;
57 import org.eclipse.jface.text.source.ISourceViewer;
58 import org.eclipse.jface.text.source.SourceViewerConfiguration;
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.widgets.Shell;
61 import org.eclipse.ui.IFileEditorInput;
64 * Configuration for an <code>SourceViewer</code> which shows PHP code.
66 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
69 * Preference key used to look up display tab width.
73 public final static String PREFERENCE_TAB_WIDTH = PreferenceConstants.EDITOR_TAB_WIDTH;
75 * Preference key for inserting spaces rather than tabs.
79 public final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
81 // public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
82 //IDocument.DEFAULT_CONTENT_TYPE;
84 private JavaTextTools fJavaTextTools;
85 private PHPEditor fEditor;
87 private ContentFormatter fFormatter;
88 private HTMLFormattingStrategy fFormattingStrategy;
90 * Single token scanner.
92 static class SingleTokenScanner extends BufferedRuleBasedScanner {
93 public SingleTokenScanner(TextAttribute attribute) {
94 setDefaultReturnToken(new Token(attribute));
99 * Default constructor.
101 public PHPSourceViewerConfiguration(JavaTextTools textTools, PHPEditor editor) {
102 fJavaTextTools = textTools;
107 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
109 public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
110 // if (fFormatter == null) {
111 // fFormatter = new ContentFormatter();
112 // fFormattingStrategy = new HTMLFormattingStrategy(this, sourceViewer);
113 // fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
114 // fFormatter.enablePartitionAwareFormatting(false);
115 // fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
117 // return fFormatter;
119 if (fFormatter == null) {
121 fFormatter = new ContentFormatter();
122 IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
124 fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
125 fFormatter.enablePartitionAwareFormatting(false);
126 fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
132 * Returns the names of the document position categories used by the document
133 * partitioners created by this object to manage their partition information.
134 * If the partitioners don't use document position categories, the returned
135 * result is <code>null</code>.
137 * @return the partition managing position categories or <code>null</code>
140 public String[] getPartitionManagingPositionCategories() {
141 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
144 // * Returns the names of the document position categories used by the document
145 // * partitioners created by this object to manage their partition information.
146 // * If the partitioners don't use document position categories, the returned
147 // * result is <code>null</code>.
149 // * @return the partition managing position categories or <code>null</code>
150 // * if there is none
152 // private String[] getPartitionManagingPositionCategories() {
153 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
156 public PHPEditor getEditor() {
161 * Returns the preference store used by this configuration to initialize
162 * the individual bits and pieces.
164 * @return the preference store used to initialize this configuration
168 protected IPreferenceStore getPreferenceStore() {
169 return PHPeclipsePlugin.getDefault().getPreferenceStore();
173 * Method declared on SourceViewerConfiguration
175 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
176 return new PHPAnnotationHover();
180 * Method declared on SourceViewerConfiguration
182 public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
183 return (IPHPPartitionScannerConstants.PHP.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
187 * Returns the PHP source code scanner for this configuration.
189 * @return the PHP source code scanner
191 protected RuleBasedScanner getCodeScanner() {
192 return fJavaTextTools.getCodeScanner();
196 * Returns the HTML source code scanner for this configuration.
198 * @return the HTML source code scanner
200 protected RuleBasedScanner getHTMLScanner() {
201 return fJavaTextTools.getHTMLScanner();
205 * Returns the PHPDoc source code scanner for this configuration.
207 * @return the PHPDoc source code scanner
209 protected RuleBasedScanner getPHPDocScanner() {
210 return fJavaTextTools.getJavaDocScanner();
214 * Method declared on SourceViewerConfiguration
216 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
217 return new String[] {
218 IPHPPartitionScannerConstants.HTML,
219 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
220 IPHPPartitionScannerConstants.PHP,
221 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
222 IPHPPartitionScannerConstants.CSS,
223 IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
224 IPHPPartitionScannerConstants.JAVASCRIPT,
225 IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT,
226 IDocument.DEFAULT_CONTENT_TYPE };
230 * Method declared on SourceViewerConfiguration
232 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
234 ContentAssistant assistant = new ContentAssistant();
235 IContentAssistProcessor processor = new HTMLCompletionProcessor();
236 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML);
237 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
238 assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
239 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS);
240 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
241 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JAVASCRIPT);
242 assistant.setContentAssistProcessor(processor, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
244 assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IPHPPartitionScannerConstants.PHP);
246 assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
248 // assistant.enableAutoActivation(true);
249 // assistant.setAutoActivationDelay(500);
250 // assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
251 // ContentAssistPreference.configure(assistant, getPreferenceStore());
252 // assistant.setContextInformationPopupOrientation(
253 // ContentAssistant.CONTEXT_INFO_ABOVE);
254 // assistant.setContextInformationPopupBackground(
255 // PHPEditorEnvironment.getPHPColorProvider().getColor(
256 // new RGB(150, 150, 0)));
257 ContentAssistPreference.configure(assistant, getPreferenceStore());
259 assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
260 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
266 * Method declared on SourceViewerConfiguration
268 // public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
269 // return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
270 // // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
274 * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
277 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
278 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
282 * Method declared on SourceViewerConfiguration
284 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
285 return new PHPDoubleClickSelector();
289 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
291 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
293 Vector vector = new Vector();
295 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
297 final IPreferenceStore preferences = PHPeclipsePlugin.getDefault().getPreferenceStore();
298 int tabWidth = preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
299 boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
301 for (int i = 0; i <= tabWidth; i++) {
302 StringBuffer prefix = new StringBuffer();
305 for (int j = 0; j + i < tabWidth; j++)
311 for (int j = 0; j < i; j++)
318 vector.add(prefix.toString());
321 vector.add(""); //$NON-NLS-1$
323 return (String[]) vector.toArray(new String[vector.size()]);
326 * Method declared on SourceViewerConfiguration
328 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
329 // PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
330 // JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
331 PresentationReconciler reconciler = new PresentationReconciler();
333 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
334 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
335 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
337 dr = new DefaultDamagerRepairer(getHTMLScanner());
338 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
339 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
340 dr = new DefaultDamagerRepairer(getHTMLScanner());
341 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
342 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
343 dr = new DefaultDamagerRepairer(getHTMLScanner());
344 reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
345 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
346 dr = new DefaultDamagerRepairer(getHTMLScanner());
347 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
348 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
349 dr = new DefaultDamagerRepairer(getHTMLScanner());
350 reconciler.setDamager(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
351 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
353 new DefaultDamagerRepairer(
354 new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
355 reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
356 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
358 dr = new DefaultDamagerRepairer(getCodeScanner());
359 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
360 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
362 dr = new DefaultDamagerRepairer(getPHPDocScanner());
363 reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
364 reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
370 * Method declared on SourceViewerConfiguration
372 public int getTabWidth(ISourceViewer sourceViewer) {
373 return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
377 * Method declared on SourceViewerConfiguration
379 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
381 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
382 return new PHPTextHover(f.getProject());
383 } catch (NullPointerException e) {
384 // this exception occurs, if getTextHover is called by preference pages !
386 return new PHPTextHover(null);
390 * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
393 public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
394 return new IInformationControlCreator() {
395 public IInformationControl createInformationControl(Shell parent) {
396 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
397 // return new HoverBrowserControl(parent);