56274cd6e0eb5c468ec73482c46ad90147e301d1
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / DeclarationEngine.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.template;
6
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.SortedMap;
10
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;
19
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;
29
30 public class DeclarationEngine {
31
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;
38
39   private IProject fProject;
40   private IFile fFile;
41 //  private String fFileName;
42
43   /**
44    * Creates the template engine for a particular context type.
45    * See <code>TemplateContext</code> for supported context types.
46    */
47   public DeclarationEngine(IProject project, JavaContextType contextType, int lastSignificantToken, IFile file) {
48     //  Assert.isNotNull(contextType);
49     fProject = project;
50     fContextType = contextType;
51
52     fLastSignificantToken = lastSignificantToken;
53     fFile = file;
54 //    if (fFile != null) {
55 //      fFileName = fFile.getFullPath().toString();
56 //    } else {
57 //      fFileName = "";
58 //    }
59   }
60
61   /**
62    * Empties the collector.
63    * 
64    * @param viewer the text viewer  
65    * @param unit   the compilation unit (may be <code>null</code>)
66    */
67   public void reset() {
68     fProposals.clear();
69   }
70
71   /**
72    * Returns the array of matching templates.
73    */
74   public IPHPCompletionProposal[] getResults() {
75     return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
76   }
77
78   /**
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>)
84    */
85   public void complete(ITextViewer viewer, int completionPosition, SortedMap map, ICompilationUnit compilationUnit) {
86     IDocument document = viewer.getDocument();
87
88     if (!(fContextType instanceof CompilationUnitContextType))
89       return;
90
91     Point selection = viewer.getSelectedRange();
92
93     // remember selected text
94     String selectedText = null;
95
96     if (selection.y != 0) {
97       try {
98         selectedText = document.get(selection.x, selection.y);
99       } catch (BadLocationException e) {
100       }
101     }
102
103 //    ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);
104
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$
108
109     int start = context.getStart();
110     int end = context.getEnd();
111     String prefix = context.getKey();
112     IRegion region = new Region(start, end - start);
113
114     String identifier = null;
115
116     SortedMap subMap = map.subMap(prefix, prefix + '\255');
117     Iterator iter = subMap.keySet().iterator();
118     PHPIdentifierLocation location;
119     ArrayList list;
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
132               }
133               break;
134             case ITerminalSymbols.TokenNameVariable :
135               if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
136                 continue; // for loop
137               }
138               break;
139             case ITerminalSymbols.TokenNamethis_PHP_COMPLETION:
140               if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
141                 continue; // for loop
142               }
143               // check all filenames of the subclasses
144 //              if (!fFileName.equals(location.getFilename())) {
145 //                continue; // for loop
146 //              }
147               break;
148             case ITerminalSymbols.TokenNamenew :
149               if (type != PHPIdentifierLocation.CLASS && type != PHPIdentifierLocation.CONSTRUCTOR) {
150                 continue; // for loop
151               }
152               break;
153             default :
154               if (type == PHPIdentifierLocation.METHOD || type == PHPIdentifierLocation.CONSTRUCTOR || type == PHPIdentifierLocation.VARIABLE) {
155                 continue; // for loop
156               }
157           }
158           if (maxProposals-- < 0) {
159             return;
160           }
161           fProposals.add( new DeclarationProposal(fProject, identifier, location, context, region, viewer));
162         }
163       }
164     }
165
166   }
167
168 }