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;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.TextPresentation;
28 import org.eclipse.jface.text.contentassist.ICompletionProposal;
29 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
30 import org.eclipse.jface.text.contentassist.IContextInformation;
31 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
32 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
33 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
34 import org.eclipse.jface.text.templates.TemplateContextType;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.ui.IEditorPart;
39 * HTML completion processor.
41 public class HTMLCompletionProcessor implements IContentAssistProcessor {
44 * Simple content assist tip closer. The tip is valid in a range of 5
45 * characters around its popup location.
47 protected static class Validator implements IContextInformationValidator,
48 IContextInformationPresenter {
50 protected int fInstallOffset;
53 * @see IContextInformationValidator#isContextInformationValid(int)
55 public boolean isContextInformationValid(int offset) {
56 return Math.abs(fInstallOffset - offset) < 5;
60 * @see IContextInformationValidator#install(IContextInformation,
63 public void install(IContextInformation info, ITextViewer viewer,
65 fInstallOffset = offset;
69 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int,
72 public boolean updatePresentation(int documentPosition,
73 TextPresentation presentation) {
78 private static class ContextInformationWrapper implements
79 IContextInformation, IContextInformationExtension {
81 private final IContextInformation fContextInformation;
83 private int fPosition;
85 public ContextInformationWrapper(IContextInformation contextInformation) {
86 fContextInformation = contextInformation;
90 * @see IContextInformation#getContextDisplayString()
92 public String getContextDisplayString() {
93 return fContextInformation.getContextDisplayString();
97 * @see IContextInformation#getImage()
99 public Image getImage() {
100 return fContextInformation.getImage();
104 * @see IContextInformation#getInformationDisplayString()
106 public String getInformationDisplayString() {
107 return fContextInformation.getInformationDisplayString();
111 * @see IContextInformationExtension#getContextInformationPosition()
113 public int getContextInformationPosition() {
117 public void setContextInformationPosition(int position) {
118 fPosition = position;
122 protected IContextInformationValidator fValidator = new Validator();
124 private TemplateEngine fTemplateEngine;
126 private char[] fProposalAutoActivationSet;
128 private PHPCompletionProposalComparator fComparator;
130 private int fNumberOfComputedResults = 0;
132 private IEditorPart fEditor;
134 protected IWorkingCopyManager fManager;
136 public HTMLCompletionProcessor(IEditorPart editor) {
138 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
140 TemplateContextType contextType = PHPeclipsePlugin.getDefault()
141 .getTemplateContextRegistry().getContextType("html"); //$NON-NLS-1$
142 if (contextType != null)
143 fTemplateEngine = new TemplateEngine(contextType);
145 fComparator = new PHPCompletionProposalComparator();
149 * (non-Javadoc) Method declared on IContentAssistProcessor
151 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
152 int documentOffset) {
153 int contextInformationPosition = guessContextInformationPosition(
154 viewer, documentOffset);
155 return internalComputeCompletionProposals(viewer, documentOffset,
156 contextInformationPosition);
159 private ICompletionProposal[] internalComputeCompletionProposals(
160 ITextViewer viewer, int offset, int contextOffset) {
161 IDocument document = viewer.getDocument();
162 ICompilationUnit unit = fManager.getWorkingCopy(fEditor
165 if (fTemplateEngine != null) {
166 ICompletionProposal[] results;
168 fTemplateEngine.reset();
169 fTemplateEngine.complete(viewer, offset, unit);
170 // } catch (JavaModelException x) {
171 // Shell shell= viewer.getTextWidget().getShell();
172 // ErrorDialog.openError(shell,
173 // JavaTextMessages.getString("CompletionProcessor.error.accessing.title"),
174 // JavaTextMessages.getString("CompletionProcessor.error.accessing.message"),
175 // x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
178 IPHPCompletionProposal[] templateResults = fTemplateEngine
181 // concatenate arrays
182 IPHPCompletionProposal[] total;
183 total = new IPHPCompletionProposal[templateResults.length];
184 System.arraycopy(templateResults, 0, total, 0,
185 templateResults.length);
188 fNumberOfComputedResults = (results == null ? 0 : results.length);
190 * Order here and not in result collector to make sure that the
191 * order applies to all proposals and not just those of the
194 return order(results);
196 return new IPHPCompletionProposal[0];
199 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
200 int contextPosition = offset;
201 IDocument document = viewer.getDocument();
202 return contextPosition;
206 * (non-Javadoc) Method declared on IContentAssistProcessor
208 // public IContextInformation[] computeContextInformation(ITextViewer
209 // viewer, int documentOffset) {
210 // IContextInformation[] result = new IContextInformation[5];
211 // for (int i = 0; i < result.length; i++)
213 // ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"),
214 // new Object[] { new Integer(i), new Integer(documentOffset)}),
216 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"),
217 // new Object[] { new Integer(i), new Integer(documentOffset - 5), new
218 // Integer(documentOffset + 5)})); //$NON-NLS-1$
222 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
224 public IContextInformation[] computeContextInformation(ITextViewer viewer,
226 int contextInformationPosition = guessContextInformationPosition(
228 List result = addContextInformations(viewer, contextInformationPosition);
229 return (IContextInformation[]) result
230 .toArray(new IContextInformation[result.size()]);
233 private List addContextInformations(ITextViewer viewer, int offset) {
234 ICompletionProposal[] proposals = internalComputeCompletionProposals(
237 List result = new ArrayList();
238 for (int i = 0; i < proposals.length; i++) {
239 IContextInformation contextInformation = proposals[i]
240 .getContextInformation();
241 if (contextInformation != null) {
242 ContextInformationWrapper wrapper = new ContextInformationWrapper(
244 wrapper.setContextInformationPosition(offset);
252 * Order the given proposals.
254 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
255 Arrays.sort(proposals, fComparator);
260 * Tells this processor to order the proposals alphabetically.
263 * <code>true</code> if proposals should be ordered.
265 public void orderProposalsAlphabetically(boolean order) {
266 fComparator.setOrderAlphabetically(order);
270 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
272 public char[] getCompletionProposalAutoActivationCharacters() {
273 return fProposalAutoActivationSet;
277 * Sets this processor's set of characters triggering the activation of the
278 * completion proposal computation.
280 * @param activationSet
283 public void setCompletionProposalAutoActivationCharacters(
284 char[] activationSet) {
285 fProposalAutoActivationSet = activationSet;
289 * (non-Javadoc) Method declared on IContentAssistProcessor
291 public char[] getContextInformationAutoActivationCharacters() {
292 return new char[] {};
296 * (non-Javadoc) Method declared on IContentAssistProcessor
298 public IContextInformationValidator getContextInformationValidator() {
303 * (non-Javadoc) Method declared on IContentAssistProcessor
305 public String getErrorMessage() {