5c0e7b1868070c33cc4ef1de82824a6039aa59a6
[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.Vector;
15
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;
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 HTMLCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
92     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.PHP);
93     //assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.HTML);
94
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)));
100
101     return assistant;
102   }
103
104   /* (non-Javadoc)
105    * Method declared on SourceViewerConfiguration
106    */
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$
110 //  }
111
112   /*
113    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
114    * @since 2.0
115    */
116   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
117     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
118   }
119   
120   /* (non-Javadoc)
121    * Method declared on SourceViewerConfiguration
122    */
123   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
124     return new PHPDoubleClickSelector();
125   }
126
127   /* (non-Javadoc)
128    * Method declared on SourceViewerConfiguration
129    */
130 //  public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
131 //    return new String[] { "\t", "    " }; //$NON-NLS-1$ //$NON-NLS-2$
132 //  }
133
134   /*
135    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
136    */
137   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
138
139     Vector vector= new Vector();
140
141     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
142         
143     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
144
145     int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
146     boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
147     
148     for (int i= 0; i <= tabWidth; i++) {
149         StringBuffer prefix= new StringBuffer();
150
151       if (useSpaces) {
152           for (int j= 0; j + i < tabWidth; j++)
153             prefix.append(' ');
154           
155         if (i != 0)
156             prefix.append('\t');        
157       } else {    
158           for (int j= 0; j < i; j++)
159             prefix.append(' ');
160           
161         if (i != tabWidth)
162             prefix.append('\t');
163       }
164       
165       vector.add(prefix.toString());
166     }
167
168     vector.add(""); //$NON-NLS-1$
169     
170     return (String[]) vector.toArray(new String[vector.size()]);
171   }
172   /* (non-Javadoc)
173    * Method declared on SourceViewerConfiguration
174    */
175   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
176
177     PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
178     PresentationReconciler reconciler = new PresentationReconciler();
179     
180     DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
181     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
182     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
183     
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);
187
188     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
189     reconciler.setDamager(dr, PHPPartitionScanner.PHP);
190     reconciler.setRepairer(dr, PHPPartitionScanner.PHP);
191
192 //    dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
193 //    reconciler.setDamager(dr, PHPPartitionScanner.HTML);
194 //    reconciler.setRepairer(dr, PHPPartitionScanner.HTML);
195
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);
199
200     return reconciler;
201   }
202
203   /* (non-Javadoc)
204    * Method declared on SourceViewerConfiguration
205    */
206   public int getTabWidth(ISourceViewer sourceViewer) {
207     return 4;
208   }
209
210   /* (non-Javadoc)
211    * Method declared on SourceViewerConfiguration
212    */
213   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
214     return new PHPTextHover();
215   }
216 }