Improved CodeCompletion with information from the project.index file.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / IdentifierEngine.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
9 import net.sourceforge.phpdt.internal.corext.template.ContextType;
10 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
11 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
12 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
13 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
14
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.IRegion;
18 import org.eclipse.jface.text.ITextViewer;
19 import org.eclipse.jface.text.Region;
20 import org.eclipse.swt.graphics.Point;
21 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
22
23 public class IdentifierEngine {
24
25   /** The context type. */
26   private ContextType fContextType;
27   /** The result proposals. */
28   private ArrayList fProposals = new ArrayList();
29
30   /**
31    * Creates the template engine for a particular context type.
32    * See <code>TemplateContext</code> for supported context types.
33    */
34   public IdentifierEngine(ContextType contextType) {
35     //  Assert.isNotNull(contextType);
36     fContextType = contextType;
37   }
38
39   /**
40    * Empties the collector.
41    * 
42    * @param viewer the text viewer  
43    * @param unit   the compilation unit (may be <code>null</code>)
44    */
45   public void reset() {
46     fProposals.clear();
47   }
48
49   /**
50    * Returns the array of matching templates.
51    */
52   public IPHPCompletionProposal[] getResults() {
53     return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
54   }
55
56   /**
57    * Inspects the context of the compilation unit around <code>completionPosition</code>
58    * and feeds the collector with proposals.
59    * @param viewer the text viewer
60    * @param completionPosition the context position in the document of the text viewer
61    * @param compilationUnit the compilation unit (may be <code>null</code>)
62    */
63   public void complete(ITextViewer viewer, int completionPosition, Object[] identifiers)
64   //,ICompilationUnit compilationUnit)
65   //hrows JavaModelException
66   {
67     IDocument document = viewer.getDocument();
68
69     if (!(fContextType instanceof CompilationUnitContextType))
70       return;
71
72     Point selection = viewer.getSelectedRange();
73     // remember selected text
74     String selectedText = null;
75     if (selection.y != 0) {
76       try {
77         selectedText = document.get(selection.x, selection.y);
78       } catch (BadLocationException e) {
79       }
80     }
81
82     ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
83
84     PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
85     int start = context.getStart();
86     int end = context.getEnd();
87     IRegion region = new Region(start, end - start);
88
89     //          Template[] templates= Templates.getInstance().getTemplates();
90     String identifier = null;
91     for (int i = 0; i != identifiers.length; i++) {
92       identifier = (String) identifiers[i];
93       if (context.canEvaluate(identifier)) {
94         fProposals.add(
95           new IdentifierProposal(
96             identifier,
97             context,
98             region,
99             viewer,
100             PHPUiImages.get(PHPUiImages.IMG_FUN),
101             PHPUiImages.get(PHPUiImages.IMG_VAR)));
102       }
103     }
104   }
105
106 }