new PartitionScanner version
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / text / 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.phpdt.ui.text;
13
14 import java.util.Vector;
15
16 import net.sourceforge.phpdt.core.JavaCore;
17 import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference;
18 import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter;
19 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
20 import net.sourceforge.phpdt.internal.ui.text.JavaAnnotationHover;
21 import net.sourceforge.phpdt.internal.ui.text.JavaCompositeReconcilingStrategy;
22 import net.sourceforge.phpdt.internal.ui.text.JavaElementProvider;
23 import net.sourceforge.phpdt.internal.ui.text.JavaOutlineInformationControl;
24 import net.sourceforge.phpdt.internal.ui.text.JavaPresentationReconciler;
25 import net.sourceforge.phpdt.internal.ui.text.JavaReconciler;
26 import net.sourceforge.phpdt.internal.ui.text.PreferencesAdapter;
27 import net.sourceforge.phpdt.internal.ui.text.java.JavaFormattingStrategy;
28 import net.sourceforge.phpdt.internal.ui.text.java.JavaStringAutoIndentStrategy;
29 import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaEditorTextHoverDescriptor;
30 import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaEditorTextHoverProxy;
31 import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaInformationProvider;
32 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCompletionProcessor;
33 import net.sourceforge.phpdt.ui.PreferenceConstants;
34 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
35 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
36 import net.sourceforge.phpeclipse.phpeditor.html.HTMLFormattingStrategy;
37 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
38 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
39 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
40 import net.sourceforge.phpeclipse.phpeditor.php.PHPDocumentPartitioner;
41 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
42 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
43 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
44 import net.sourceforge.phpeclipse.xml.ui.XMLPlugin;
45 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLConfiguration;
46 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLPartitionScanner;
47 import net.sourceforge.phpeclipse.xml.ui.text.XMLTextTools;
48
49 import org.eclipse.core.runtime.NullProgressMonitor;
50 import org.eclipse.jface.preference.IPreferenceStore;
51 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
52 import org.eclipse.jface.text.DefaultInformationControl;
53 import org.eclipse.jface.text.IAutoIndentStrategy;
54 import org.eclipse.jface.text.IDocument;
55 import org.eclipse.jface.text.IInformationControl;
56 import org.eclipse.jface.text.IInformationControlCreator;
57 import org.eclipse.jface.text.ITextDoubleClickStrategy;
58 import org.eclipse.jface.text.ITextHover;
59 import org.eclipse.jface.text.ITextViewerExtension2;
60 import org.eclipse.jface.text.TextAttribute;
61 import org.eclipse.jface.text.contentassist.ContentAssistant;
62 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
63 import org.eclipse.jface.text.contentassist.IContentAssistant;
64 import org.eclipse.jface.text.formatter.ContentFormatter;
65 import org.eclipse.jface.text.formatter.IContentFormatter;
66 import org.eclipse.jface.text.formatter.IFormattingStrategy;
67 import org.eclipse.jface.text.information.IInformationPresenter;
68 import org.eclipse.jface.text.information.IInformationProvider;
69 import org.eclipse.jface.text.information.InformationPresenter;
70 import org.eclipse.jface.text.presentation.IPresentationDamager;
71 import org.eclipse.jface.text.presentation.IPresentationReconciler;
72 import org.eclipse.jface.text.presentation.IPresentationRepairer;
73 import org.eclipse.jface.text.presentation.PresentationReconciler;
74 import org.eclipse.jface.text.reconciler.IReconciler;
75 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
76 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
77 import org.eclipse.jface.text.rules.DefaultPartitioner;
78 import org.eclipse.jface.text.rules.RuleBasedScanner;
79 import org.eclipse.jface.text.rules.Token;
80 import org.eclipse.jface.text.source.IAnnotationHover;
81 import org.eclipse.jface.text.source.ISourceViewer;
82 import org.eclipse.jface.text.source.SourceViewerConfiguration;
83 import org.eclipse.jface.util.Assert;
84 import org.eclipse.jface.util.PropertyChangeEvent;
85 import org.eclipse.swt.SWT;
86 import org.eclipse.swt.widgets.Shell;
87 import org.eclipse.ui.editors.text.EditorsUI;
88 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
89 import org.eclipse.ui.texteditor.ITextEditor;
90
91 /**
92  * Configuration for an <code>SourceViewer</code> which shows PHP code.
93  */
94 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
95   /**
96    * Preference key used to look up display tab width.
97    * 
98    * @since 2.0
99    */
100   public final static String PREFERENCE_TAB_WIDTH = PreferenceConstants.EDITOR_TAB_WIDTH;
101
102   /**
103    * Preference key for inserting spaces rather than tabs.
104    * 
105    * @since 2.0
106    */
107   public final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
108
109   //  public static final String HTML_DEFAULT =
110   // IPHPPartitionScannerConstants.HTML;
111   //IDocument.DEFAULT_CONTENT_TYPE;
112   private JavaTextTools fJavaTextTools;
113
114   private ITextEditor fTextEditor;
115   /**
116          * The document partitioning.
117          * @since 3.0
118          */
119   private String fDocumentPartitioning;
120         
121   private ContentFormatter fFormatter;
122
123   private HTMLFormattingStrategy fFormattingStrategy;
124
125   /**
126    * Single token scanner.
127    */
128   static class SingleTokenScanner extends BufferedRuleBasedScanner {
129     public SingleTokenScanner(TextAttribute attribute) {
130       setDefaultReturnToken(new Token(attribute));
131     }
132   };
133
134   /**
135    * The document partitioning.
136    * 
137    * @since 3.0
138    */
139 //  private String fDocumentPartitioning;
140
141 //  /**
142 //   * The Java source code scanner
143 //   * 
144 //   * @since 3.0
145 //   */
146 //  private AbstractJavaScanner fCodeScanner;
147 //
148 //  /**
149 //   * The Java multi-line comment scanner
150 //   * 
151 //   * @since 3.0
152 //   */
153 //  private AbstractJavaScanner fMultilineCommentScanner;
154 //
155 //  /**
156 //   * The Java single-line comment scanner
157 //   * 
158 //   * @since 3.0
159 //   */
160 //  private AbstractJavaScanner fSinglelineCommentScanner;
161 //
162 //  /**
163 //   * The Java string scanner
164 //   * 
165 //   * @since 3.0
166 //   */
167 //  private AbstractJavaScanner fStringScanner;
168 //
169 //  /**
170 //   * The Javadoc scanner
171 //   * 
172 //   * @since 3.0
173 //   */
174 //  private AbstractJavaScanner fJavaDocScanner;
175
176   /**
177    * The preference store, can be read-only
178    * 
179    * @since 3.0
180    */
181   private IPreferenceStore fPreferenceStore;
182
183   /**
184    * The color manager
185    * 
186    * @since 3.0
187    */
188   private IColorManager fColorManager;
189
190   private XMLTextTools fXMLTextTools;
191   private XMLConfiguration xmlConfiguration;
192   /**
193    * Creates a new Java source viewer configuration for viewers in the given editor using the given preference store, the color
194    * manager and the specified document partitioning.
195    * <p>
196    * Creates a Java source viewer configuration in the new setup without text tools. Clients are allowed to call
197    * {@link JavaSourceViewerConfiguration#handlePropertyChangeEvent(PropertyChangeEvent)}and disallowed to call
198    * {@link JavaSourceViewerConfiguration#getPreferenceStore()}on the resulting Java source viewer configuration.
199    * </p>
200    * 
201    * @param colorManager
202    *          the color manager
203    * @param preferenceStore
204    *          the preference store, can be read-only
205    * @param editor
206    *          the editor in which the configured viewer(s) will reside
207    * @param partitioning
208    *          the document partitioning for this configuration
209    * @since 3.0
210    */
211   public PHPSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore preferenceStore, ITextEditor editor,
212       String partitioning) {
213     fColorManager = colorManager;
214     fPreferenceStore = preferenceStore;
215     fTextEditor = editor;
216     fDocumentPartitioning = partitioning;
217     fJavaTextTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
218     fXMLTextTools = XMLPlugin.getDefault().getXMLTextTools();
219     xmlConfiguration = new XMLConfiguration(fXMLTextTools);
220
221     //          initializeScanners(); 
222   }
223
224   /**
225    * Default constructor.
226    */
227   public PHPSourceViewerConfiguration(JavaTextTools tools, PHPEditor editor, String partitioning) {   
228         fJavaTextTools= tools;
229         fColorManager= tools.getColorManager();
230         fPreferenceStore= createPreferenceStore();
231         fDocumentPartitioning = partitioning;
232 //      fCodeScanner= (AbstractJavaScanner) fJavaTextTools.getCodeScanner();
233 //      fMultilineCommentScanner= (AbstractJavaScanner) fJavaTextTools.getMultilineCommentScanner();
234 //      fSinglelineCommentScanner= (AbstractJavaScanner) fJavaTextTools.getSinglelineCommentScanner();
235 //      fStringScanner= (AbstractJavaScanner) fJavaTextTools.getStringScanner();
236 //      fJavaDocScanner= (AbstractJavaScanner) fJavaTextTools.getJavaDocScanner();
237         fTextEditor= editor;
238     fXMLTextTools = XMLPlugin.getDefault().getXMLTextTools();
239     xmlConfiguration = new XMLConfiguration(fXMLTextTools);
240   }
241
242   /*
243    * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
244    */
245   public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
246     //    if (fFormatter == null) {
247     //      fFormatter = new ContentFormatter();
248     //      fFormattingStrategy = new HTMLFormattingStrategy(this,
249     // sourceViewer);
250     //      fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
251     //      fFormatter.enablePartitionAwareFormatting(false);
252     //      fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
253     //    }
254     //    return fFormatter;
255     if (fFormatter == null) {
256       //ContentFormatter
257       fFormatter = new ContentFormatter();
258       IFormattingStrategy strategy = new JavaFormattingStrategy(sourceViewer);
259       fFormatter.setFormattingStrategy(strategy, IDocument.DEFAULT_CONTENT_TYPE);
260       fFormatter.enablePartitionAwareFormatting(false);
261       fFormatter.setPartitionManagingPositionCategories(getPartitionManagingPositionCategories());
262     }
263     return fFormatter;
264   }
265
266   /**
267    * Returns the names of the document position categories used by the document partitioners created by this object to manage their
268    * partition information. If the partitioners don't use document position categories, the returned result is <code>null</code>.
269    * 
270    * @return the partition managing position categories or <code>null</code> if there is none
271    */
272   public String[] getPartitionManagingPositionCategories() {
273     return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
274   }
275
276   //  /**
277   //   * Returns the names of the document position categories used by the
278   // document
279   //   * partitioners created by this object to manage their partition
280   // information.
281   //   * If the partitioners don't use document position categories, the
282   // returned
283   //   * result is <code>null</code>.
284   //   *
285   //   * @return the partition managing position categories or
286   // <code>null</code>
287   //   * if there is none
288   //   */
289   //  private String[] getPartitionManagingPositionCategories() {
290   //    return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
291   //  }
292   public ITextEditor getEditor() {
293     return fTextEditor;
294   }
295
296   /**
297    * Returns the preference store used by this configuration to initialize the individual bits and pieces.
298    * 
299    * @return the preference store used to initialize this configuration
300    * 
301    * @since 2.0
302    */
303   protected IPreferenceStore getPreferenceStore() {
304     return PHPeclipsePlugin.getDefault().getPreferenceStore();
305   }
306
307   //  /* (non-Javadoc)
308   //   * Method declared on SourceViewerConfiguration
309   //   */
310   //  public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
311   //    return new PHPAnnotationHover();
312   //  }
313   /*
314    * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
315    */
316   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
317     return new JavaAnnotationHover(JavaAnnotationHover.VERTICAL_RULER_HOVER);
318   }
319
320   /*
321    * @see SourceViewerConfiguration#getOverviewRulerAnnotationHover(ISourceViewer)
322    * @since 3.0
323    */
324   public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
325     return new JavaAnnotationHover(JavaAnnotationHover.OVERVIEW_RULER_HOVER);
326   }
327
328   /*
329    * (non-Javadoc) Method declared on SourceViewerConfiguration
330    */
331   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
332     if (IPHPPartitions.PHP_STRING_DQ.equals(contentType))
333       return new JavaStringAutoIndentStrategy(getConfiguredDocumentPartitioning(sourceViewer));
334
335     return (IPHPPartitions.PHP_PARTITIONING.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
336   }
337
338   /**
339    * Returns the PHP source code scanner for this configuration.
340    * 
341    * @return the PHP source code scanner
342    */
343   protected RuleBasedScanner getCodeScanner() {
344     return fJavaTextTools.getCodeScanner();
345   }
346
347   /**
348    * Returns the HTML source code scanner for this configuration.
349    * 
350    * @return the HTML source code scanner
351    */
352 //  protected RuleBasedScanner getHTMLScanner() {
353 //    return fJavaTextTools.getHTMLScanner();
354 //  }
355
356   /**
357    * Returns the Smarty source code scanner for this configuration.
358    * 
359    * @return the Smarty source code scanner
360    */
361   protected RuleBasedScanner getSmartyScanner() {
362     return fJavaTextTools.getSmartyScanner();
363   }
364
365   /*
366    * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
367    */
368   /*
369    * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
370    */
371   public IReconciler getReconciler(ISourceViewer sourceViewer) {
372
373     final ITextEditor editor = getEditor();
374     if (editor != null && editor.isEditable()) {
375
376       JavaCompositeReconcilingStrategy strategy = new JavaCompositeReconcilingStrategy(editor,
377           getConfiguredDocumentPartitioning(sourceViewer));
378       JavaReconciler reconciler = new JavaReconciler(editor, strategy, false);
379       reconciler.setIsIncrementalReconciler(false);
380       reconciler.setProgressMonitor(new NullProgressMonitor());
381       reconciler.setDelay(500);
382
383       return reconciler;
384     }
385     return null;
386   }
387
388   //    public IReconciler getReconciler(ISourceViewer sourceViewer) {
389   //            if (getEditor() != null && getEditor().isEditable()) {
390   //                    JavaReconciler reconciler = new JavaReconciler(getEditor(),
391   //                                    new JavaReconcilingStrategy(getEditor()), false);
392   //                    reconciler.setProgressMonitor(new NullProgressMonitor());
393   //                    reconciler.setDelay(500);
394   //                    return reconciler;
395   //            }
396   //            return null;
397   //    }
398   /*
399    * @see SourceViewerConfiguration#getConfiguredTextHoverStateMasks(ISourceViewer, String)
400    * @since 2.1
401    */
402   public int[] getConfiguredTextHoverStateMasks(ISourceViewer sourceViewer, String contentType) {
403     JavaEditorTextHoverDescriptor[] hoverDescs = PHPeclipsePlugin.getDefault().getJavaEditorTextHoverDescriptors();
404     int stateMasks[] = new int[hoverDescs.length];
405     int stateMasksLength = 0;
406     for (int i = 0; i < hoverDescs.length; i++) {
407       if (hoverDescs[i].isEnabled()) {
408         int j = 0;
409         int stateMask = hoverDescs[i].getStateMask();
410         while (j < stateMasksLength) {
411           if (stateMasks[j] == stateMask)
412             break;
413           j++;
414         }
415         if (j == stateMasksLength)
416           stateMasks[stateMasksLength++] = stateMask;
417       }
418     }
419     if (stateMasksLength == hoverDescs.length)
420       return stateMasks;
421     int[] shortenedStateMasks = new int[stateMasksLength];
422     System.arraycopy(stateMasks, 0, shortenedStateMasks, 0, stateMasksLength);
423     return shortenedStateMasks;
424   }
425
426   /*
427    * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
428    * @since 2.1
429    */
430   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
431     JavaEditorTextHoverDescriptor[] hoverDescs = PHPeclipsePlugin.getDefault().getJavaEditorTextHoverDescriptors();
432     int i = 0;
433     while (i < hoverDescs.length) {
434       if (hoverDescs[i].isEnabled() && hoverDescs[i].getStateMask() == stateMask)
435         return new JavaEditorTextHoverProxy(hoverDescs[i], getEditor());
436       i++;
437     }
438     return null;
439     //          if (fEditor != null) {
440     //                  IEditorInput editorInput = fEditor.getEditorInput();
441     //                  if (editorInput instanceof IFileEditorInput) {
442     //                          try {
443     //                                  IFile f = ((IFileEditorInput) editorInput).getFile();
444     //                                  return new PHPTextHover(f.getProject());
445     //                          } catch (NullPointerException e) {
446     //                                  // this exception occurs, if getTextHover is called by
447     //                                  // preference pages !
448     //                          }
449     //                  }
450     //          }
451     //          return new PHPTextHover(null);
452   }
453
454   /*
455    * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String)
456    */
457   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
458     return getTextHover(sourceViewer, contentType, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
459   }
460
461   /**
462    * Returns the SmartyDoc source code scanner for this configuration.
463    * 
464    * @return the SmartyDoc source code scanner
465    */
466   protected RuleBasedScanner getSmartyDocScanner() {
467     return fJavaTextTools.getSmartyDocScanner();
468   }
469
470   /**
471    * Returns the PHPDoc source code scanner for this configuration.
472    * 
473    * @return the PHPDoc source code scanner
474    */
475   protected RuleBasedScanner getPHPDocScanner() {
476     return fJavaTextTools.getJavaDocScanner();
477   }
478
479   /*
480    * (non-Javadoc) Method declared on SourceViewerConfiguration
481    */
482   public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
483     return new String[] { 
484         IDocument.DEFAULT_CONTENT_TYPE, 
485         PHPPartitionScanner.PHP_SCRIPTING_AREA,
486         
487         IPHPPartitions.HTML, 
488         IPHPPartitions.HTML_MULTILINE_COMMENT,
489         IPHPPartitions.PHP_PARTITIONING, 
490         IPHPPartitions.PHP_SINGLELINE_COMMENT,
491         IPHPPartitions.PHP_MULTILINE_COMMENT,
492         IPHPPartitions.PHP_PHPDOC_COMMENT, 
493         IPHPPartitions.PHP_STRING_DQ,
494         IPHPPartitions.PHP_STRING_SQ,
495         IPHPPartitions.CSS,
496         IPHPPartitions.CSS_MULTILINE_COMMENT, 
497         IPHPPartitions.JAVASCRIPT, 
498         IPHPPartitions.JS_MULTILINE_COMMENT,
499         IPHPPartitions.SMARTY, 
500         IPHPPartitions.SMARTY_MULTILINE_COMMENT,
501         
502         XMLPartitionScanner.XML_PI,
503                 XMLPartitionScanner.XML_COMMENT,
504                 XMLPartitionScanner.XML_DECL,
505                 XMLPartitionScanner.XML_TAG,
506                 XMLPartitionScanner.XML_ATTRIBUTE,
507                 XMLPartitionScanner.XML_CDATA,
508                 
509                 XMLPartitionScanner.DTD_INTERNAL,
510                 XMLPartitionScanner.DTD_INTERNAL_PI,
511                 XMLPartitionScanner.DTD_INTERNAL_COMMENT,
512                 XMLPartitionScanner.DTD_INTERNAL_DECL,
513                 
514                 PHPDocumentPartitioner.PHP_TEMPLATE_DATA,
515                 PHPDocumentPartitioner.PHP_SCRIPT_CODE
516                 };
517   }
518
519   public String[] getConfiguredHTMLContentTypes() {
520     return new String[] {  
521         XMLPartitionScanner.XML_PI,
522                 XMLPartitionScanner.XML_COMMENT,
523                 XMLPartitionScanner.XML_DECL,
524                 XMLPartitionScanner.XML_TAG,
525                 XMLPartitionScanner.XML_ATTRIBUTE,
526                 XMLPartitionScanner.XML_CDATA,
527                 
528                 XMLPartitionScanner.DTD_INTERNAL,
529                 XMLPartitionScanner.DTD_INTERNAL_PI,
530                 XMLPartitionScanner.DTD_INTERNAL_COMMENT,
531                 XMLPartitionScanner.DTD_INTERNAL_DECL,
532         };
533   }
534   public String[] getConfiguredPHPContentTypes() {
535     return new String[] { 
536         IDocument.DEFAULT_CONTENT_TYPE, 
537         IPHPPartitions.PHP_PARTITIONING, 
538         IPHPPartitions.PHP_SINGLELINE_COMMENT,
539         IPHPPartitions.PHP_MULTILINE_COMMENT,
540         IPHPPartitions.PHP_PHPDOC_COMMENT, 
541         IPHPPartitions.PHP_STRING_DQ,
542         IPHPPartitions.PHP_STRING_SQ,
543         IPHPPartitions.CSS,
544         IPHPPartitions.CSS_MULTILINE_COMMENT, 
545         IPHPPartitions.JAVASCRIPT, 
546         IPHPPartitions.JS_MULTILINE_COMMENT,
547         IPHPPartitions.SMARTY, 
548         IPHPPartitions.SMARTY_MULTILINE_COMMENT,
549         };
550   }
551         /*
552          * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
553          * @since 3.0
554          */
555         public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
556                 if (fDocumentPartitioning != null)
557                         return fDocumentPartitioning;
558                 return super.getConfiguredDocumentPartitioning(sourceViewer);
559         }
560   /*
561    * (non-Javadoc) Method declared on SourceViewerConfiguration
562    */
563   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
564     ContentAssistant assistant = new ContentAssistant();
565     IContentAssistProcessor processor = new HTMLCompletionProcessor();
566     assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
567     assistant.setContentAssistProcessor(processor, IPHPPartitions.HTML);
568     assistant.setContentAssistProcessor(processor, IPHPPartitions.HTML_MULTILINE_COMMENT);
569     
570     assistant.setContentAssistProcessor(processor, IPHPPartitions.CSS);
571     assistant.setContentAssistProcessor(processor, IPHPPartitions.CSS_MULTILINE_COMMENT);
572     assistant.setContentAssistProcessor(processor, IPHPPartitions.JAVASCRIPT);
573     assistant.setContentAssistProcessor(processor, IPHPPartitions.JS_MULTILINE_COMMENT);
574     // TODO define special smarty partition content assist
575     assistant.setContentAssistProcessor(processor, IPHPPartitions.SMARTY);
576     assistant.setContentAssistProcessor(processor, IPHPPartitions.SMARTY_MULTILINE_COMMENT);
577     
578     assistant.setContentAssistProcessor(processor, PHPDocumentPartitioner.PHP_TEMPLATE_DATA);
579     String[] htmlTypes = getConfiguredHTMLContentTypes();
580     for (int i = 0; i < htmlTypes.length; i++) {
581       assistant.setContentAssistProcessor(processor, htmlTypes[i]); 
582     }
583     processor = new PHPCompletionProcessor(getEditor());
584     
585     assistant.setContentAssistProcessor(processor, PHPDocumentPartitioner.PHP_SCRIPT_CODE);
586     assistant.setContentAssistProcessor(processor, IPHPPartitions.PHP_PARTITIONING);
587     assistant.setContentAssistProcessor(processor, IPHPPartitions.PHP_STRING_DQ);
588     assistant.setContentAssistProcessor(processor, IPHPPartitions.PHP_STRING_SQ);
589     
590     assistant.setContentAssistProcessor(new PHPDocCompletionProcessor(), IPHPPartitions.PHP_PHPDOC_COMMENT);
591     //    assistant.enableAutoActivation(true);
592     //    assistant.setAutoActivationDelay(500);
593     //    assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
594     //    ContentAssistPreference.configure(assistant, getPreferenceStore());
595     //    assistant.setContextInformationPopupOrientation(
596     //      ContentAssistant.CONTEXT_INFO_ABOVE);
597     //    assistant.setContextInformationPopupBackground(
598     //      PHPEditorEnvironment.getPHPColorProvider().getColor(
599     //        new RGB(150, 150, 0)));
600     ContentAssistPreference.configure(assistant, getPreferenceStore());
601     assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
602     assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
603     return assistant;
604   } 
605
606   /*
607    * (non-Javadoc) Method declared on SourceViewerConfiguration
608    */
609   //  public String getDefaultPrefix(ISourceViewer sourceViewer, String
610   // contentType) {
611   //    return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null);
612   // //$NON-NLS-1$
613   //    // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" :
614   // null); //$NON-NLS-1$
615   //  }
616   /*
617    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
618    * @since 2.0
619    */
620   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
621     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
622   }
623
624   /*
625    * (non-Javadoc) Method declared on SourceViewerConfiguration
626    */
627   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
628     return new PHPDoubleClickSelector();
629   }
630
631   /*
632    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
633    */
634   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
635     Vector vector = new Vector();
636     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
637     final IPreferenceStore preferences = PHPeclipsePlugin.getDefault().getPreferenceStore();
638     int tabWidth = preferences.getInt(JavaCore.FORMATTER_TAB_SIZE);
639     boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
640     for (int i = 0; i <= tabWidth; i++) {
641       StringBuffer prefix = new StringBuffer();
642       if (useSpaces) {
643         for (int j = 0; j + i < tabWidth; j++)
644           prefix.append(' ');
645         if (i != 0)
646           prefix.append('\t');
647       } else {
648         for (int j = 0; j < i; j++)
649           prefix.append(' ');
650         if (i != tabWidth)
651           prefix.append('\t');
652       }
653       vector.add(prefix.toString());
654     }
655     vector.add(""); //$NON-NLS-1$
656     return (String[]) vector.toArray(new String[vector.size()]);
657   }
658   /**
659          * @return <code>true</code> iff the new setup without text tools is in use.
660          * 
661          * @since 3.0
662          */
663         private boolean isNewSetup() {
664                 return fJavaTextTools == null;
665         }
666   /**
667          * Creates and returns a preference store which combines the preference
668          * stores from the text tools and which is read-only.
669          *
670          * @return the read-only preference store 
671          * @since 3.0
672          */
673         private IPreferenceStore createPreferenceStore() {
674                 Assert.isTrue(!isNewSetup());
675                 IPreferenceStore generalTextStore= EditorsUI.getPreferenceStore();
676                 if (fJavaTextTools.getCorePreferenceStore() == null)
677                         return new ChainedPreferenceStore(new IPreferenceStore[] { fJavaTextTools.getPreferenceStore(), generalTextStore});
678                 
679                 return new ChainedPreferenceStore(new IPreferenceStore[] { fJavaTextTools.getPreferenceStore(), new PreferencesAdapter(fJavaTextTools.getCorePreferenceStore()), generalTextStore });
680         }
681   /*
682    * (non-Javadoc) Method declared on SourceViewerConfiguration
683    */
684   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
685     //  PHPColorProvider provider =
686     // PHPEditorEnvironment.getPHPColorProvider();
687     //    JavaColorManager provider =
688     // PHPEditorEnvironment.getPHPColorProvider();
689     PresentationReconciler phpReconciler = new JavaPresentationReconciler();
690     phpReconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
691
692 //    DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getHTMLScanner());
693 //    reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
694 //    reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
695 //    dr = new DefaultDamagerRepairer(getHTMLScanner());
696 //    reconciler.setDamager(dr, IPHPPartitions.HTML);
697 //    reconciler.setRepairer(dr, IPHPPartitions.HTML);
698 //    dr = new DefaultDamagerRepairer(getHTMLScanner());
699 //    reconciler.setDamager(dr, IPHPPartitions.CSS);
700 //    reconciler.setRepairer(dr, IPHPPartitions.CSS);
701 //    dr = new DefaultDamagerRepairer(getHTMLScanner());
702 //    reconciler.setDamager(dr, IPHPPartitions.CSS_MULTILINE_COMMENT);
703 //    reconciler.setRepairer(dr, IPHPPartitions.CSS_MULTILINE_COMMENT);
704 //    dr = new DefaultDamagerRepairer(getHTMLScanner());
705 //    reconciler.setDamager(dr, IPHPPartitions.JAVASCRIPT);
706 //    reconciler.setRepairer(dr, IPHPPartitions.JAVASCRIPT);
707 //    dr = new DefaultDamagerRepairer(getHTMLScanner());
708 //    reconciler.setDamager(dr, IPHPPartitions.JS_MULTILINE_COMMENT);
709 //    reconciler.setRepairer(dr, IPHPPartitions.JS_MULTILINE_COMMENT);
710     DefaultDamagerRepairer phpDR = new DefaultDamagerRepairer(getSmartyScanner());
711     phpReconciler.setDamager(phpDR, IPHPPartitions.SMARTY);
712     phpReconciler.setRepairer(phpDR, IPHPPartitions.SMARTY);
713     phpDR = new DefaultDamagerRepairer(getSmartyDocScanner());
714     phpReconciler.setDamager(phpDR, IPHPPartitions.SMARTY_MULTILINE_COMMENT);
715     phpReconciler.setRepairer(phpDR, IPHPPartitions.SMARTY_MULTILINE_COMMENT);
716 //    dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(
717 //        PHPColorProvider.MULTI_LINE_COMMENT))));
718 //    reconciler.setDamager(dr, IPHPPartitions.HTML_MULTILINE_COMMENT);
719 //    reconciler.setRepairer(dr, IPHPPartitions.HTML_MULTILINE_COMMENT);
720
721     phpDR = new DefaultDamagerRepairer(getCodeScanner());
722     phpReconciler.setDamager(phpDR, IDocument.DEFAULT_CONTENT_TYPE);
723     phpReconciler.setRepairer(phpDR, IDocument.DEFAULT_CONTENT_TYPE);
724     
725     phpDR = new DefaultDamagerRepairer(getCodeScanner());
726     phpReconciler.setDamager(phpDR, IPHPPartitions.PHP_PARTITIONING);
727     phpReconciler.setRepairer(phpDR, IPHPPartitions.PHP_PARTITIONING);
728
729     phpDR = new DefaultDamagerRepairer(getPHPDocScanner());
730     phpReconciler.setDamager(phpDR, IPHPPartitions.PHP_PHPDOC_COMMENT);
731     phpReconciler.setRepairer(phpDR, IPHPPartitions.PHP_PHPDOC_COMMENT);
732
733     phpDR = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(
734         PHPColorProvider.STRING))));
735     phpReconciler.setDamager(phpDR, IPHPPartitions.PHP_STRING_DQ);
736     phpReconciler.setRepairer(phpDR, IPHPPartitions.PHP_STRING_DQ);
737     phpDR = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(
738         PHPColorProvider.STRING))));
739     phpReconciler.setDamager(phpDR, IPHPPartitions.PHP_STRING_SQ);
740     phpReconciler.setRepairer(phpDR, IPHPPartitions.PHP_STRING_SQ);
741     phpDR = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(
742         PHPColorProvider.SINGLE_LINE_COMMENT))));
743     phpReconciler.setDamager(phpDR, IPHPPartitions.PHP_SINGLELINE_COMMENT);
744     phpReconciler.setRepairer(phpDR, IPHPPartitions.PHP_SINGLELINE_COMMENT);
745     phpDR = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(
746         PHPColorProvider.MULTI_LINE_COMMENT))));
747     phpReconciler.setDamager(phpDR, IPHPPartitions.PHP_MULTILINE_COMMENT);
748     phpReconciler.setRepairer(phpDR, IPHPPartitions.PHP_MULTILINE_COMMENT);
749     
750     PresentationReconciler reconciler = new PresentationReconciler();
751     reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
752 //
753     JavaTextTools jspTextTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
754     DefaultDamagerRepairer dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(
755         PHPColorProvider.PHPDOC_TAG))));//jspTextTools.getJSPTextScanner());
756         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
757         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
758
759 //      dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(fJavaTextTools.getColorManager().getColor(
760 //        PHPColorProvider.PHPDOC_TAG))));//jspTextTools.getJSPBracketScanner());
761 //      reconciler.setDamager(dr, JSPScriptScanner.JSP_BRACKET);
762 //      reconciler.setRepairer(dr, JSPScriptScanner.JSP_BRACKET);
763         
764     // xml partitions
765         configureEmbeddedPresentationReconciler(reconciler,
766             xmlConfiguration.getPresentationReconciler(sourceViewer),
767                 xmlConfiguration.getConfiguredContentTypes(sourceViewer),
768                 PHPDocumentPartitioner.PHP_TEMPLATE_DATA);
769
770         // java partitions
771         configureEmbeddedPresentationReconciler(reconciler,
772             phpReconciler,
773                 getConfiguredPHPContentTypes(),
774                 PHPDocumentPartitioner.PHP_SCRIPT_CODE);
775         
776     return reconciler;
777   }
778   
779   private void configureEmbeddedPresentationReconciler(
780                 PresentationReconciler reconciler,
781                 IPresentationReconciler embedded,
782                 String[] types, 
783                 String defaultType
784         ) {
785                 for (int i = 0; i < types.length; i++) {
786                         String type = types[i];
787
788                         IPresentationDamager damager = embedded.getDamager(type);
789                         IPresentationRepairer repairer = embedded.getRepairer(type);
790
791                         if (type == IDocument.DEFAULT_CONTENT_TYPE) {
792                                 type = defaultType;
793                         }
794
795                         reconciler.setDamager(damager, type);
796                         reconciler.setRepairer(repairer, type);
797                 }
798         }
799
800   /*
801    * (non-Javadoc) Method declared on SourceViewerConfiguration
802    */
803   public int getTabWidth(ISourceViewer sourceViewer) {
804     return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
805   }
806
807   /*
808    * (non-Javadoc) Method declared on SourceViewerConfiguration
809    */
810   //  public ITextHover getTextHover(ISourceViewer sourceViewer, String
811   // contentType) {
812   //    if (fEditor != null) {
813   //      IEditorInput editorInput = fEditor.getEditorInput();
814   //      if (editorInput instanceof IFileEditorInput) {
815   //        try {
816   //          IFile f = ((IFileEditorInput) editorInput).getFile();
817   //          return new PHPTextHover(f.getProject());
818   //        } catch (NullPointerException e) {
819   //          // this exception occurs, if getTextHover is called by preference pages
820   // !
821   //        }
822   //      }
823   //    }
824   //    return new PHPTextHover(null);
825   //  }
826   /*
827    * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
828    * @since 2.0
829    */
830   public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
831     return new IInformationControlCreator() {
832       public IInformationControl createInformationControl(Shell parent) {
833         return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
834         // return new HoverBrowserControl(parent);
835       }
836     };
837   }
838
839         /*
840          * @see SourceViewerConfiguration#getInformationPresenter(ISourceViewer)
841          * @since 2.0
842          */
843         public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) {
844                 InformationPresenter presenter= new InformationPresenter(getInformationPresenterControlCreator(sourceViewer));
845                 presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
846                 IInformationProvider provider= new JavaInformationProvider(getEditor());
847                 presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE);
848                 presenter.setInformationProvider(provider, IPHPPartitions.PHP_PHPDOC_COMMENT);
849 //              presenter.setInformationProvider(provider, IPHPPartitions.JAVA_CHARACTER);
850                 presenter.setSizeConstraints(60, 10, true, true);
851                 return presenter;
852         }
853   /*
854    * @see SourceViewerConfiguration#getInformationPresenter(ISourceViewer)
855    * @since 2.0
856    */
857   //    public IInformationPresenter getInformationPresenter(ISourceViewer
858   // sourceViewer) {
859   //            InformationPresenter presenter= new
860   // InformationPresenter(getInformationPresenterControlCreator(sourceViewer));
861   //            IInformationProvider provider= new JavaInformationProvider(getEditor());
862   //            presenter.setInformationProvider(provider,
863   // IDocument.DEFAULT_CONTENT_TYPE);
864   //            presenter.setInformationProvider(provider, IJavaPartitions.JAVA_DOC);
865   //            presenter.setSizeConstraints(60, 10, true, true);
866   //            return presenter;
867   //    }
868   /**
869    * Returns the information presenter control creator. The creator is a factory creating the presenter controls for the given
870    * source viewer. This implementation always returns a creator for <code>DefaultInformationControl</code> instances.
871    * 
872    * @param sourceViewer
873    *          the source viewer to be configured by this configuration
874    * @return an information control creator
875    * @since 2.1
876    */
877   private IInformationControlCreator getInformationPresenterControlCreator(ISourceViewer sourceViewer) {
878     return new IInformationControlCreator() {
879       public IInformationControl createInformationControl(Shell parent) {
880         int shellStyle = SWT.RESIZE;
881         int style = SWT.V_SCROLL | SWT.H_SCROLL;
882         return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false));
883         // return new HoverBrowserControl(parent);
884       }
885     };
886   }
887
888   /**
889    * Returns the outline presenter control creator. The creator is a factory creating outline presenter controls for the given
890    * source viewer. This implementation always returns a creator for <code>JavaOutlineInformationControl</code> instances.
891    * 
892    * @param sourceViewer
893    *          the source viewer to be configured by this configuration
894    * @return an information control creator
895    * @since 2.1
896    */
897   private IInformationControlCreator getOutlinePresenterControlCreator(ISourceViewer sourceViewer) {
898     return new IInformationControlCreator() {
899       public IInformationControl createInformationControl(Shell parent) {
900         int shellStyle = SWT.RESIZE;
901         int treeStyle = SWT.V_SCROLL | SWT.H_SCROLL;
902         return new JavaOutlineInformationControl(parent, shellStyle, treeStyle);
903       }
904     };
905   }
906
907   /**
908    * Returns the outline presenter which will determine and shown information requested for the current cursor position.
909    * 
910    * @param sourceViewer
911    *          the source viewer to be configured by this configuration
912    * @param doCodeResolve
913    *          a boolean which specifies whether code resolve should be used to compute the Java element
914    * @return an information presenter
915    * @since 2.1
916    */
917   public IInformationPresenter getOutlinePresenter(ISourceViewer sourceViewer, boolean doCodeResolve) {
918     InformationPresenter presenter = new InformationPresenter(getOutlinePresenterControlCreator(sourceViewer));
919     presenter.setAnchor(InformationPresenter.ANCHOR_GLOBAL);
920     IInformationProvider provider = new JavaElementProvider(getEditor(), doCodeResolve);
921     presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE);
922     presenter.setInformationProvider(provider, IPHPPartitions.PHP_PARTITIONING);
923     presenter.setInformationProvider(provider, IPHPPartitions.PHP_PHPDOC_COMMENT);
924     presenter.setInformationProvider(provider, IPHPPartitions.SMARTY_MULTILINE_COMMENT);
925     presenter.setInformationProvider(provider, IPHPPartitions.HTML);
926     presenter.setInformationProvider(provider, IPHPPartitions.HTML_MULTILINE_COMMENT);
927     presenter.setSizeConstraints(40, 20, true, false);
928     return presenter;
929   }
930 }