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
 
   9     IBM Corporation - Initial implementation
 
  10     Klaus Hartlage - www.eclipseproject.de
 
  11 **********************************************************************/
 
  12 package net.sourceforge.phpeclipse.phpeditor;
 
  14 import java.util.Vector;
 
  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;
 
  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;
 
  48  * Configuration for an <code>SourceViewer</code> which shows PHP code.
 
  50 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
 
  52   public static final String HTML_DEFAULT = IDocument.DEFAULT_CONTENT_TYPE;
 
  54   private PHPEditor fEditor;
 
  56   private ContentFormatter fFormatter;
 
  57   private HTMLFormattingStrategy fFormattingStrategy;
 
  59    * Single token scanner.
 
  61   static class SingleTokenScanner extends BufferedRuleBasedScanner {
 
  62     public SingleTokenScanner(TextAttribute attribute) {
 
  63       setDefaultReturnToken(new Token(attribute));
 
  68    * Default constructor.
 
  70   public PHPSourceViewerConfiguration(PHPEditor editor) {
 
  75    * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
 
  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));
 
  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>.
 
  94 //   * @return the partition managing position categories or <code>null</code> 
 
  97 //  private String[] getPartitionManagingPositionCategories() {
 
  98 //    return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
 
 101   public PHPEditor getEditor() {
 
 105    * Method declared on SourceViewerConfiguration
 
 107   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
 
 108     return new PHPAnnotationHover();
 
 112    * Method declared on SourceViewerConfiguration
 
 114   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
 
 115     return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
 
 119    * Method declared on SourceViewerConfiguration
 
 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 };
 
 128    * Method declared on SourceViewerConfiguration
 
 130   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
 
 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);
 
 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)));
 
 147    * Method declared on SourceViewerConfiguration
 
 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$
 
 155    * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
 
 158   public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
 
 159     return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
 
 163    * Method declared on SourceViewerConfiguration
 
 165   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
 
 166     return new PHPDoubleClickSelector();
 
 170    * Method declared on SourceViewerConfiguration
 
 172 //  public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
 
 173 //    return new String[] { "\t", "    " }; //$NON-NLS-1$ //$NON-NLS-2$
 
 177    * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
 
 179   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
 
 181     Vector vector= new Vector();
 
 183     // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
 
 185     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
 187     int tabWidth= store.getInt(PHPeclipsePlugin.FORMATTER_TAB_SIZE);
 
 188     boolean useSpaces= store.getBoolean(PHPeclipsePlugin.SPACES_FOR_TABS);
 
 190     for (int i= 0; i <= tabWidth; i++) {
 
 191         StringBuffer prefix= new StringBuffer();
 
 194           for (int j= 0; j + i < tabWidth; j++)
 
 200           for (int j= 0; j < i; j++)
 
 207       vector.add(prefix.toString());
 
 210     vector.add(""); //$NON-NLS-1$
 
 212     return (String[]) vector.toArray(new String[vector.size()]);
 
 215    * Method declared on SourceViewerConfiguration
 
 217   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
 
 219     PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
 
 220     PresentationReconciler reconciler = new PresentationReconciler();
 
 222     DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
 
 223     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
 
 224     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
 
 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);
 
 230     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
 
 231     reconciler.setDamager(dr, PHPPartitionScanner.PHP);
 
 232     reconciler.setRepairer(dr, PHPPartitionScanner.PHP);
 
 234 //    dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
 
 235 //    reconciler.setDamager(dr, PHPPartitionScanner.HTML);
 
 236 //    reconciler.setRepairer(dr, PHPPartitionScanner.HTML);
 
 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);
 
 246    * Method declared on SourceViewerConfiguration
 
 248   public int getTabWidth(ISourceViewer sourceViewer) {
 
 253    * Method declared on SourceViewerConfiguration
 
 255   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
 
 256     return new PHPTextHover();