fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[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
25 public class BuiltInEngine {
26
27         /** The context type. */
28         private JavaContextType fContextType;
29
30         /** The result proposals. */
31         private ArrayList fProposals = new ArrayList();
32
33         /**
34          * Creates the template engine for a particular context type. See
35          * <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
46          *            the text viewer
47          * @param unit
48          *            the compilation unit (may be <code>null</code>)
49          */
50         public void reset() {
51                 fProposals.clear();
52         }
53
54         /**
55          * Returns the array of matching templates.
56          */
57         public IPHPCompletionProposal[] getResults() {
58                 return (IPHPCompletionProposal[]) fProposals
59                                 .toArray(new IPHPCompletionProposal[fProposals.size()]);
60         }
61
62         /**
63          * Inspects the context of the compilation unit around
64          * <code>completionPosition</code> and feeds the collector with proposals.
65          * 
66          * @param viewer
67          *            the text viewer
68          * @param completionPosition
69          *            the context position in the document of the text viewer
70          * @param compilationUnit
71          *            the compilation unit (may be <code>null</code>)
72          */
73         public void complete(ITextViewer viewer, int completionPosition,
74                         ArrayList identifiers, ICompilationUnit compilationUnit)
75         // hrows JavaModelException
76         {
77                 IDocument document = viewer.getDocument();
78
79                 // prohibit recursion
80                 // if (LinkedPositionManager.hasActiveManager(document))
81                 // return;
82
83                 if (!(fContextType instanceof CompilationUnitContextType))
84                         return;
85                 Point selection = viewer.getSelectedRange();
86                 // remember selected text
87                 String selectedText = null;
88                 if (selection.y != 0) {
89                         try {
90                                 selectedText = document.get(selection.x, selection.y);
91                         } catch (BadLocationException e) {
92                         }
93                 }
94
95                 // ((CompilationUnitContextType)
96                 // fContextType).setContextParameters(document, completionPosition,
97                 // selection.y); //mpilationUnit);
98                 // JavaContext context = (JavaContext) fContextType.createContext();
99                 JavaContext context = (JavaContext) fContextType.createContext(
100                                 document, completionPosition, selection.y, compilationUnit);
101                 context.setVariable("selection", selectedText); //$NON-NLS-1$
102                 int start = context.getStart();
103                 int end = context.getEnd();
104                 IRegion region = new Region(start, end - start);
105
106                 // Template[] templates= Templates.getInstance().getTemplates();
107                 String identifier = null;
108                 int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
109                 PHPElement element = null;
110                 for (int i = 0; i != identifiers.size(); i++) {
111                         element = (PHPElement) identifiers.get(i);
112                         if (element instanceof PHPFunction) {
113                                 identifier = ((PHPFunction) element).getName();
114                                 if (context.canEvaluate(identifier)) {
115                                         if (maxProposals-- < 0) {
116                                                 return;
117                                         }
118                                         fProposals.add(new BuiltInProposal(identifier,
119                                                         (PHPFunction) element, context, region, viewer));
120                                 }
121                         }
122                 }
123         }
124
125 }