improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / BuiltInEngine.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.text.java.IPHPCompletionProposal;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
16 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
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 net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
25
26 public class BuiltInEngine {
27
28   /** The context type. */
29   private JavaContextType 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 BuiltInEngine(JavaContextType 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, ArrayList identifiers,ICompilationUnit compilationUnit)
67   //hrows JavaModelException
68   {
69     IDocument document = viewer.getDocument();
70
71     // prohibit recursion
72     //          if (LinkedPositionManager.hasActiveManager(document))
73     //                  return;
74
75     if (!(fContextType instanceof CompilationUnitContextType))
76       return;
77     Point selection = viewer.getSelectedRange();
78     // remember selected text
79     String selectedText = null;
80     if (selection.y != 0) {
81       try {
82         selectedText = document.get(selection.x, selection.y);
83       } catch (BadLocationException e) {
84       }
85     }
86
87 //    ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
88 //    JavaContext context = (JavaContext) fContextType.createContext();
89     JavaContext context = (JavaContext) fContextType.createContext(document, completionPosition,selection.y,compilationUnit);
90     context.setVariable("selection", selectedText); //$NON-NLS-1$
91     int start = context.getStart();
92     int end = context.getEnd();
93     IRegion region = new Region(start, end - start);
94
95     //          Template[] templates= Templates.getInstance().getTemplates();
96     String identifier = null;
97     int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
98     PHPElement element = null;
99     for (int i = 0; i != identifiers.size(); i++) {
100       element = (PHPElement) identifiers.get(i);
101       if (element instanceof PHPFunction) {
102         identifier = ((PHPFunction) element).getName();
103         if (context.canEvaluate(identifier)) {
104           if (maxProposals-- < 0) {
105             return;
106           }
107           fProposals.add(new BuiltInProposal(identifier, (PHPFunction) element, context, region, viewer));
108         }
109       }
110     }
111   }
112
113 }