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.BuiltInEngine;
23 import net.sourceforge.phpdt.internal.ui.text.template.IdentifierEngine;
24 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
25 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
26 import net.sourceforge.phpeclipse.phpeditor.PHPContentOutlinePage;
27 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.ITextViewer;
30 import org.eclipse.jface.text.TextPresentation;
31 import org.eclipse.jface.text.contentassist.ICompletionProposal;
32 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
33 import org.eclipse.jface.text.contentassist.IContextInformation;
34 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
35 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
36 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.ui.IEditorPart;
41 * Example PHP completion processor.
43 public class HTMLCompletionProcessor implements IContentAssistProcessor {
46 * Simple content assist tip closer. The tip is valid in a range
47 * of 5 characters around its popup location.
49 protected static class Validator implements IContextInformationValidator, 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, ITextViewer, int)
63 public void install(IContextInformation info, ITextViewer viewer, int offset) {
64 fInstallOffset = offset;
68 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
70 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
75 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
77 private final IContextInformation fContextInformation;
78 private int fPosition;
80 public ContextInformationWrapper(IContextInformation contextInformation) {
81 fContextInformation = contextInformation;
85 * @see IContextInformation#getContextDisplayString()
87 public String getContextDisplayString() {
88 return fContextInformation.getContextDisplayString();
92 * @see IContextInformation#getImage()
94 public Image getImage() {
95 return fContextInformation.getImage();
99 * @see IContextInformation#getInformationDisplayString()
101 public String getInformationDisplayString() {
102 return fContextInformation.getInformationDisplayString();
106 * @see IContextInformationExtension#getContextInformationPosition()
108 public int getContextInformationPosition() {
112 public void setContextInformationPosition(int position) {
113 fPosition = position;
117 protected IContextInformationValidator fValidator = new Validator();
118 private TemplateEngine fTemplateEngine;
119 private PHPCompletionProposalComparator fComparator;
120 private int fNumberOfComputedResults = 0;
122 public HTMLCompletionProcessor() {
124 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("html"); //$NON-NLS-1$
125 if (contextType != null)
126 fTemplateEngine = new TemplateEngine(contextType);
128 fComparator = new PHPCompletionProposalComparator();
131 * Method declared on IContentAssistProcessor
133 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
134 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
135 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
138 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
139 IDocument document = viewer.getDocument();
141 if (fTemplateEngine != null) {
142 ICompletionProposal[] results;
144 fTemplateEngine.reset();
145 fTemplateEngine.complete(viewer, offset); //, unit);
146 // } catch (JavaModelException x) {
147 // Shell shell= viewer.getTextWidget().getShell();
148 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
151 IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
153 // concatenate arrays
154 IPHPCompletionProposal[] total;
155 total = new IPHPCompletionProposal[templateResults.length];
156 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
159 fNumberOfComputedResults = (results == null ? 0 : results.length);
161 * Order here and not in result collector to make sure that the order
162 * applies to all proposals and not just those of the compilation unit.
164 return order(results);
166 return new IPHPCompletionProposal[0];
169 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
170 int contextPosition = offset;
171 IDocument document = viewer.getDocument();
172 return contextPosition;
176 * Method declared on IContentAssistProcessor
178 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
179 // IContextInformation[] result = new IContextInformation[5];
180 // for (int i = 0; i < result.length; i++)
181 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
182 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
186 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
188 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
189 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
190 List result = addContextInformations(viewer, contextInformationPosition);
191 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
194 private List addContextInformations(ITextViewer viewer, int offset) {
195 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
197 List result = new ArrayList();
198 for (int i = 0; i < proposals.length; i++) {
199 IContextInformation contextInformation = proposals[i].getContextInformation();
200 if (contextInformation != null) {
201 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
202 wrapper.setContextInformationPosition(offset);
210 * Order the given proposals.
212 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
213 Arrays.sort(proposals, fComparator);
218 * Method declared on IContentAssistProcessor
220 public char[] getCompletionProposalAutoActivationCharacters() {
221 return new char[] { '<', '&', '#' };
225 * Method declared on IContentAssistProcessor
227 public char[] getContextInformationAutoActivationCharacters() {
233 * Method declared on IContentAssistProcessor
235 public IContextInformationValidator getContextInformationValidator() {
240 * Method declared on IContentAssistProcessor
242 public String getErrorMessage() {