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