Defined a limit for code completion list entries PHPeclipsePlugin.MAX_PROPOSALS
[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
33   /**
34    * Creates the template engine for a particular context type.
35    * See <code>TemplateContext</code> for supported context types.
36    */
37   public DeclarationEngine(ContextType contextType) {
38     //  Assert.isNotNull(contextType);
39     fContextType = contextType;
40   }
41
42   /**
43    * Empties the collector.
44    * 
45    * @param viewer the text viewer  
46    * @param unit   the compilation unit (may be <code>null</code>)
47    */
48   public void reset() {
49     fProposals.clear();
50   }
51
52   /**
53    * Returns the array of matching templates.
54    */
55   public IPHPCompletionProposal[] getResults() {
56     return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
57   }
58
59   /**
60    * Inspects the context of the compilation unit around <code>completionPosition</code>
61    * and feeds the collector with proposals.
62    * @param viewer the text viewer
63    * @param completionPosition the context position in the document of the text viewer
64    * @param compilationUnit the compilation unit (may be <code>null</code>)
65    */
66   public void complete(ITextViewer viewer, int completionPosition, SortedMap map)
67   //,ICompilationUnit compilationUnit)
68   //hrows JavaModelException
69   {
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     if (selection.y != 0) {
79       try {
80         selectedText = document.get(selection.x, selection.y);
81       } catch (BadLocationException e) {
82       }
83     }
84
85     ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
86
87     PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
88     int start = context.getStart();
89     int end = context.getEnd();
90     String prefix = context.getKey();
91     IRegion region = new Region(start, end - start);
92
93     String identifier = null;
94
95     SortedMap subMap = map.subMap(prefix, prefix + '\255');
96     Iterator iter = subMap.keySet().iterator();
97     PHPIdentifierLocation location;
98     ArrayList list;
99     int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
100     while (iter.hasNext()) {
101       identifier = (String) iter.next();
102       if (context.canEvaluate(identifier)) {
103         list = (ArrayList) subMap.get(identifier);
104         for (int i = 0; i < list.size(); i++) {
105           if (maxProposals-- < 0) {
106             return;
107           }
108           fProposals.add(new DeclarationProposal(identifier, (PHPIdentifierLocation) list.get(i), context, region, viewer));
109         }
110       }
111     }
112
113   }
114
115 }