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.core.ToolFactory;
20 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
21 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
22 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
23 import net.sourceforge.phpdt.internal.corext.template.ContextType;
24 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
25 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
26 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
27 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
28 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
29 import net.sourceforge.phpdt.internal.ui.text.template.BuiltInEngine;
30 import net.sourceforge.phpdt.internal.ui.text.template.DeclarationEngine;
31 import net.sourceforge.phpdt.internal.ui.text.template.IdentifierEngine;
32 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
33 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
34 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
35 import net.sourceforge.phpeclipse.phpeditor.AbstractContentOutlinePage;
36 import net.sourceforge.phpeclipse.phpeditor.PHPContentOutlinePage;
37 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
38 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
40 import org.eclipse.core.resources.IFile;
41 import org.eclipse.core.resources.IProject;
42 import org.eclipse.jface.text.BadLocationException;
43 import org.eclipse.jface.text.IDocument;
44 import org.eclipse.jface.text.ITextViewer;
45 import org.eclipse.jface.text.TextPresentation;
46 import org.eclipse.jface.text.contentassist.ICompletionProposal;
47 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
48 import org.eclipse.jface.text.contentassist.IContextInformation;
49 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
50 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
51 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
52 import org.eclipse.swt.graphics.Image;
53 import org.eclipse.ui.IEditorPart;
54 import org.eclipse.ui.IFileEditorInput;
57 * Example PHP completion processor.
59 public class PHPCompletionProcessor implements IContentAssistProcessor {
62 * Simple content assist tip closer. The tip is valid in a range
63 * of 5 characters around its popup location.
65 protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
67 protected int fInstallOffset;
70 * @see IContextInformationValidator#isContextInformationValid(int)
72 public boolean isContextInformationValid(int offset) {
73 return Math.abs(fInstallOffset - offset) < 5;
77 * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
79 public void install(IContextInformation info, ITextViewer viewer, int offset) {
80 fInstallOffset = offset;
84 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
86 public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
91 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
93 private final IContextInformation fContextInformation;
94 private int fPosition;
96 public ContextInformationWrapper(IContextInformation contextInformation) {
97 fContextInformation = contextInformation;
101 * @see IContextInformation#getContextDisplayString()
103 public String getContextDisplayString() {
104 return fContextInformation.getContextDisplayString();
108 * @see IContextInformation#getImage()
110 public Image getImage() {
111 return fContextInformation.getImage();
115 * @see IContextInformation#getInformationDisplayString()
117 public String getInformationDisplayString() {
118 return fContextInformation.getInformationDisplayString();
122 * @see IContextInformationExtension#getContextInformationPosition()
124 public int getContextInformationPosition() {
128 public void setContextInformationPosition(int position) {
129 fPosition = position;
133 private char[] fProposalAutoActivationSet;
134 protected IContextInformationValidator fValidator = new Validator();
135 private TemplateEngine fTemplateEngine;
136 private PHPCompletionProposalComparator fComparator;
137 private int fNumberOfComputedResults = 0;
139 public PHPCompletionProcessor() {
141 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
142 if (contextType != null)
143 fTemplateEngine = new TemplateEngine(contextType);
145 fComparator = new PHPCompletionProposalComparator();
149 * Tells this processor to order the proposals alphabetically.
151 * @param order <code>true</code> if proposals should be ordered.
153 public void orderProposalsAlphabetically(boolean order) {
154 fComparator.setOrderAlphabetically(order);
158 * Sets this processor's set of characters triggering the activation of the
159 * completion proposal computation.
161 * @param activationSet the activation set
163 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
164 fProposalAutoActivationSet = activationSet;
168 * Method declared on IContentAssistProcessor
170 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
171 int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
172 return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
175 private int getLastToken(ITextViewer viewer, int completionPosition, PHPUnitContext context) {
176 IDocument document = viewer.getDocument();
177 int start = context.getStart();
178 int end = context.getEnd();
181 int lastSignificantToken = ITerminalSymbols.TokenNameEOF;
184 // begin search 2 lines behind of this
189 ch = document.getChar(j);
195 ch = document.getChar(j);
202 // scan the line for the dereferencing operator '->'
203 startText = document.get(j, start - j);
204 // System.out.println(startText);
205 Scanner scanner = ToolFactory.createScanner(false, false, false);
206 scanner.setSource(startText.toCharArray());
207 scanner.setPHPMode(true);
208 int token = ITerminalSymbols.TokenNameEOF;
209 int beforeLastToken = ITerminalSymbols.TokenNameEOF;
210 int lastToken = ITerminalSymbols.TokenNameEOF;
213 token = scanner.getNextToken();
215 while (token != ITerminalSymbols.TokenNameERROR && token != ITerminalSymbols.TokenNameEOF) {
216 beforeLastToken = lastToken;
218 // System.out.println(scanner.toStringAction(lastToken));
219 token = scanner.getNextToken();
221 } catch (InvalidInputException e1) {
224 case ITerminalSymbols.TokenNameMINUS_GREATER :
225 // dereferencing operator '->' found
226 lastSignificantToken = ITerminalSymbols.TokenNameMINUS_GREATER;
227 if (beforeLastToken == ITerminalSymbols.TokenNamethis) {
228 lastSignificantToken = ITerminalSymbols.TokenNamethis;
231 case ITerminalSymbols.TokenNamenew :
232 lastSignificantToken = ITerminalSymbols.TokenNamenew;
236 } catch (BadLocationException e) {
238 return lastSignificantToken;
241 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
242 IDocument document = viewer.getDocument();
243 Object[] identifiers = null;
245 IProject project = null;
248 PHPEditor editor = null;
249 AbstractContentOutlinePage outlinePage = null;
251 IEditorPart targetEditor = PHPeclipsePlugin.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
252 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
253 editor = (PHPEditor) targetEditor;
254 file = ((IFileEditorInput) editor.getEditorInput()).getFile();
255 project = file.getProject();
256 outlinePage = editor.getfOutlinePage();
257 if (outlinePage instanceof PHPContentOutlinePage) {
258 identifiers = ((PHPContentOutlinePage) outlinePage).getVariables();
263 ContextType phpContextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
264 ((CompilationUnitContextType) phpContextType).setContextParameters(document, offset, 0);
266 PHPUnitContext context = (PHPUnitContext) phpContextType.createContext();
267 String prefix = context.getKey();
269 int lastSignificantToken = getLastToken(viewer, offset, context);
270 boolean useClassMembers =
271 (lastSignificantToken == ITerminalSymbols.TokenNameMINUS_GREATER) ||
272 (lastSignificantToken == ITerminalSymbols.TokenNamethis) ||
273 (lastSignificantToken == ITerminalSymbols.TokenNamenew);
274 boolean emptyPrefix = prefix == null || prefix.equals("");
276 if (fTemplateEngine != null) {
277 IPHPCompletionProposal[] templateResults = new IPHPCompletionProposal[0];
279 ICompletionProposal[] results;
281 fTemplateEngine.reset();
282 fTemplateEngine.complete(viewer, offset); //, unit);
283 templateResults = fTemplateEngine.getResults();
286 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
287 if ((!useClassMembers) && identifiers != null) {
288 IdentifierEngine identifierEngine;
290 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
291 if (contextType != null) {
292 identifierEngine = new IdentifierEngine(contextType);
293 identifierEngine.complete(viewer, offset, identifiers);
294 identifierResults = identifierEngine.getResults();
298 // declarations stored in file project.index on project level
299 IPHPCompletionProposal[] declarationResults = new IPHPCompletionProposal[0];
300 if (project != null) {
301 DeclarationEngine declarationEngine;
303 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
304 if (contextType != null) {
305 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(project);
306 SortedMap sortedMap = indexManager.getIdentifierMap();
308 declarationEngine = new DeclarationEngine(contextType, lastSignificantToken, file);
309 declarationEngine.complete(viewer, offset, sortedMap);
310 declarationResults = declarationEngine.getResults();
314 // built in function names from phpsyntax.xml
315 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
316 IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
317 if ((!useClassMembers) && syntaxbuffer != null) {
318 BuiltInEngine builtinEngine;
321 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
322 if (contextType != null) {
323 builtinEngine = new BuiltInEngine(contextType);
324 builtinEngine.complete(viewer, offset, syntaxbuffer);
325 builtinResults = builtinEngine.getResults();
329 // concatenate the result arrays
330 IPHPCompletionProposal[] total;
332 new IPHPCompletionProposal[templateResults.length
333 + identifierResults.length
334 + builtinResults.length
335 + declarationResults.length];
336 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
337 System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
338 System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
343 templateResults.length + identifierResults.length + builtinResults.length,
344 declarationResults.length);
348 fNumberOfComputedResults = (results == null ? 0 : results.length);
350 * Order here and not in result collector to make sure that the order
351 * applies to all proposals and not just those of the compilation unit.
353 return order(results);
355 return new IPHPCompletionProposal[0];
358 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
359 int contextPosition = offset;
361 IDocument document = viewer.getDocument();
365 // PHPCodeReader reader= new PHPCodeReader();
366 // reader.configureBackwardReader(document, offset, true, true);
368 // int nestingLevel= 0;
370 // int curr= reader.read();
371 // while (curr != PHPCodeReader.EOF) {
373 // if (')' == (char) curr)
376 // else if ('(' == (char) curr) {
379 // if (nestingLevel < 0) {
380 // int start= reader.getOffset();
381 // if (looksLikeMethod(reader))
386 // curr= reader.read();
388 // } catch (IOException e) {
391 return contextPosition;
395 * Method declared on IContentAssistProcessor
397 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
398 // IContextInformation[] result = new IContextInformation[5];
399 // for (int i = 0; i < result.length; i++)
400 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
401 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
405 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
407 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
408 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
409 List result = addContextInformations(viewer, contextInformationPosition);
410 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
413 private List addContextInformations(ITextViewer viewer, int offset) {
414 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
416 List result = new ArrayList();
417 for (int i = 0; i < proposals.length; i++) {
418 IContextInformation contextInformation = proposals[i].getContextInformation();
419 if (contextInformation != null) {
420 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
421 wrapper.setContextInformationPosition(offset);
429 * Order the given proposals.
431 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
432 Arrays.sort(proposals, fComparator);
437 * Method declared on IContentAssistProcessor
439 public char[] getCompletionProposalAutoActivationCharacters() {
440 return fProposalAutoActivationSet;
441 // return null; // new char[] { '$' };
445 * Method declared on IContentAssistProcessor
447 public char[] getContextInformationAutoActivationCharacters() {
453 * Method declared on IContentAssistProcessor
455 public IContextInformationValidator getContextInformationValidator() {
460 * Method declared on IContentAssistProcessor
462 public String getErrorMessage() {