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 protected IContextInformationValidator fValidator = new Validator();
239 private TemplateEngine fTemplateEngine;
240 private PHPCompletionProposalComparator fComparator;
241 private int fNumberOfComputedResults = 0;
243 public PHPCompletionProcessor() {
245 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
246 if (contextType != null)
247 fTemplateEngine = new TemplateEngine(contextType);
249 fComparator = new PHPCompletionProposalComparator();
252 * Method declared on IContentAssistProcessor
254 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
255 // IDocument document = viewer.getDocument();
256 // if (documentOffset > 0) {
258 // ICompletionProposal[] result;
259 // char character = document.getChar(documentOffset - 1);
260 // if (character == '$') {
261 ////viewer. .getActivePage().getActiveEditor();
262 // result = new ICompletionProposal[fgProposals.length];
263 // for (int i = 0; i < fgProposals.length; i++) {
264 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
265 // 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$
269 // } catch (BadLocationException e) {
270 // return new ICompletionProposal[0];
274 // ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
275 // for (int i = 0; i < fgProposals.length; i++) {
276 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
277 // 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 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
281 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
285 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
286 IDocument document = viewer.getDocument();
287 Object[] identifiers = null;
290 PHPEditor editor = null;
291 PHPContentOutlinePage outlinePage = null;
293 IEditorPart targetEditor = PHPeclipsePlugin.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
294 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
295 editor = (PHPEditor) targetEditor;
296 outlinePage = editor.getfOutlinePage();
297 identifiers = outlinePage.getVariables();
301 if (fTemplateEngine != null) {
302 ICompletionProposal[] results;
304 fTemplateEngine.reset();
305 fTemplateEngine.complete(viewer, offset); //, unit);
306 // } catch (JavaModelException x) {
307 // Shell shell= viewer.getTextWidget().getShell();
308 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
311 IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
313 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
314 if (identifiers != null) {
315 IdentifierEngine identifierEngine;
318 // for (int i = templateResults.length; i < templateResults.length + variables.length; i++) {
319 // proposal = (String) variables[j++];
320 // IContextInformation info = new ContextInformation(proposal, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { proposal })); //$NON-NLS-1$
321 // 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$
324 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
325 if (contextType != null) {
326 identifierEngine = new IdentifierEngine(contextType);
327 identifierEngine.complete(viewer, offset, identifiers);
328 identifierResults = identifierEngine.getResults();
332 IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
333 if (PHPFunctionNames.FUNCTION_NAMES != null) {
334 BuiltInEngine builtinEngine;
337 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
338 if (contextType != null) {
339 builtinEngine = new BuiltInEngine(contextType);
340 builtinEngine.complete(viewer, offset, PHPFunctionNames.FUNCTION_NAMES);
341 builtinResults = builtinEngine.getResults();
345 // concatenate arrays
346 IPHPCompletionProposal[] total;
347 total = new IPHPCompletionProposal[templateResults.length + identifierResults.length + builtinResults.length];
348 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
349 System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
350 System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
353 fNumberOfComputedResults = (results == null ? 0 : results.length);
355 * Order here and not in result collector to make sure that the order
356 * applies to all proposals and not just those of the compilation unit.
358 return order(results);
360 return new IPHPCompletionProposal[0];
363 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
364 int contextPosition = offset;
366 IDocument document = viewer.getDocument();
370 // PHPCodeReader reader= new PHPCodeReader();
371 // reader.configureBackwardReader(document, offset, true, true);
373 // int nestingLevel= 0;
375 // int curr= reader.read();
376 // while (curr != PHPCodeReader.EOF) {
378 // if (')' == (char) curr)
381 // else if ('(' == (char) curr) {
384 // if (nestingLevel < 0) {
385 // int start= reader.getOffset();
386 // if (looksLikeMethod(reader))
391 // curr= reader.read();
393 // } catch (IOException e) {
396 return contextPosition;
400 * Method declared on IContentAssistProcessor
402 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
403 // IContextInformation[] result = new IContextInformation[5];
404 // for (int i = 0; i < result.length; i++)
405 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
406 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
410 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
412 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
413 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
414 List result = addContextInformations(viewer, contextInformationPosition);
415 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
418 private List addContextInformations(ITextViewer viewer, int offset) {
419 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
421 List result = new ArrayList();
422 for (int i = 0; i < proposals.length; i++) {
423 IContextInformation contextInformation = proposals[i].getContextInformation();
424 if (contextInformation != null) {
425 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
426 wrapper.setContextInformationPosition(offset);
434 * Order the given proposals.
436 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
437 Arrays.sort(proposals, fComparator);
442 * Method declared on IContentAssistProcessor
444 public char[] getCompletionProposalAutoActivationCharacters() {
445 return null; // new char[] { '$' };
449 * Method declared on IContentAssistProcessor
451 public char[] getContextInformationAutoActivationCharacters() {
457 * Method declared on IContentAssistProcessor
459 public IContextInformationValidator getContextInformationValidator() {
464 * Method declared on IContentAssistProcessor
466 public String getErrorMessage() {