*** empty log message ***
[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
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;
42
43 /**
44  * Configuration for an <code>SourceViewer</code> which shows PHP code.
45  */
46 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
47
48   /**
49    * Single token scanner.
50    */
51   static class SingleTokenScanner extends BufferedRuleBasedScanner {
52     public SingleTokenScanner(TextAttribute attribute) {
53       setDefaultReturnToken(new Token(attribute));
54     }
55   };
56
57   /**
58    * Default constructor.
59    */
60   public PHPSourceViewerConfiguration() {
61   }
62
63   /* (non-Javadoc)
64    * Method declared on SourceViewerConfiguration
65    */
66   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
67     return new PHPAnnotationHover();
68   }
69
70   /* (non-Javadoc)
71    * Method declared on SourceViewerConfiguration
72    */
73   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
74     return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
75   }
76
77   /* (non-Javadoc)
78    * Method declared on SourceViewerConfiguration
79    */
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 };
84   }
85
86   /* (non-Javadoc)
87    * Method declared on SourceViewerConfiguration
88    */
89   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
90
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);
95
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)));
101
102     return assistant;
103   }
104
105   /* (non-Javadoc)
106    * Method declared on SourceViewerConfiguration
107    */
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$
111 //  }
112
113   /*
114    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
115    * @since 2.0
116    */
117   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
118     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
119   }
120   
121   /* (non-Javadoc)
122    * Method declared on SourceViewerConfiguration
123    */
124   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
125     return new PHPDoubleClickSelector();
126   }
127
128   /* (non-Javadoc)
129    * Method declared on SourceViewerConfiguration
130    */
131 //  public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
132 //    return new String[] { "\t", "    " }; //$NON-NLS-1$ //$NON-NLS-2$
133 //  }
134
135   /*
136    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
137    */
138   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
139
140     Vector vector= new Vector();
141
142     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
143         
144     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
145
146     int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
147     boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
148     
149     for (int i= 0; i <= tabWidth; i++) {
150         StringBuffer prefix= new StringBuffer();
151
152       if (useSpaces) {
153           for (int j= 0; j + i < tabWidth; j++)
154             prefix.append(' ');
155           
156         if (i != 0)
157             prefix.append('\t');        
158       } else {    
159           for (int j= 0; j < i; j++)
160             prefix.append(' ');
161           
162         if (i != tabWidth)
163             prefix.append('\t');
164       }
165       
166       vector.add(prefix.toString());
167     }
168
169     vector.add(""); //$NON-NLS-1$
170     
171     return (String[]) vector.toArray(new String[vector.size()]);
172   }
173   /* (non-Javadoc)
174    * Method declared on SourceViewerConfiguration
175    */
176   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
177
178     PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
179     PresentationReconciler reconciler = new PresentationReconciler();
180     
181     DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
182     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
183     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
184     
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);
188
189     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
190     reconciler.setDamager(dr, PHPPartitionScanner.PHP);
191     reconciler.setRepairer(dr, PHPPartitionScanner.PHP);
192
193 //    dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
194 //    reconciler.setDamager(dr, PHPPartitionScanner.HTML);
195 //    reconciler.setRepairer(dr, PHPPartitionScanner.HTML);
196
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);
200
201     return reconciler;
202   }
203
204   /* (non-Javadoc)
205    * Method declared on SourceViewerConfiguration
206    */
207   public int getTabWidth(ISourceViewer sourceViewer) {
208     return 4;
209   }
210
211   /* (non-Javadoc)
212    * Method declared on SourceViewerConfiguration
213    */
214   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
215     return new PHPTextHover();
216   }
217 }