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.JavaOutlinePage;
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.TokenNameVariable) {
228 lastSignificantToken = ITerminalSymbols.TokenNameVariable;
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 // JavaOutlinePage 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 // TODO: get the identifiers from the new model
258 // if (outlinePage instanceof PHPContentOutlinePage) {
259 // identifiers = ((PHPContentOutlinePage) outlinePage).getVariables();
264 ContextType phpContextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
265 ((CompilationUnitContextType) phpContextType).setContextParameters(document, offset, 0);
267 PHPUnitContext context = (PHPUnitContext) phpContextType.createContext();
268 String prefix = context.getKey();
270 int lastSignificantToken = getLastToken(viewer, offset, context);
271 boolean useClassMembers =
272 (lastSignificantToken == ITerminalSymbols.TokenNameMINUS_GREATER) ||
273 (lastSignificantToken == ITerminalSymbols.TokenNameVariable) ||
274 (lastSignificantToken == ITerminalSymbols.TokenNamenew);
275 boolean emptyPrefix = prefix == null || prefix.equals("");
277 if (fTemplateEngine != null) {
278 IPHPCompletionProposal[] templateResults = new IPHPCompletionProposal[0];
280 ICompletionProposal[] results;
282 fTemplateEngine.reset();
283 fTemplateEngine.complete(viewer, offset); //, unit);
284 templateResults = fTemplateEngine.getResults();
287 IPHPCompletionProposal[] identifierResults = new IPHPCompletionProposal[0];
288 if ((!useClassMembers) && identifiers != null) {
289 IdentifierEngine identifierEngine;
291 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
292 if (contextType != null) {
293 identifierEngine = new IdentifierEngine(contextType);
294 identifierEngine.complete(viewer, offset, identifiers);
295 identifierResults = identifierEngine.getResults();
299 // declarations stored in file project.index on project level
300 IPHPCompletionProposal[] declarationResults = new IPHPCompletionProposal[0];
301 if (project != null) {
302 DeclarationEngine declarationEngine;
304 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
305 if (contextType != null) {
306 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(project);
307 SortedMap sortedMap = indexManager.getIdentifierMap();
309 declarationEngine = new DeclarationEngine(contextType, lastSignificantToken, file);
310 declarationEngine.complete(viewer, offset, sortedMap);
311 declarationResults = declarationEngine.getResults();
315 // built in function names from phpsyntax.xml
316 ArrayList syntaxbuffer = PHPSyntaxRdr.getSyntaxData();
317 IPHPCompletionProposal[] builtinResults = new IPHPCompletionProposal[0];
318 if ((!useClassMembers) && syntaxbuffer != null) {
319 BuiltInEngine builtinEngine;
322 ContextType contextType = ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
323 if (contextType != null) {
324 builtinEngine = new BuiltInEngine(contextType);
325 builtinEngine.complete(viewer, offset, syntaxbuffer);
326 builtinResults = builtinEngine.getResults();
330 // concatenate the result arrays
331 IPHPCompletionProposal[] total;
333 new IPHPCompletionProposal[templateResults.length
334 + identifierResults.length
335 + builtinResults.length
336 + declarationResults.length];
337 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
338 System.arraycopy(identifierResults, 0, total, templateResults.length, identifierResults.length);
339 System.arraycopy(builtinResults, 0, total, templateResults.length + identifierResults.length, builtinResults.length);
344 templateResults.length + identifierResults.length + builtinResults.length,
345 declarationResults.length);
349 fNumberOfComputedResults = (results == null ? 0 : results.length);
351 * Order here and not in result collector to make sure that the order
352 * applies to all proposals and not just those of the compilation unit.
354 return order(results);
356 return new IPHPCompletionProposal[0];
359 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
360 int contextPosition = offset;
362 IDocument document = viewer.getDocument();
366 // PHPCodeReader reader= new PHPCodeReader();
367 // reader.configureBackwardReader(document, offset, true, true);
369 // int nestingLevel= 0;
371 // int curr= reader.read();
372 // while (curr != PHPCodeReader.EOF) {
374 // if (')' == (char) curr)
377 // else if ('(' == (char) curr) {
380 // if (nestingLevel < 0) {
381 // int start= reader.getOffset();
382 // if (looksLikeMethod(reader))
387 // curr= reader.read();
389 // } catch (IOException e) {
392 return contextPosition;
396 * Method declared on IContentAssistProcessor
398 // public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
399 // IContextInformation[] result = new IContextInformation[5];
400 // for (int i = 0; i < result.length; i++)
401 // result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
402 // MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
406 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
408 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
409 int contextInformationPosition = guessContextInformationPosition(viewer, offset);
410 List result = addContextInformations(viewer, contextInformationPosition);
411 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
414 private List addContextInformations(ITextViewer viewer, int offset) {
415 ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
417 List result = new ArrayList();
418 for (int i = 0; i < proposals.length; i++) {
419 IContextInformation contextInformation = proposals[i].getContextInformation();
420 if (contextInformation != null) {
421 ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
422 wrapper.setContextInformationPosition(offset);
430 * Order the given proposals.
432 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
433 Arrays.sort(proposals, fComparator);
438 * Method declared on IContentAssistProcessor
440 public char[] getCompletionProposalAutoActivationCharacters() {
441 return fProposalAutoActivationSet;
442 // return null; // new char[] { '$' };
446 * Method declared on IContentAssistProcessor
448 public char[] getContextInformationAutoActivationCharacters() {
454 * Method declared on IContentAssistProcessor
456 public IContextInformationValidator getContextInformationValidator() {
461 * Method declared on IContentAssistProcessor
463 public String getErrorMessage() {