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;
17 import java.util.SortedMap;
19 import net.sourceforge.phpdt.internal.corext.template.ContextType;
20 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
21 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
22 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
23 import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
24 import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine;
25 import net.sourceforge.phpdt.internal.ui.text.template.IdentifierEngine;
26 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
27 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
28 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
29 import net.sourceforge.phpeclipse.phpeditor.AbstractContentOutlinePage;
30 import net.sourceforge.phpeclipse.phpeditor.PHPContentOutlinePage;
31 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
33 import org.eclipse.core.resources.IFile;
34 import org.eclipse.core.resources.IProject;
35 import org.eclipse.jface.text.IDocument;
36 import org.eclipse.jface.text.ITextViewer;
37 import org.eclipse.jface.text.TextPresentation;
38 import org.eclipse.jface.text.contentassist.ICompletionProposal;
39 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
40 import org.eclipse.jface.text.contentassist.IContextInformation;
41 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
42 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
43 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
44 import org.eclipse.swt.graphics.Image;
45 import org.eclipse.ui.IEditorPart;
46 import org.eclipse.ui.IFileEditorInput;
49 * Example PHP completion processor.
51 public class PHPCompletionProcessor implements IContentAssistProcessor {
54 * Simple content assist tip closer. The tip is valid in a range
55 * of 5 characters around its popup location.
57 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
59 protected int fInstallOffset;
62 * @see IContextInformationValidator#isContextInformationValid(int)
64 public boolean isContextInformationValid(int offset) {
65 return Math.abs(fInstallOffset - offset) < 5;
69 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
71 public void install(IContextInformation info, ITextViewer viewer, int offset) {
72 fInstallOffset = offset;
76 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
78 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
83 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
85 private final IContextInformation fContextInformation;
86 private int fPosition;
88 public ContextInformationWrapper(IContextInformation contextInformation) {
89 fContextInformation = contextInformation;
93 * @see IContextInformation#getContextDisplayString()
95 public String getContextDisplayString() {
96 return fContextInformation.getContextDisplayString();
100 * @see IContextInformation#getImage()
102 public Image getImage() {
103 return fContextInformation.getImage();
107 * @see IContextInformation#getInformationDisplayString()
109 public String getInformationDisplayString() {
110 return fContextInformation.getInformationDisplayString();
114 * @see IContextInformationExtension#getContextInformationPosition()
116 public int getContextInformationPosition() {
120 public void setContextInformationPosition(int position) {
121 fPosition = position;
125 // public final class VariablesCompletionProposal implements IJavaCompletionProposal {
126 // private String fDisplayString;
127 // private String fReplacementString;
128 // private int fReplacementOffset;
129 // private int fReplacementLength;
130 // private int fCursorPosition;
131 // private Image fImage;
132 // private IContextInformation fContextInformation;
133 // private String fAdditionalProposalInfo;
136 // * Creates a new completion proposal based on the provided information. The replacement string is
137 // * considered being the display string too. All remaining fields are set to <code>null</code>.
139 // * @param replacementString the actual string to be inserted into the document
140 // * @param replacementOffset the offset of the text to be replaced
141 // * @param replacementLength the length of the text to be replaced
142 // * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
144 // public VariablesCompletionProposal(
145 // String replacementString,
146 // int replacementOffset,
147 // int replacementLength,
148 // int cursorPosition) {
149 // this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null);
153 // * Creates a new completion proposal. All fields are initialized based on the provided information.
155 // * @param replacementString the actual string to be inserted into the document
156 // * @param replacementOffset the offset of the text to be replaced
157 // * @param replacementLength the length of the text to be replaced
158 // * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
159 // * @param image the image to display for this proposal
160 // * @param displayString the string to be displayed for the proposal
161 // * @param contentInformation the context information associated with this proposal
162 // * @param additionalProposalInfo the additional information associated with this proposal
164 // public VariablesCompletionProposal(
165 // String replacementString,
166 // int replacementOffset,
167 // int replacementLength,
168 // int cursorPosition,
170 // String displayString,
171 // IContextInformation contextInformation,
172 // String additionalProposalInfo) {
173 // // Assert.isNotNull(replacementString);
174 // // Assert.isTrue(replacementOffset >= 0);
175 // // Assert.isTrue(replacementLength >= 0);
176 // // Assert.isTrue(cursorPosition >= 0);
178 // fReplacementString = replacementString;
179 // fReplacementOffset = replacementOffset;
180 // fReplacementLength = replacementLength;
181 // fCursorPosition = cursorPosition;
183 // fDisplayString = displayString;
184 // fContextInformation = contextInformation;
185 // fAdditionalProposalInfo = additionalProposalInfo;
189 // * @see ICompletionProposal#apply
191 // public void apply(IDocument document) {
193 // document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
194 // } catch (BadLocationException x) {
200 // * @see ICompletionProposal#getSelection
202 // public Point getSelection(IDocument document) {
203 // return new Point(fReplacementOffset + fCursorPosition, 0);
207 // * @see ICompletionProposal#getContextInformation()
209 // public IContextInformation getContextInformation() {
210 // return fContextInformation;
214 // * @see ICompletionProposal#getImage()
216 // public Image getImage() {
221 // * @see ICompletionProposal#getDisplayString()
223 // public String getDisplayString() {
224 // if (fDisplayString != null)
225 // return fDisplayString;
226 // return fReplacementString;
230 // * @see ICompletionProposal#getAdditionalProposalInfo()
232 // public String getAdditionalProposalInfo() {
233 // return fAdditionalProposalInfo;
236 // * Returns the relevance of the proposal.
238 // public int getRelevance() {
243 protected final static String[] fgProposals = PHPFunctionNames.FUNCTION_NAMES;
245 private char[] fProposalAutoActivationSet;
246 protected IContextInformationValidator fValidator = new Validator();
247 private TemplateEngine fTemplateEngine;
248 private PHPCompletionProposalComparator fComparator;
249 private int fNumberOfComputedResults = 0;
251 public PHPCompletionProcessor() {
253 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
254 if (contextType != null)
255 fTemplateEngine = new TemplateEngine(contextType);
257 fComparator = new PHPCompletionProposalComparator();
261 * Tells this processor to order the proposals alphabetically.
263 * @param order <code>true</code> if proposals should be ordered.
265 public void orderProposalsAlphabetically(boolean order) {
266 fComparator.setOrderAlphabetically(order);
270 * Sets this processor's set of characters triggering the activation of the
271 * completion proposal computation.
273 * @param activationSet the activation set
275 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
276 fProposalAutoActivationSet = activationSet;
279 * Method declared on IContentAssistProcessor
281 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
282 // IDocument document = viewer.getDocument();
283 // if (documentOffset > 0) {
285 // ICompletionProposal[] result;
286 // char character = document.getChar(documentOffset - 1);
287 // if (character == '$') {
288 ////viewer. .getActivePage().getActiveEditor();
289 // result = new ICompletionProposal[fgProposals.length];
290 // for (int i = 0; i < fgProposals.length; i++) {
291 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
292 // 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$
296 // } catch (BadLocationException e) {
297 // return new ICompletionProposal[0];
301 // ICompletionProposal[] result = new ICompletionProposal[fgProposals.length];
302 // for (int i = 0; i < fgProposals.length; i++) {
303 // IContextInformation info = new ContextInformation(fgProposals[i], MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { fgProposals[i] })); //$NON-NLS-1$
304 // 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$
307 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
308 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
312 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
313 IDocument document = viewer.getDocument();
314 Object[] identifiers = null;
315 IProject project = null;
318 PHPEditor editor = null;
319 AbstractContentOutlinePage outlinePage = null;
321 IEditorPart targetEditor = PHPeclipsePlugin.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
322 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
323 editor = (PHPEditor) targetEditor;
324 IFile f = ((IFileEditorInput) editor.getEditorInput()).getFile();
325 project = f.getProject();
326 outlinePage = editor.getfOutlinePage();
327 if (outlinePage instanceof PHPContentOutlinePage) {
328 identifiers = ((PHPContentOutlinePage) outlinePage).getVariables();
333 if (fTemplateEngine != null) {
334 ICompletionProposal[] results;
336 fTemplateEngine.reset();
337 fTemplateEngine.complete(viewer, offset); //, unit);
338 // } catch (JavaModelException x) {
339 // Shell shell= viewer.getTextWidget().getShell();
340 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
343 IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
345 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
346 if (identifiers != null) {
347 IdentifierEngine identifierEngine;
350 // for (int i = templateResults.length; i < templateResults.length + variables.length; i++) {
351 // proposal = (String) variables[j++];
352 // IContextInformation info = new ContextInformation(proposal, MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.Proposal.ContextInfo.pattern"), new Object[] { proposal })); //$NON-NLS-1$
353 // 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$
356 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
357 if (contextType != null) {
358 identifierEngine = new IdentifierEngine(contextType);
359 identifierEngine.complete(viewer, offset, identifiers);
360 identifierResults = identifierEngine.getResults();
364 IPHPCompletionProposal[] declarationResults = new IPHPCompletionProposal[0];
365 if (project != null) {
366 DeclarationEngine identifierEngine;
369 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
370 if (contextType != null) {
371 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(project);
372 SortedMap sortedMap = indexManager.getIdentifierMap();
374 identifierEngine = new DeclarationEngine(contextType);
375 identifierEngine.complete(viewer, offset, sortedMap);
376 identifierResults = identifierEngine.getResults();
380 // built in function names from phpsyntax.xml
381 IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
382 if (PHPFunctionNames.FUNCTION_NAMES != null) {
383 BuiltInEngine builtinEngine;
386 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
387 if (contextType != null) {
388 builtinEngine = new BuiltInEngine(contextType);
389 // TODO PHPFunctionNames.FUNCTION_NAMES should be taken from phpsyntax.xml
390 builtinEngine.complete(viewer, offset, PHPFunctionNames.FUNCTION_NAMES);
391 builtinResults = builtinEngine.getResults();
395 // concatenate arrays
396 IPHPCompletionProposal[] total;
398 new IPHPCompletionProposal[templateResults.length
399 + identifierResults.length
400 + builtinResults.length
401 + declarationResults.length];
402 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
403 System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
404 System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
409 templateResults.length + identifierResults.length + builtinResults.length,
410 declarationResults.length);
414 fNumberOfComputedResults = (results == null ? 0 : results.length);
416 * Order here and not in result collector to make sure that the order
417 * applies to all proposals and not just those of the compilation unit.
419 return order(results);
421 return new IPHPCompletionProposal[0];
424 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
425 int contextPosition = offset;
427 IDocument document = viewer.getDocument();
431 // PHPCodeReader reader= new PHPCodeReader();
432 // reader.configureBackwardReader(document, offset, true, true);
434 // int nestingLevel= 0;
436 // int curr= reader.read();
437 // while (curr != PHPCodeReader.EOF) {
439 // if (')' == (char) curr)
442 // else if ('(' == (char) curr) {
445 // if (nestingLevel < 0) {
446 // int start= reader.getOffset();
447 // if (looksLikeMethod(reader))
452 // curr= reader.read();
454 // } catch (IOException e) {
457 return contextPosition;
461 * Method declared on IContentAssistProcessor
463 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
464 // IContextInformation[] result = new IContextInformation[5];
465 // for (int i = 0; i < result.length; i++)
466 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
467 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
471 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
473 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
474 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
475 List result = addContextInformations(viewer, contextInformationPosition);
476 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
479 private List addContextInformations(ITextViewer viewer, int offset) {
480 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
482 List result = new ArrayList();
483 for (int i = 0; i < proposals.length; i++) {
484 IContextInformation contextInformation = proposals[i].getContextInformation();
485 if (contextInformation != null) {
486 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
487 wrapper.setContextInformationPosition(offset);
495 * Order the given proposals.
497 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
498 Arrays.sort(proposals, fComparator);
503 * Method declared on IContentAssistProcessor
505 public char[] getCompletionProposalAutoActivationCharacters() {
506 return fProposalAutoActivationSet;
507 // return null; // new char[] { '$' };
511 * Method declared on IContentAssistProcessor
513 public char[] getContextInformationAutoActivationCharacters() {
519 * Method declared on IContentAssistProcessor
521 public IContextInformationValidator getContextInformationValidator() {
526 * Method declared on IContentAssistProcessor
528 public String getErrorMessage() {