fa9aba0a81fb7d8dbf229a938279a22a8d75139c
[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.internal.corext.template.ContextType;
12 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
13 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
14 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
17
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.Region;
23 import org.eclipse.swt.graphics.Point;
24 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
25
26 public class DeclarationEngine {
27
28   /** The context type. */
29   private ContextType fContextType;
30   /** The result proposals. */
31   private ArrayList fProposals = new ArrayList();
32   /** Use only methods or variables inside a class*/
33   private boolean fUseClassEntries;
34
35   /**
36    * Creates the template engine for a particular context type.
37    * See <code>TemplateContext</code> for supported context types.
38    */
39   public DeclarationEngine(ContextType contextType, boolean useClassEntries) {
40     //  Assert.isNotNull(contextType);
41     fContextType = contextType;
42                 fUseClassEntries = useClassEntries;
43   }
44
45   /**
46    * Empties the collector.
47    * 
48    * @param viewer the text viewer  
49    * @param unit   the compilation unit (may be <code>null</code>)
50    */
51   public void reset() {
52     fProposals.clear();
53   }
54
55   /**
56    * Returns the array of matching templates.
57    */
58   public IPHPCompletionProposal[] getResults() {
59     return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
60   }
61
62   /**
63    * Inspects the context of the compilation unit around <code>completionPosition</code>
64    * and feeds the collector with proposals.
65    * @param viewer the text viewer
66    * @param completionPosition the context position in the document of the text viewer
67    * @param compilationUnit the compilation unit (may be <code>null</code>)
68    */
69   public void complete(ITextViewer viewer, int completionPosition, SortedMap map) {
70     IDocument document = viewer.getDocument();
71
72     if (!(fContextType instanceof CompilationUnitContextType))
73       return;
74
75     Point selection = viewer.getSelectedRange();
76     // remember selected text
77     String selectedText = null;
78     
79     if (selection.y != 0) {
80       try {
81         selectedText = document.get(selection.x, selection.y);
82       } catch (BadLocationException e) {
83       }
84     }
85
86     ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);
87
88     PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
89     int start = context.getStart();
90     int end = context.getEnd();
91     String prefix = context.getKey();
92     IRegion region = new Region(start, end - start);
93     
94     String identifier = null;
95
96     SortedMap subMap = map.subMap(prefix, prefix + '\255');
97     Iterator iter = subMap.keySet().iterator();
98     PHPIdentifierLocation location;
99     ArrayList list;
100     int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
101     while (iter.hasNext()) {
102       identifier = (String) iter.next();
103       if (context.canEvaluate(identifier)) {
104         list = (ArrayList) subMap.get(identifier);
105         for (int i = 0; i < list.size(); i++) {
106           location = (PHPIdentifierLocation) list.get(i);
107           int type = location.getType();
108           if (fUseClassEntries) {
109             if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) {
110               continue; // for loop
111             }
112           } else {
113             if (type == PHPIdentifierLocation.METHOD || type == PHPIdentifierLocation.VARIABLE) {
114               continue; // for loop
115             }
116           }
117           if (maxProposals-- < 0) {
118             return;
119           }
120           fProposals.add(new DeclarationProposal(identifier, location, context, region, viewer));
121         }
122       }
123     }
124
125   }
126
127 }