fe08e43bcb56855d51c5306b059e777e2056f672
[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 net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
16 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
17 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
18 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
20 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
21 import org.eclipse.jface.text.IAutoIndentStrategy;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.ITextDoubleClickStrategy;
24 import org.eclipse.jface.text.ITextHover;
25 import org.eclipse.jface.text.TextAttribute;
26 import org.eclipse.jface.text.contentassist.ContentAssistant;
27 import org.eclipse.jface.text.contentassist.IContentAssistant;
28 import org.eclipse.jface.text.presentation.IPresentationReconciler;
29 import org.eclipse.jface.text.presentation.PresentationReconciler;
30 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
31 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
32 import org.eclipse.jface.text.rules.Token;
33 import org.eclipse.jface.text.source.IAnnotationHover;
34 import org.eclipse.jface.text.source.ISourceViewer;
35 import org.eclipse.jface.text.source.SourceViewerConfiguration;
36 import org.eclipse.swt.graphics.RGB;
37
38 /**
39  * Configuration for an <code>SourceViewer</code> which shows PHP code.
40  */
41 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration {
42
43   /**
44    * Single token scanner.
45    */
46   static class SingleTokenScanner extends BufferedRuleBasedScanner {
47     public SingleTokenScanner(TextAttribute attribute) {
48       setDefaultReturnToken(new Token(attribute));
49     }
50   };
51
52   /**
53    * Default constructor.
54    */
55   public PHPSourceViewerConfiguration() {
56   }
57
58   /* (non-Javadoc)
59    * Method declared on SourceViewerConfiguration
60    */
61   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
62     return new PHPAnnotationHover();
63   }
64
65   /* (non-Javadoc)
66    * Method declared on SourceViewerConfiguration
67    */
68   public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) {
69     return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new PHPAutoIndentStrategy() : new DefaultAutoIndentStrategy());
70   }
71
72   /* (non-Javadoc)
73    * Method declared on SourceViewerConfiguration
74    */
75   public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
76     return new String[] { IDocument.DEFAULT_CONTENT_TYPE, PHPPartitionScanner.PHP,
77       //        PHPPartitionScanner.JAVA_DOC,
78       PHPPartitionScanner.HTML_MULTILINE_COMMENT };
79   }
80
81   /* (non-Javadoc)
82    * Method declared on SourceViewerConfiguration
83    */
84   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
85
86     ContentAssistant assistant = new ContentAssistant();
87     assistant.setContentAssistProcessor(new HTMLCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
88     assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.PHP);
89     //assistant.setContentAssistProcessor(new PHPCompletionProcessor(), PHPPartitionScanner.HTML);
90
91     assistant.enableAutoActivation(true);
92     assistant.setAutoActivationDelay(500);
93     assistant.setProposalPopupOrientation(assistant.PROPOSAL_OVERLAY);
94     assistant.setContextInformationPopupOrientation(assistant.CONTEXT_INFO_ABOVE);
95     assistant.setContextInformationPopupBackground(PHPEditorEnvironment.getPHPColorProvider().getColor(new RGB(150, 150, 0)));
96
97     return assistant;
98   }
99
100   /* (non-Javadoc)
101    * Method declared on SourceViewerConfiguration
102    */
103   public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
104     return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
105     // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
106   }
107
108   /* (non-Javadoc)
109    * Method declared on SourceViewerConfiguration
110    */
111   public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
112     return new PHPDoubleClickSelector();
113   }
114
115   /* (non-Javadoc)
116    * Method declared on SourceViewerConfiguration
117    */
118   public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
119     return new String[] { "\t", "    " }; //$NON-NLS-1$ //$NON-NLS-2$
120   }
121
122   /* (non-Javadoc)
123    * Method declared on SourceViewerConfiguration
124    */
125   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
126
127     PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
128     PresentationReconciler reconciler = new PresentationReconciler();
129     
130     DefaultDamagerRepairer dr= new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
131     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
132     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
133     
134 //    dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(provider.HTML_DEFAULT))));
135 //    reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
136 //    reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
137
138     dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getPHPCodeScanner());
139     reconciler.setDamager(dr, PHPPartitionScanner.PHP);
140     reconciler.setRepairer(dr, PHPPartitionScanner.PHP);
141
142 //    dr = new DefaultDamagerRepairer(PHPEditorEnvironment.getHTMLCodeScanner());
143 //    reconciler.setDamager(dr, PHPPartitionScanner.HTML);
144 //    reconciler.setRepairer(dr, PHPPartitionScanner.HTML);
145
146     dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(provider.getColor(provider.MULTI_LINE_COMMENT))));
147     reconciler.setDamager(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
148     reconciler.setRepairer(dr, PHPPartitionScanner.HTML_MULTILINE_COMMENT);
149
150     return reconciler;
151   }
152
153   /* (non-Javadoc)
154    * Method declared on SourceViewerConfiguration
155    */
156   public int getTabWidth(ISourceViewer sourceViewer) {
157     return 4;
158   }
159
160   /* (non-Javadoc)
161    * Method declared on SourceViewerConfiguration
162    */
163   public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
164     return new PHPTextHover();
165   }
166 }