Syntax highlighting is changeable.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / TemplateEditorSourceViewerConfiguration.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation 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 API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
12
13 import java.util.Iterator;
14
15 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
16 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
17 import net.sourceforge.phpdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
18 import net.sourceforge.phpdt.ui.PreferenceConstants;
19 import net.sourceforge.phpdt.ui.text.IColorManager;
20 import net.sourceforge.phpdt.ui.text.JavaTextTools;
21 import net.sourceforge.phpdt.ui.text.PHPSourceViewerConfiguration;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.preference.PreferenceConverter;
26 import org.eclipse.jface.text.BadLocationException;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.IRegion;
29 import org.eclipse.jface.text.ITextHover;
30 import org.eclipse.jface.text.ITextViewer;
31 import org.eclipse.jface.text.contentassist.ContentAssistant;
32 import org.eclipse.jface.text.contentassist.IContentAssistant;
33 import org.eclipse.jface.text.source.ISourceViewer;
34 import org.eclipse.jface.text.templates.TemplateContextType;
35 import org.eclipse.jface.text.templates.TemplateVariableResolver;
36 import org.eclipse.swt.graphics.Color;
37 import org.eclipse.swt.graphics.RGB;
38 import org.eclipse.ui.texteditor.ITextEditor;
39
40
41 public class TemplateEditorSourceViewerConfiguration extends PHPSourceViewerConfiguration {
42
43         private static class TemplateVariableTextHover implements ITextHover {
44
45                 private TemplateVariableProcessor fProcessor;
46
47                 /**
48                  * @param type
49                  */
50                 public TemplateVariableTextHover(TemplateVariableProcessor processor) {
51                         fProcessor= processor;
52                 }
53
54                 /* (non-Javadoc)
55                  * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
56                  */
57                 public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
58                         try {
59                                 IDocument doc= textViewer.getDocument();
60                                 int offset= subject.getOffset();
61                                 if (offset >= 2 && "${".equals(doc.get(offset-2, 2))) { //$NON-NLS-1$
62                                         String varName= doc.get(offset, subject.getLength());
63                                         TemplateContextType contextType= fProcessor.getContextType();
64                                         if (contextType != null) {
65                                                 Iterator iter= contextType.resolvers();
66                                                 while (iter.hasNext()) {
67                                                         TemplateVariableResolver var= (TemplateVariableResolver) iter.next();
68                                                         if (varName.equals(var.getType())) {
69                                                                 return var.getDescription();
70                                                         }
71                                                 }
72                                         }
73                                 }                               
74                         } catch (BadLocationException e) {
75                         }
76                         return null;
77                 }
78
79                 /* (non-Javadoc)
80                  * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
81                  */
82                 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
83                         if (textViewer != null) {
84                                 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
85                         }
86                         return null;    
87                 }
88                 
89         } 
90         
91         private final TemplateVariableProcessor fProcessor;
92
93         public TemplateEditorSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore store, ITextEditor editor, TemplateVariableProcessor processor) {
94                 super(colorManager, store, editor, IPHPPartitions.PHP_PARTITIONING);
95                 fProcessor= processor;
96         }
97         
98         /*
99          * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
100          */
101         public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
102
103                 IPreferenceStore store= PHPeclipsePlugin.getDefault().getPreferenceStore();
104                 JavaTextTools textTools= PHPeclipsePlugin.getDefault().getJavaTextTools();
105                 IColorManager manager= textTools.getColorManager();                                     
106                 
107
108                 ContentAssistant assistant= new ContentAssistant();
109                 assistant.setContentAssistProcessor(fProcessor, IDocument.DEFAULT_CONTENT_TYPE);
110                         // Register the same processor for strings and single line comments to get code completion at the start of those partitions.
111                 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_STRING_DQ);
112                 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_STRING_SQ);
113                 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_SINGLELINE_COMMENT);
114                 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_MULTILINE_COMMENT);
115                 assistant.setContentAssistProcessor(fProcessor, IPHPPartitions.PHP_PHPDOC_COMMENT);
116
117                 assistant.enableAutoInsert(store.getBoolean(PreferenceConstants.CODEASSIST_AUTOINSERT));
118                 assistant.enableAutoActivation(store.getBoolean(PreferenceConstants.CODEASSIST_AUTOACTIVATION));
119                 assistant.setAutoActivationDelay(store.getInt(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY));
120                 assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
121                 assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
122                 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
123
124                 Color background= getColor(store, PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager);                        
125                 assistant.setContextInformationPopupBackground(background);
126                 assistant.setContextSelectorBackground(background);
127                 assistant.setProposalSelectorBackground(background);
128
129                 Color foreground= getColor(store, PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager);
130                 assistant.setContextInformationPopupForeground(foreground);
131                 assistant.setContextSelectorForeground(foreground);
132                 assistant.setProposalSelectorForeground(foreground);
133                 
134                 return assistant;
135         }       
136
137         private Color getColor(IPreferenceStore store, String key, IColorManager manager) {
138                 RGB rgb= PreferenceConverter.getColor(store, key);
139                 return manager.getColor(rgb);
140         }
141         
142         /*
143          * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
144          * @since 2.1
145          */
146         public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
147                 return new TemplateVariableTextHover(fProcessor);
148         }
149
150 }