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
10 Klaus Hartlage - www.eclipseproject.de
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
45 * of 5 characters around its popup location.
47 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
49 protected int fInstallOffset;
52 * @see IContextInformationValidator#isContextInformationValid(int)
54 public boolean isContextInformationValid(int offset) {
55 return Math.abs(fInstallOffset - offset) < 5;
59 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
61 public void install(IContextInformation info, ITextViewer viewer, int offset) {
62 fInstallOffset = offset;
66 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
68 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
73 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
75 private final IContextInformation fContextInformation;
76 private int fPosition;
78 public ContextInformationWrapper(IContextInformation contextInformation) {
79 fContextInformation = contextInformation;
83 * @see IContextInformation#getContextDisplayString()
85 public String getContextDisplayString() {
86 return fContextInformation.getContextDisplayString();
90 * @see IContextInformation#getImage()
92 public Image getImage() {
93 return fContextInformation.getImage();
97 * @see IContextInformation#getInformationDisplayString()
99 public String getInformationDisplayString() {
100 return fContextInformation.getInformationDisplayString();
104 * @see IContextInformationExtension#getContextInformationPosition()
106 public int getContextInformationPosition() {
110 public void setContextInformationPosition(int position) {
111 fPosition = position;
115 protected IContextInformationValidator fValidator = new Validator();
116 private TemplateEngine fTemplateEngine;
117 private char[] fProposalAutoActivationSet;
118 private PHPCompletionProposalComparator fComparator;
119 private int fNumberOfComputedResults = 0;
121 private IEditorPart fEditor;
123 protected IWorkingCopyManager fManager;
125 public HTMLCompletionProcessor(IEditorPart editor) {
127 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
129 TemplateContextType contextType = PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType("html"); //$NON-NLS-1$
130 if (contextType != null)
131 fTemplateEngine = new TemplateEngine(contextType);
133 fComparator = new PHPCompletionProposalComparator();
137 * Method declared on IContentAssistProcessor
139 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
140 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
141 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
144 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
145 IDocument document = viewer.getDocument();
146 ICompilationUnit unit= fManager.getWorkingCopy(fEditor.getEditorInput());
148 if (fTemplateEngine != null) {
149 ICompletionProposal[] results;
151 fTemplateEngine.reset();
152 fTemplateEngine.complete(viewer, offset, unit);
153 // } catch (JavaModelException x) {
154 // Shell shell= viewer.getTextWidget().getShell();
155 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
158 IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
160 // concatenate arrays
161 IPHPCompletionProposal[] total;
162 total = new IPHPCompletionProposal[templateResults.length];
163 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
166 fNumberOfComputedResults = (results == null ? 0 : results.length);
168 * Order here and not in result collector to make sure that the order
169 * applies to all proposals and not just those of the compilation unit.
171 return order(results);
173 return new IPHPCompletionProposal[0];
176 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
177 int contextPosition = offset;
178 IDocument document = viewer.getDocument();
179 return contextPosition;
183 * Method declared on IContentAssistProcessor
185 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
186 // IContextInformation[] result = new IContextInformation[5];
187 // for (int i = 0; i < result.length; i++)
188 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
189 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
193 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
195 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
196 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
197 List result = addContextInformations(viewer, contextInformationPosition);
198 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
201 private List addContextInformations(ITextViewer viewer, int offset) {
202 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
204 List result = new ArrayList();
205 for (int i = 0; i < proposals.length; i++) {
206 IContextInformation contextInformation = proposals[i].getContextInformation();
207 if (contextInformation != null) {
208 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
209 wrapper.setContextInformationPosition(offset);
217 * Order the given proposals.
219 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
220 Arrays.sort(proposals, fComparator);
225 * Tells this processor to order the proposals alphabetically.
227 * @param order <code>true</code> if proposals should be ordered.
229 public void orderProposalsAlphabetically(boolean order) {
230 fComparator.setOrderAlphabetically(order);
234 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
236 public char[] getCompletionProposalAutoActivationCharacters() {
237 return fProposalAutoActivationSet;
241 * Sets this processor's set of characters triggering the activation of the
242 * completion proposal computation.
244 * @param activationSet the activation set
246 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
247 fProposalAutoActivationSet= activationSet;
251 * Method declared on IContentAssistProcessor
253 public char[] getContextInformationAutoActivationCharacters() {
259 * Method declared on IContentAssistProcessor
261 public IContextInformationValidator getContextInformationValidator() {
266 * Method declared on IContentAssistProcessor
268 public String getErrorMessage() {