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
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
18 import net.sourceforge.phpdt.core.ICompilationUnit;
19 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
20 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
21 import net.sourceforge.phpdt.internal.ui.text.template.contentassist.TemplateEngine;
22 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
23 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import net.sourceforge.phpeclipse.ui.WebUI;
26 //import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.ITextViewer;
28 import org.eclipse.jface.text.TextPresentation;
29 import org.eclipse.jface.text.contentassist.ICompletionProposal;
30 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
31 import org.eclipse.jface.text.contentassist.IContextInformation;
32 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
33 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
34 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
35 import org.eclipse.jface.text.templates.TemplateContextType;
36 import org.eclipse.swt.graphics.Image;
37 import org.eclipse.ui.IEditorPart;
40 * HTML completion processor.
42 public class HTMLCompletionProcessor implements IContentAssistProcessor {
45 * Simple content assist tip closer. The tip is valid in a range of 5
46 * characters around its popup location.
48 protected static class Validator implements IContextInformationValidator,
49 IContextInformationPresenter {
51 protected int fInstallOffset;
54 * @see IContextInformationValidator#isContextInformationValid(int)
56 public boolean isContextInformationValid(int offset) {
57 return Math.abs(fInstallOffset - offset) < 5;
61 * @see IContextInformationValidator#install(IContextInformation,
64 public void install(IContextInformation info, ITextViewer viewer,
66 fInstallOffset = offset;
70 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int,
73 public boolean updatePresentation(int documentPosition,
74 TextPresentation presentation) {
79 private static class ContextInformationWrapper implements
80 IContextInformation, IContextInformationExtension {
82 private final IContextInformation fContextInformation;
84 private int fPosition;
86 public ContextInformationWrapper(IContextInformation contextInformation) {
87 fContextInformation = contextInformation;
91 * @see IContextInformation#getContextDisplayString()
93 public String getContextDisplayString() {
94 return fContextInformation.getContextDisplayString();
98 * @see IContextInformation#getImage()
100 public Image getImage() {
101 return fContextInformation.getImage();
105 * @see IContextInformation#getInformationDisplayString()
107 public String getInformationDisplayString() {
108 return fContextInformation.getInformationDisplayString();
112 * @see IContextInformationExtension#getContextInformationPosition()
114 public int getContextInformationPosition() {
118 public void setContextInformationPosition(int position) {
119 fPosition = position;
123 protected IContextInformationValidator fValidator = new Validator();
125 private TemplateEngine fTemplateEngine;
127 private char[] fProposalAutoActivationSet;
129 private PHPCompletionProposalComparator fComparator;
131 private int fNumberOfComputedResults = 0;
133 private IEditorPart fEditor;
135 protected IWorkingCopyManager fManager;
137 public HTMLCompletionProcessor(IEditorPart editor) {
139 fManager = WebUI.getDefault().getWorkingCopyManager();
141 TemplateContextType contextType = WebUI.getDefault()
142 .getTemplateContextRegistry().getContextType("html"); //$NON-NLS-1$
143 if (contextType != null)
144 fTemplateEngine = new TemplateEngine(contextType);
146 fComparator = new PHPCompletionProposalComparator();
150 * (non-Javadoc) Method declared on IContentAssistProcessor
152 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
153 int documentOffset) {
154 int contextInformationPosition = guessContextInformationPosition(
155 viewer, documentOffset);
156 return internalComputeCompletionProposals(viewer, documentOffset,
157 contextInformationPosition);
160 private ICompletionProposal[] internalComputeCompletionProposals(
161 ITextViewer viewer, int offset, int contextOffset) {
162 //IDocument document = viewer.getDocument();
163 ICompilationUnit unit = fManager.getWorkingCopy(fEditor
166 if (fTemplateEngine != null) {
167 ICompletionProposal[] results;
169 fTemplateEngine.reset();
170 fTemplateEngine.complete(viewer, offset, unit);
171 // } catch (JavaModelException x) {
172 // Shell shell= viewer.getTextWidget().getShell();
173 // ErrorDialog.openError(shell,
174 // JavaTextMessages.getString("CompletionProcessor.error.accessing.title"),
175 // JavaTextMessages.getString("CompletionProcessor.error.accessing.message"),
176 // x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
179 IPHPCompletionProposal[] templateResults = fTemplateEngine
182 // concatenate arrays
183 IPHPCompletionProposal[] total;
184 total = new IPHPCompletionProposal[templateResults.length];
185 System.arraycopy(templateResults, 0, total, 0,
186 templateResults.length);
189 fNumberOfComputedResults = (results == null ? 0 : results.length);
191 * Order here and not in result collector to make sure that the
192 * order applies to all proposals and not just those of the
195 return order(results);
197 return new IPHPCompletionProposal[0];
200 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
201 int contextPosition = offset;
202 //IDocument document = viewer.getDocument();
203 return contextPosition;
207 * (non-Javadoc) Method declared on IContentAssistProcessor
209 // public IContextInformation[] computeContextInformation(ITextViewer
210 // viewer, int documentOffset) {
211 // IContextInformation[] result = new IContextInformation[5];
212 // for (int i = 0; i < result.length; i++)
214 // ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"),
215 // new Object[] { new Integer(i), new Integer(documentOffset)}),
217 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"),
218 // new Object[] { new Integer(i), new Integer(documentOffset - 5), new
219 // Integer(documentOffset + 5)})); //$NON-NLS-1$
223 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
225 public IContextInformation[] computeContextInformation(ITextViewer viewer,
227 int contextInformationPosition = guessContextInformationPosition(
229 List result = addContextInformations(viewer, contextInformationPosition);
230 return (IContextInformation[]) result
231 .toArray(new IContextInformation[result.size()]);
234 private List addContextInformations(ITextViewer viewer, int offset) {
235 ICompletionProposal[] proposals = internalComputeCompletionProposals(
238 List result = new ArrayList();
239 for (int i = 0; i < proposals.length; i++) {
240 IContextInformation contextInformation = proposals[i]
241 .getContextInformation();
242 if (contextInformation != null) {
243 ContextInformationWrapper wrapper = new ContextInformationWrapper(
245 wrapper.setContextInformationPosition(offset);
253 * Order the given proposals.
255 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
256 Arrays.sort(proposals, fComparator);
261 * Tells this processor to order the proposals alphabetically.
264 * <code>true</code> if proposals should be ordered.
266 public void orderProposalsAlphabetically(boolean order) {
267 fComparator.setOrderAlphabetically(order);
271 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
273 public char[] getCompletionProposalAutoActivationCharacters() {
274 return fProposalAutoActivationSet;
278 * Sets this processor's set of characters triggering the activation of the
279 * completion proposal computation.
281 * @param activationSet
284 public void setCompletionProposalAutoActivationCharacters(
285 char[] activationSet) {
286 fProposalAutoActivationSet = activationSet;
290 * (non-Javadoc) Method declared on IContentAssistProcessor
292 public char[] getContextInformationAutoActivationCharacters() {
293 return new char[] {};
297 * (non-Javadoc) Method declared on IContentAssistProcessor
299 public IContextInformationValidator getContextInformationValidator() {
304 * (non-Javadoc) Method declared on IContentAssistProcessor
306 public String getErrorMessage() {