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.internal.corext.template.ContextType;
19 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
20 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
21 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
22 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.TextPresentation;
27 import org.eclipse.jface.text.contentassist.ICompletionProposal;
28 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
29 import org.eclipse.jface.text.contentassist.IContextInformation;
30 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
31 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
32 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
33 import org.eclipse.swt.graphics.Image;
36 * Example PHP completion processor.
38 public class HTMLCompletionProcessor implements IContentAssistProcessor {
41 * Simple content assist tip closer. The tip is valid in a range
42 * of 5 characters around its popup location.
44 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
46 protected int fInstallOffset;
49 * @see IContextInformationValidator#isContextInformationValid(int)
51 public boolean isContextInformationValid(int offset) {
52 return Math.abs(fInstallOffset - offset) < 5;
56 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
58 public void install(IContextInformation info, ITextViewer viewer, int offset) {
59 fInstallOffset = offset;
63 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
65 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
70 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
72 private final IContextInformation fContextInformation;
73 private int fPosition;
75 public ContextInformationWrapper(IContextInformation contextInformation) {
76 fContextInformation = contextInformation;
80 * @see IContextInformation#getContextDisplayString()
82 public String getContextDisplayString() {
83 return fContextInformation.getContextDisplayString();
87 * @see IContextInformation#getImage()
89 public Image getImage() {
90 return fContextInformation.getImage();
94 * @see IContextInformation#getInformationDisplayString()
96 public String getInformationDisplayString() {
97 return fContextInformation.getInformationDisplayString();
101 * @see IContextInformationExtension#getContextInformationPosition()
103 public int getContextInformationPosition() {
107 public void setContextInformationPosition(int position) {
108 fPosition = position;
112 protected IContextInformationValidator fValidator = new Validator();
113 private TemplateEngine fTemplateEngine;
114 private PHPCompletionProposalComparator fComparator;
115 private int fNumberOfComputedResults = 0;
117 public HTMLCompletionProcessor() {
119 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("html"); //$NON-NLS-1$
120 if (contextType != null)
121 fTemplateEngine = new TemplateEngine(contextType);
123 fComparator = new PHPCompletionProposalComparator();
126 * Method declared on IContentAssistProcessor
128 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
129 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
130 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
133 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
134 IDocument document = viewer.getDocument();
136 if (fTemplateEngine != null) {
137 ICompletionProposal[] results;
139 fTemplateEngine.reset();
140 fTemplateEngine.complete(viewer, offset); //, unit);
141 // } catch (JavaModelException x) {
142 // Shell shell= viewer.getTextWidget().getShell();
143 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
146 IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
148 // concatenate arrays
149 IPHPCompletionProposal[] total;
150 total = new IPHPCompletionProposal[templateResults.length];
151 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
154 fNumberOfComputedResults = (results == null ? 0 : results.length);
156 * Order here and not in result collector to make sure that the order
157 * applies to all proposals and not just those of the compilation unit.
159 return order(results);
161 return new IPHPCompletionProposal[0];
164 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
165 int contextPosition = offset;
166 IDocument document = viewer.getDocument();
167 return contextPosition;
171 * Method declared on IContentAssistProcessor
173 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
174 // IContextInformation[] result = new IContextInformation[5];
175 // for (int i = 0; i < result.length; i++)
176 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
177 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
181 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
183 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
184 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
185 List result = addContextInformations(viewer, contextInformationPosition);
186 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
189 private List addContextInformations(ITextViewer viewer, int offset) {
190 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
192 List result = new ArrayList();
193 for (int i = 0; i < proposals.length; i++) {
194 IContextInformation contextInformation = proposals[i].getContextInformation();
195 if (contextInformation != null) {
196 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
197 wrapper.setContextInformationPosition(offset);
205 * Order the given proposals.
207 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
208 Arrays.sort(proposals, fComparator);
213 * Method declared on IContentAssistProcessor
215 public char[] getCompletionProposalAutoActivationCharacters() {
216 return new char[] { '<', '&', '#' };
220 * Method declared on IContentAssistProcessor
222 public char[] getContextInformationAutoActivationCharacters() {
228 * Method declared on IContentAssistProcessor
230 public IContextInformationValidator getContextInformationValidator() {
235 * Method declared on IContentAssistProcessor
237 public String getErrorMessage() {