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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
13 import java.util.Iterator;
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;
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;
40 public class TemplateEditorSourceViewerConfiguration extends
41 PHPSourceViewerConfiguration {
43 private static class TemplateVariableTextHover implements ITextHover {
45 private TemplateVariableProcessor fProcessor;
50 public TemplateVariableTextHover(TemplateVariableProcessor processor) {
51 fProcessor = processor;
57 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
58 * org.eclipse.jface.text.IRegion)
60 public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
62 IDocument doc = textViewer.getDocument();
63 int offset = subject.getOffset();
64 if (offset >= 2 && "${".equals(doc.get(offset - 2, 2))) { //$NON-NLS-1$
65 String varName = doc.get(offset, subject.getLength());
66 TemplateContextType contextType = fProcessor
68 if (contextType != null) {
69 Iterator iter = contextType.resolvers();
70 while (iter.hasNext()) {
71 TemplateVariableResolver var = (TemplateVariableResolver) iter
73 if (varName.equals(var.getType())) {
74 return var.getDescription();
79 } catch (BadLocationException e) {
87 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
90 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
91 if (textViewer != null) {
93 .findWord(textViewer.getDocument(), offset);
100 private final TemplateVariableProcessor fProcessor;
102 public TemplateEditorSourceViewerConfiguration(IColorManager colorManager,
103 IPreferenceStore store, ITextEditor editor,
104 TemplateVariableProcessor processor) {
105 super(colorManager, store, editor, IPHPPartitions.PHP_PARTITIONING);
106 fProcessor = processor;
110 * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
112 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
114 IPreferenceStore store = PHPeclipsePlugin.getDefault()
115 .getPreferenceStore();
116 JavaTextTools textTools = PHPeclipsePlugin.getDefault()
118 IColorManager manager = textTools.getColorManager();
120 ContentAssistant assistant = new ContentAssistant();
121 assistant.setContentAssistProcessor(fProcessor,
122 IDocument.DEFAULT_CONTENT_TYPE);
123 // Register the same processor for strings and single line comments to
124 // get code completion at the start of those partitions.
125 assistant.setContentAssistProcessor(fProcessor,
126 IPHPPartitions.PHP_STRING_DQ);
127 assistant.setContentAssistProcessor(fProcessor,
128 IPHPPartitions.PHP_STRING_SQ);
129 assistant.setContentAssistProcessor(fProcessor,
130 IPHPPartitions.PHP_STRING_HEREDOC);
131 assistant.setContentAssistProcessor(fProcessor,
132 IPHPPartitions.PHP_SINGLELINE_COMMENT);
133 assistant.setContentAssistProcessor(fProcessor,
134 IPHPPartitions.PHP_MULTILINE_COMMENT);
135 assistant.setContentAssistProcessor(fProcessor,
136 IPHPPartitions.PHP_PHPDOC_COMMENT);
138 assistant.enableAutoInsert(store
139 .getBoolean(PreferenceConstants.CODEASSIST_AUTOINSERT));
140 assistant.enableAutoActivation(store
141 .getBoolean(PreferenceConstants.CODEASSIST_AUTOACTIVATION));
142 assistant.setAutoActivationDelay(store
143 .getInt(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY));
145 .setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
147 .setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_ABOVE);
149 .setInformationControlCreator(getInformationControlCreator(sourceViewer));
151 Color background = getColor(store,
152 PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager);
153 assistant.setContextInformationPopupBackground(background);
154 assistant.setContextSelectorBackground(background);
155 assistant.setProposalSelectorBackground(background);
157 Color foreground = getColor(store,
158 PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager);
159 assistant.setContextInformationPopupForeground(foreground);
160 assistant.setContextSelectorForeground(foreground);
161 assistant.setProposalSelectorForeground(foreground);
166 private Color getColor(IPreferenceStore store, String key,
167 IColorManager manager) {
168 RGB rgb = PreferenceConverter.getColor(store, key);
169 return manager.getColor(rgb);
173 * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
176 public ITextHover getTextHover(ISourceViewer sourceViewer,
177 String contentType, int stateMask) {
178 return new TemplateVariableTextHover(fProcessor);