abea8df38948aa22675e2c5c027d905bf001e60b
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPSourceViewerConfiguration.java
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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
13
14 import java.util.List;
15
16 import org.eclipse.swt.graphics.RGB;
17 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
18 import org.eclipse.jface.text.IAutoIndentStrategy;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.ITextDoubleClickStrategy;
21 import org.eclipse.jface.text.ITextHover;
22 import org.eclipse.jface.text.TextAttribute;
23 import org.eclipse.jface.text.contentassist.ContentAssistant;
24 import org.eclipse.jface.text.contentassist.IContentAssistant;
25 import org.eclipse.jface.text.presentation.IPresentationReconciler;
26 import org.eclipse.jface.text.presentation.PresentationReconciler;
27 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
28 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
29 import org.eclipse.jface.text.rules.IToken;
30 import org.eclipse.jface.text.rules.RuleBasedDamagerRepairer;
31 import org.eclipse.jface.text.rules.Token;
32 import org.eclipse.jface.text.source.IAnnotationHover;
33 import org.eclipse.jface.text.source.ISourceViewer;
34 import org.eclipse.jface.text.source.SourceViewerConfiguration;
35 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
36 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
37 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
38 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
39 //import net.sourceforge.phpeclipse.phpeditor.html.JavaDocCompletionProcessor;
40 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
41
42 /**
43  * Configuration for an <code>SourceViewer</code> which shows PHP code.
44  */
45 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
46
47   /**
48    * Single token scanner.
49    */
50   static class SingleTokenScanner extends BufferedRuleBasedScanner {
51     public SingleTokenScanner(TextAttribute attribute) {
52       setDefaultReturnToken(new Token(attribute));
53     }
54   };
55
56   /**
57    * Default constructor.
58    */
59   public PHPSourceViewerConfiguration() {
60   }
61
62   /* (non-Javadoc)
63    * Method declared on SourceViewerConfiguration
64    */
65   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
66     return new PHPAnnotationHover();
67   }
68
69   /* (non-Javadoc)
70    * Method declared on SourceViewerConfiguration
71    */
72   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
73     return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
74   }
75
76   /* (non-Javadoc)
77    * Method declared on SourceViewerConfiguration
78    */
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 };
83   }
84
85   /* (non-Javadoc)
86    * Method declared on SourceViewerConfiguration
87    */
88   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
89
90     ContentAssistant assistant = new ContentAssistant();
91     // assistant.setContentAssistProcessor(new PHPCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
92     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.PHP);
93
94     assistant.enableAutoActivation(true);
95     assistant.setAutoActivationDelay(500);
96     assistant.setProposalPopupOrientation(assistant.PROPOSAL_OVERLAY);
97     assistant.setContextInformationPopupOrientation(assistant.CONTEXT_INFO_ABOVE);
98     assistant.setContextInformationPopupBackground(PHPEditorEnvironment.getJavaColorProvider().getColor(new RGB(150, 150, 0)));
99
100     return assistant;
101   }
102
103   /* (non-Javadoc)
104    * Method declared on SourceViewerConfiguration
105    */
106   public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
107     return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
108   }
109
110   /* (non-Javadoc)
111    * Method declared on SourceViewerConfiguration
112    */
113   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
114     return new PHPDoubleClickSelector();
115   }
116
117   /* (non-Javadoc)
118    * Method declared on SourceViewerConfiguration
119    */
120   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
121     return new String[] { "\t", "    " }; //$NON-NLS-1$ //$NON-NLS-2$
122   }
123
124   /* (non-Javadoc)
125    * Method declared on SourceViewerConfiguration
126    */
127   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
128
129     PHPColorProvider provider = PHPEditorEnvironment.getJavaColorProvider();
130     PresentationReconciler reconciler = new PresentationReconciler();
131     
132     DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
133     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
134     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
135     
136 //    dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(provider.HTML_DEFAULT))));
137 //    reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
138 //    reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
139
140     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
141     reconciler.setDamager(dr, PHPPartitionScanner.PHP);
142     reconciler.setRepairer(dr, PHPPartitionScanner.PHP);
143
144 //    dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
145 //    reconciler.setDamager(dr, PHPPartitionScanner.HTML);
146 //    reconciler.setRepairer(dr, PHPPartitionScanner.HTML);
147
148     dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(provider.MULTI_LINE_COMMENT))));
149     reconciler.setDamager(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
150     reconciler.setRepairer(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
151
152     return reconciler;
153   }
154
155   /* (non-Javadoc)
156    * Method declared on SourceViewerConfiguration
157    */
158   public int getTabWidth(ISourceViewer sourceViewer) {
159     return 4;
160   }
161
162   /* (non-Javadoc)
163    * Method declared on SourceViewerConfiguration
164    */
165   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
166     return new PHPTextHover();
167   }
168 }