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