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;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.ITextViewer;
31 import org.eclipse.jface.text.TextPresentation;
32 import org.eclipse.jface.text.contentassist.ICompletionProposal;
33 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
34 import org.eclipse.jface.text.contentassist.IContextInformation;
35 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
36 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
37 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.ui.IEditorPart;
42 * Example PHP completion processor.
44 public class PHPCompletionProcessor implements IContentAssistProcessor {
47 * Simple content assist tip closer. The tip is valid in a range
48 * of 5 characters around its popup location.
50 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
52 protected int fInstallOffset;
55 * @see IContextInformationValidator#isContextInformationValid(int)
57 public boolean isContextInformationValid(int offset) {
58 return Math.abs(fInstallOffset - offset) < 5;
62 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
64 public void install(IContextInformation info, ITextViewer viewer, int offset) {
65 fInstallOffset = offset;
69 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
71 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
76 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
78 private final IContextInformation fContextInformation;
79 private int fPosition;
81 public ContextInformationWrapper(IContextInformation contextInformation) {
82 fContextInformation = contextInformation;
86 * @see IContextInformation#getContextDisplayString()
88 public String getContextDisplayString() {
89 return fContextInformation.getContextDisplayString();
93 * @see IContextInformation#getImage()
95 public Image getImage() {
96 return fContextInformation.getImage();
100 * @see IContextInformation#getInformationDisplayString()
102 public String getInformationDisplayString() {
103 return fContextInformation.getInformationDisplayString();
107 * @see IContextInformationExtension#getContextInformationPosition()
109 public int getContextInformationPosition() {
113 public void setContextInformationPosition(int position) {
114 fPosition = position;
118 // public final class VariablesCompletionProposal implements IJavaCompletionProposal {
119 // private String fDisplayString;
120 // private String fReplacementString;
121 // private int fReplacementOffset;
122 // private int fReplacementLength;
123 // private int fCursorPosition;
124 // private Image fImage;
125 // private IContextInformation fContextInformation;
126 // private String fAdditionalProposalInfo;
129 // * Creates a new completion proposal based on the provided information. The replacement string is
130 // * considered being the display string too. All remaining fields are set to <code>null</code>.
132 // * @param replacementString the actual string to be inserted into the document
133 // * @param replacementOffset the offset of the text to be replaced
134 // * @param replacementLength the length of the text to be replaced
135 // * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
137 // public VariablesCompletionProposal(
138 // String replacementString,
139 // int replacementOffset,
140 // int replacementLength,
141 // int cursorPosition) {
142 // this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null);
146 // * Creates a new completion proposal. All fields are initialized based on the provided information.
148 // * @param replacementString the actual string to be inserted into the document
149 // * @param replacementOffset the offset of the text to be replaced
150 // * @param replacementLength the length of the text to be replaced
151 // * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
152 // * @param image the image to display for this proposal
153 // * @param displayString the string to be displayed for the proposal
154 // * @param contentInformation the context information associated with this proposal
155 // * @param additionalProposalInfo the additional information associated with this proposal
157 // public VariablesCompletionProposal(
158 // String replacementString,
159 // int replacementOffset,
160 // int replacementLength,
161 // int cursorPosition,
163 // String displayString,
164 // IContextInformation contextInformation,
165 // String additionalProposalInfo) {
166 // // Assert.isNotNull(replacementString);
167 // // Assert.isTrue(replacementOffset >= 0);
168 // // Assert.isTrue(replacementLength >= 0);
169 // // Assert.isTrue(cursorPosition >= 0);
171 // fReplacementString = replacementString;
172 // fReplacementOffset = replacementOffset;
173 // fReplacementLength = replacementLength;
174 // fCursorPosition = cursorPosition;
176 // fDisplayString = displayString;
177 // fContextInformation = contextInformation;
178 // fAdditionalProposalInfo = additionalProposalInfo;
182 // * @see ICompletionProposal#apply
184 // public void apply(IDocument document) {
186 // document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
187 // } catch (BadLocationException x) {
193 // * @see ICompletionProposal#getSelection
195 // public Point getSelection(IDocument document) {
196 // return new Point(fReplacementOffset + fCursorPosition, 0);
200 // * @see ICompletionProposal#getContextInformation()
202 // public IContextInformation getContextInformation() {
203 // return fContextInformation;
207 // * @see ICompletionProposal#getImage()
209 // public Image getImage() {
214 // * @see ICompletionProposal#getDisplayString()
216 // public String getDisplayString() {
217 // if (fDisplayString != null)
218 // return fDisplayString;
219 // return fReplacementString;
223 // * @see ICompletionProposal#getAdditionalProposalInfo()
225 // public String getAdditionalProposalInfo() {
226 // return fAdditionalProposalInfo;
229 // * Returns the relevance of the proposal.
231 // public int getRelevance() {
236 protected final static String[] fgProposals = PHPFunctionNames.FUNCTION_NAMES;
238 private char[] fProposalAutoActivationSet;
239 protected IContextInformationValidator fValidator = new Validator();
240 private TemplateEngine fTemplateEngine;
241 private PHPCompletionProposalComparator fComparator;
242 private int fNumberOfComputedResults = 0;
244 public PHPCompletionProcessor() {
246 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
247 if (contextType != null)
248 fTemplateEngine = new TemplateEngine(contextType);
250 fComparator = new PHPCompletionProposalComparator();
254 * Sets this processor's set of characters triggering the activation of the
255 * completion proposal computation.
257 * @param activationSet the activation set
259 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
260 fProposalAutoActivationSet= activationSet;
263 * Method declared on IContentAssistProcessor
265 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
266 // IDocument document = viewer.getDocument();
267 // if (documentOffset > 0) {
269 // ICompletionProposal[] result;
270 // char character = document.getChar(documentOffset - 1);
271 // if (character == '$') {
272 ////viewer. .getActivePage().getActiveEditor();
273 // result = new ICompletionProposal[fgProposals.length];
274 // for (int i = 0; i < fgProposals.length; i++) {
275 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
276 // result[i] = new CompletionProposal(fgProposals[i], documentOffset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
280 // } catch (BadLocationException e) {
281 // return new ICompletionProposal[0];
285 // ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
286 // for (int i = 0; i < fgProposals.length; i++) {
287 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
288 // result[i] = new CompletionProposal(fgProposals[i], documentOffset, 0, fgProposals[i].length(), null, fgProposals[i], info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
291 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
292 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
296 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
297 IDocument document = viewer.getDocument();
298 Object[] identifiers = null;
301 PHPEditor editor = null;
302 PHPContentOutlinePage outlinePage = null;
304 IEditorPart targetEditor = PHPeclipsePlugin.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
305 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
306 editor = (PHPEditor) targetEditor;
307 outlinePage = editor.getfOutlinePage();
308 identifiers = outlinePage.getVariables();
312 if (fTemplateEngine != null) {
313 ICompletionProposal[] results;
315 fTemplateEngine.reset();
316 fTemplateEngine.complete(viewer, offset); //, unit);
317 // } catch (JavaModelException x) {
318 // Shell shell= viewer.getTextWidget().getShell();
319 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
322 IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
324 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
325 if (identifiers != null) {
326 IdentifierEngine identifierEngine;
329 // for (int i = templateResults.length; i < templateResults.length + variables.length; i++) {
330 // proposal = (String) variables[j++];
331 // IContextInformation info = new ContextInformation(proposal, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { proposal })); //$NON-NLS-1$
332 // results[i] = new VariablesCompletionProposal(proposal, offset, 0, proposal.length(), null, proposal, info, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.hoverinfo.pattern"), new Object[] { proposal })); //$NON-NLS-1$
335 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
336 if (contextType != null) {
337 identifierEngine = new IdentifierEngine(contextType);
338 identifierEngine.complete(viewer, offset, identifiers);
339 identifierResults = identifierEngine.getResults();
343 IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
344 if (PHPFunctionNames.FUNCTION_NAMES != null) {
345 BuiltInEngine builtinEngine;
348 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
349 if (contextType != null) {
350 builtinEngine = new BuiltInEngine(contextType);
351 builtinEngine.complete(viewer, offset, PHPFunctionNames.FUNCTION_NAMES);
352 builtinResults = builtinEngine.getResults();
356 // concatenate arrays
357 IPHPCompletionProposal[] total;
358 total = new IPHPCompletionProposal[templateResults.length + identifierResults.length + builtinResults.length];
359 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
360 System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
361 System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
364 fNumberOfComputedResults = (results == null ? 0 : results.length);
366 * Order here and not in result collector to make sure that the order
367 * applies to all proposals and not just those of the compilation unit.
369 return order(results);
371 return new IPHPCompletionProposal[0];
374 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
375 int contextPosition = offset;
377 IDocument document = viewer.getDocument();
381 // PHPCodeReader reader= new PHPCodeReader();
382 // reader.configureBackwardReader(document, offset, true, true);
384 // int nestingLevel= 0;
386 // int curr= reader.read();
387 // while (curr != PHPCodeReader.EOF) {
389 // if (')' == (char) curr)
392 // else if ('(' == (char) curr) {
395 // if (nestingLevel < 0) {
396 // int start= reader.getOffset();
397 // if (looksLikeMethod(reader))
402 // curr= reader.read();
404 // } catch (IOException e) {
407 return contextPosition;
411 * Method declared on IContentAssistProcessor
413 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
414 // IContextInformation[] result = new IContextInformation[5];
415 // for (int i = 0; i < result.length; i++)
416 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
417 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
421 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
423 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
424 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
425 List result = addContextInformations(viewer, contextInformationPosition);
426 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
429 private List addContextInformations(ITextViewer viewer, int offset) {
430 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
432 List result = new ArrayList();
433 for (int i = 0; i < proposals.length; i++) {
434 IContextInformation contextInformation = proposals[i].getContextInformation();
435 if (contextInformation != null) {
436 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
437 wrapper.setContextInformationPosition(offset);
445 * Order the given proposals.
447 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
448 Arrays.sort(proposals, fComparator);
453 * Method declared on IContentAssistProcessor
455 public char[] getCompletionProposalAutoActivationCharacters() {
456 return fProposalAutoActivationSet;
457 // return null; // new char[] { '$' };
461 * Method declared on IContentAssistProcessor
463 public char[] getContextInformationAutoActivationCharacters() {
469 * Method declared on IContentAssistProcessor
471 public IContextInformationValidator getContextInformationValidator() {
476 * Method declared on IContentAssistProcessor
478 public String getErrorMessage() {