new PartitionScanner version
[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.compiler.ITerminalSymbols;
12 import net.sourceforge.phpdt.internal.corext.template.ContextType;
13 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
14 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
15 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
17 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
18
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.Region;
26 import org.eclipse.swt.graphics.Point;
27 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
28
29 public class DeclarationEngine {
30
31   /** The context type. */
32   private ContextType fContextType;
33   /** The result proposals. */
34   private ArrayList fProposals = new ArrayList();
35   /** Token determines last which declarations are allowed for proposal */
36   private int fLastSignificantToken;
37
38   private IProject fProject;
39   private IFile fFile;
40 //  private String fFileName;
41
42   /**
43    * Creates the template engine for a particular context type.
44    * See <code>TemplateContext</code> for supported context types.
45    */
46   public DeclarationEngine(IProject project, ContextType contextType, int lastSignificantToken, IFile file) {
47     //  Assert.isNotNull(contextType);
48     fProject = project;
49     fContextType = contextType;
50
51     fLastSignificantToken = lastSignificantToken;
52     fFile = file;
53 //    if (fFile != null) {
54 //      fFileName = fFile.getFullPath().toString();
55 //    } else {
56 //      fFileName = "";
57 //    }
58   }
59
60   /**
61    * Empties the collector.
62    * 
63    * @param viewer the text viewer  
64    * @param unit   the compilation unit (may be <code>null</code>)
65    */
66   public void reset() {
67     fProposals.clear();
68   }
69
70   /**
71    * Returns the array of matching templates.
72    */
73   public IPHPCompletionProposal[] getResults() {
74     return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
75   }
76
77   /**
78    * Inspects the context of the compilation unit around <code>completionPosition</code>
79    * and feeds the collector with proposals.
80    * @param viewer the text viewer
81    * @param completionPosition the context position in the document of the text viewer
82    * @param compilationUnit the compilation unit (may be <code>null</code>)
83    */
84   public void complete(ITextViewer viewer, int completionPosition, SortedMap map) {
85     IDocument document = viewer.getDocument();
86
87     if (!(fContextType instanceof CompilationUnitContextType))
88       return;
89
90     Point selection = viewer.getSelectedRange();
91
92     // remember selected text
93     String selectedText = null;
94
95     if (selection.y != 0) {
96       try {
97         selectedText = document.get(selection.x, selection.y);
98       } catch (BadLocationException e) {
99       }
100     }
101
102     ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);
103
104     PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
105     int start = context.getStart();
106     int end = context.getEnd();
107     String prefix = context.getKey();
108     IRegion region = new Region(start, end - start);
109
110     String identifier = null;
111
112     SortedMap subMap = map.subMap(prefix, prefix + '\255');
113     Iterator iter = subMap.keySet().iterator();
114     PHPIdentifierLocation location;
115     ArrayList list;
116     int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
117     while (iter.hasNext()) {
118       identifier = (String) iter.next();
119       if (context.canEvaluate(identifier)) {
120         list = (ArrayList) subMap.get(identifier);
121         for (int i = 0; i < list.size(); i++) {
122           location = (PHPIdentifierLocation) list.get(i);
123           int type = location.getType();
124           switch (fLastSignificantToken) {
125             case ITerminalSymbols.TokenNameMINUS_GREATER :
126               if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
127                 continue; // for loop
128               }
129               break;
130             case ITerminalSymbols.TokenNameVariable :
131               if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
132                 continue; // for loop
133               }
134               break;
135             case ITerminalSymbols.TokenNamethis_PHP_COMPLETION:
136               if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
137                 continue; // for loop
138               }
139               // check all filenames of the subclasses
140 //              if (!fFileName.equals(location.getFilename())) {
141 //                continue; // for loop
142 //              }
143               break;
144             case ITerminalSymbols.TokenNamenew :
145               if (type != PHPIdentifierLocation.CLASS && type != PHPIdentifierLocation.CONSTRUCTOR) {
146                 continue; // for loop
147               }
148               break;
149             default :
150               if (type == PHPIdentifierLocation.METHOD || type == PHPIdentifierLocation.CONSTRUCTOR || type == PHPIdentifierLocation.VARIABLE) {
151                 continue; // for loop
152               }
153           }
154           if (maxProposals-- < 0) {
155             return;
156           }
157           fProposals.add( new DeclarationProposal(fProject, identifier, location, context, region, viewer));
158         }
159       }
160     }
161
162   }
163
164 }