2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.SortedMap;
11 import net.sourceforge.phpdt.core.ICompilationUnit;
12 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
13 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
14 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
15 import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
16 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.Region;
27 import org.eclipse.swt.graphics.Point;
28 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
30 public class DeclarationEngine {
32 /** The context type. */
33 private JavaContextType fContextType;
34 /** The result proposals. */
35 private ArrayList fProposals = new ArrayList();
36 /** Token determines last which declarations are allowed for proposal */
37 private int fLastSignificantToken;
39 private IProject fProject;
41 // private String fFileName;
44 * Creates the template engine for a particular context type.
45 * See <code>TemplateContext</code> for supported context types.
47 public DeclarationEngine(IProject project, JavaContextType contextType, int lastSignificantToken, IFile file) {
48 // Assert.isNotNull(contextType);
50 fContextType = contextType;
52 fLastSignificantToken = lastSignificantToken;
54 // if (fFile != null) {
55 // fFileName = fFile.getFullPath().toString();
62 * Empties the collector.
64 * @param viewer the text viewer
65 * @param unit the compilation unit (may be <code>null</code>)
72 * Returns the array of matching templates.
74 public IPHPCompletionProposal[] getResults() {
75 return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
79 * Inspects the context of the compilation unit around <code>completionPosition</code>
80 * and feeds the collector with proposals.
81 * @param viewer the text viewer
82 * @param completionPosition the context position in the document of the text viewer
83 * @param compilationUnit the compilation unit (may be <code>null</code>)
85 public void complete(ITextViewer viewer, int completionPosition, SortedMap map, ICompilationUnit compilationUnit) {
86 IDocument document = viewer.getDocument();
88 if (!(fContextType instanceof CompilationUnitContextType))
91 Point selection = viewer.getSelectedRange();
93 // remember selected text
94 String selectedText = null;
96 if (selection.y != 0) {
98 selectedText = document.get(selection.x, selection.y);
99 } catch (BadLocationException e) {
103 // ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);
105 // CompilationUnitContext context = (CompilationUnitContext) fContextType.createContext();
106 JavaContext context = (JavaContext) fContextType.createContext(document, completionPosition,selection.y,compilationUnit);
107 context.setVariable("selection", selectedText); //$NON-NLS-1$
109 int start = context.getStart();
110 int end = context.getEnd();
111 String prefix = context.getKey();
112 IRegion region = new Region(start, end - start);
114 String identifier = null;
116 SortedMap subMap = map.subMap(prefix, prefix + '\255');
117 Iterator iter = subMap.keySet().iterator();
118 PHPIdentifierLocation location;
120 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
121 while (iter.hasNext()) {
122 identifier = (String) iter.next();
123 if (context.canEvaluate(identifier)) {
124 list = (ArrayList) subMap.get(identifier);
125 for (int i = 0; i < list.size(); i++) {
126 location = (PHPIdentifierLocation) list.get(i);
127 int type = location.getType();
128 switch (fLastSignificantToken) {
129 case ITerminalSymbols.TokenNameMINUS_GREATER :
130 if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
131 continue; // for loop
134 case ITerminalSymbols.TokenNameVariable :
135 if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
136 continue; // for loop
139 case ITerminalSymbols.TokenNamethis_PHP_COMPLETION:
140 if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
141 continue; // for loop
143 // check all filenames of the subclasses
144 // if (!fFileName.equals(location.getFilename())) {
145 // continue; // for loop
148 case ITerminalSymbols.TokenNamenew :
149 if (type != PHPIdentifierLocation.CLASS && type != PHPIdentifierLocation.CONSTRUCTOR) {
150 continue; // for loop
154 if (type == PHPIdentifierLocation.METHOD || type == PHPIdentifierLocation.CONSTRUCTOR || type == PHPIdentifierLocation.VARIABLE) {
155 continue; // for loop
158 if (maxProposals-- < 0) {
161 fProposals.add( new DeclarationProposal(fProject, identifier, location, context, region, viewer));