af8b262578115045b21c16c8d82bc0c0e6ff5b52
[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.html.HTMLFormattingStrategy;
18 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
19 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
20 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
21 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
22 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
23 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
24
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
27 import org.eclipse.jface.text.IAutoIndentStrategy;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.ITextDoubleClickStrategy;
30 import org.eclipse.jface.text.ITextHover;
31 import org.eclipse.jface.text.TextAttribute;
32 import org.eclipse.jface.text.contentassist.ContentAssistant;
33 import org.eclipse.jface.text.contentassist.IContentAssistant;
34 import org.eclipse.jface.text.formatter.ContentFormatter;
35 import org.eclipse.jface.text.formatter.IContentFormatter;
36 import org.eclipse.jface.text.presentation.IPresentationReconciler;
37 import org.eclipse.jface.text.presentation.PresentationReconciler;
38 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
39 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
40 import org.eclipse.jface.text.rules.DefaultPartitioner;
41 import org.eclipse.jface.text.rules.Token;
42 import org.eclipse.jface.text.source.IAnnotationHover;
43 import org.eclipse.jface.text.source.ISourceViewer;
44 import org.eclipse.jface.text.source.SourceViewerConfiguration;
45 import org.eclipse.swt.graphics.RGB;
46
47 /**
48  * Configuration for an <code>SourceViewer</code> which shows PHP code.
49  */
50 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
51
52   public static final String HTML_DEFAULT = IDocument.DEFAULT_CONTENT_TYPE;
53   
54   private PHPEditor fEditor;
55   
56   private ContentFormatter fFormatter;
57   private HTMLFormattingStrategy fFormattingStrategy;
58   /**
59    * Single token scanner.
60    */
61   static class SingleTokenScanner extends BufferedRuleBasedScanner {
62     public SingleTokenScanner(TextAttribute attribute) {
63       setDefaultReturnToken(new Token(attribute));
64     }
65   };
66
67   /**
68    * Default constructor.
69    */
70   public PHPSourceViewerConfiguration(PHPEditor editor) {
71     fEditor = editor;
72   }
73
74   /*
75    * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
76    */
77   public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
78     if (fFormatter == null) {
79       fFormatter = new ContentFormatter();
80       fFormattingStrategy = new HTMLFormattingStrategy(this, sourceViewer);
81       fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
82       fFormatter.enablePartitionAwareFormatting(false);
83       fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
84     }
85     return fFormatter;
86   }
87   
88 //  /**
89 //   * Returns the names of the document position categories used by the document
90 //   * partitioners created by this object to manage their partition information.
91 //   * If the partitioners don't use document position categories, the returned
92 //   * result is <code>null</code>.
93 //   *
94 //   * @return the partition managing position categories or <code>null</code> 
95 //   *                  if there is none
96 //   */
97 //  private String[] getPartitionManagingPositionCategories() {
98 //    return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
99 //  } 
100   
101   public PHPEditor getEditor() {
102     return fEditor;
103   }
104   /* (non-Javadoc)
105    * Method declared on SourceViewerConfiguration
106    */
107   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
108     return new PHPAnnotationHover();
109   }
110
111   /* (non-Javadoc)
112    * Method declared on SourceViewerConfiguration
113    */
114   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
115     return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
116   }
117
118   /* (non-Javadoc)
119    * Method declared on SourceViewerConfiguration
120    */
121   public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
122     return new String[] { IDocument.DEFAULT_CONTENT_TYPE, PHPPartitionScanner.PHP,
123       //        PHPPartitionScanner.JAVA_DOC,
124       PHPPartitionScanner.HTML_MULTILINE_COMMENT };
125   }
126
127   /* (non-Javadoc)
128    * Method declared on SourceViewerConfiguration
129    */
130   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
131
132     ContentAssistant assistant = new ContentAssistant();
133     assistant.setContentAssistProcessor(new HTMLCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
134     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.PHP);
135     //assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.HTML);
136
137     assistant.enableAutoActivation(true);
138     assistant.setAutoActivationDelay(500);
139     assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
140     assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
141     assistant.setContextInformationPopupBackground(PHPEditorEnvironment.getPHPColorProvider().getColor(new RGB(150, 150, 0)));
142
143     return assistant;
144   }
145
146   /* (non-Javadoc)
147    * Method declared on SourceViewerConfiguration
148    */
149 //  public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
150 //    return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
151 //    // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
152 //  }
153
154   /*
155    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
156    * @since 2.0
157    */
158   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
159     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
160   }
161   
162   /* (non-Javadoc)
163    * Method declared on SourceViewerConfiguration
164    */
165   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
166     return new PHPDoubleClickSelector();
167   }
168
169   /* (non-Javadoc)
170    * Method declared on SourceViewerConfiguration
171    */
172 //  public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
173 //    return new String[] { "\t", "    " }; //$NON-NLS-1$ //$NON-NLS-2$
174 //  }
175
176   /*
177    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
178    */
179   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
180
181     Vector vector= new Vector();
182
183     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
184         
185     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
186
187     int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
188     boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
189     
190     for (int i= 0; i <= tabWidth; i++) {
191         StringBuffer prefix= new StringBuffer();
192
193       if (useSpaces) {
194           for (int j= 0; j + i < tabWidth; j++)
195             prefix.append(' ');
196           
197         if (i != 0)
198             prefix.append('\t');        
199       } else {    
200           for (int j= 0; j < i; j++)
201             prefix.append(' ');
202           
203         if (i != tabWidth)
204             prefix.append('\t');
205       }
206       
207       vector.add(prefix.toString());
208     }
209
210     vector.add(""); //$NON-NLS-1$
211     
212     return (String[]) vector.toArray(new String[vector.size()]);
213   }
214   /* (non-Javadoc)
215    * Method declared on SourceViewerConfiguration
216    */
217   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
218
219     PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
220     PresentationReconciler reconciler = new PresentationReconciler();
221     
222     DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
223     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
224     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
225     
226 //    dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(provider.HTML_DEFAULT))));
227 //    reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
228 //    reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
229
230     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
231     reconciler.setDamager(dr, PHPPartitionScanner.PHP);
232     reconciler.setRepairer(dr, PHPPartitionScanner.PHP);
233
234 //    dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
235 //    reconciler.setDamager(dr, PHPPartitionScanner.HTML);
236 //    reconciler.setRepairer(dr, PHPPartitionScanner.HTML);
237
238     dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(PHPColorProvider.MULTI_LINE_COMMENT))));
239     reconciler.setDamager(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
240     reconciler.setRepairer(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
241
242     return reconciler;
243   }
244
245   /* (non-Javadoc)
246    * Method declared on SourceViewerConfiguration
247    */
248   public int getTabWidth(ISourceViewer sourceViewer) {
249     return 4;
250   }
251
252   /* (non-Javadoc)
253    * Method declared on SourceViewerConfiguration
254    */
255   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
256     return new PHPTextHover();
257   }
258 }