fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / phpdoc / PHPDocCompletionProcessor.java
1 package net.sourceforge.phpdt.internal.ui.text.phpdoc;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */
7
8 import java.util.Arrays;
9 import java.util.Comparator;
10
11 import net.sourceforge.phpdt.core.ICompilationUnit;
12 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
13 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
14 import net.sourceforge.phpdt.internal.ui.text.template.contentassist.TemplateEngine;
15 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
17
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.contentassist.ICompletionProposal;
21 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
24 import org.eclipse.jface.text.templates.TemplateContextType;
25 import org.eclipse.ui.IEditorPart;
26
27 /**
28  * Simple PHPDoc completion processor.
29  */
30 public class PHPDocCompletionProcessor implements IContentAssistProcessor {
31
32         private static class PHPDocCompletionProposalComparator implements
33                         Comparator {
34                 public int compare(Object o1, Object o2) {
35                         ICompletionProposal c1 = (ICompletionProposal) o1;
36                         ICompletionProposal c2 = (ICompletionProposal) o2;
37                         return c1.getDisplayString().compareTo(c2.getDisplayString());
38                 }
39         };
40
41         // private IEditorPart fEditor;
42         // private IWorkingCopyManager fManager;
43         private char[] fProposalAutoActivationSet;
44
45         private PHPCompletionProposalComparator fComparator;
46
47         private TemplateEngine fTemplateEngine;
48
49         private boolean fRestrictToMatchingCase;
50
51         private IEditorPart fEditor;
52
53         protected IWorkingCopyManager fManager;
54
55         public PHPDocCompletionProcessor(IEditorPart editor) {
56                 fEditor = editor;
57                 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
58
59                 // fEditor= editor;
60                 // fManager= JavaPlugin.getDefault().getWorkingCopyManager();
61                 TemplateContextType contextType = PHPeclipsePlugin.getDefault()
62                                 .getTemplateContextRegistry().getContextType("phpdoc"); //$NON-NLS-1$
63                 if (contextType != null)
64                         fTemplateEngine = new TemplateEngine(contextType);
65                 fRestrictToMatchingCase = false;
66
67                 fComparator = new PHPCompletionProposalComparator();
68         }
69
70         /**
71          * Tells this processor to order the proposals alphabetically.
72          * 
73          * @param order
74          *            <code>true</code> if proposals should be ordered.
75          */
76         public void orderProposalsAlphabetically(boolean order) {
77                 fComparator.setOrderAlphabetically(order);
78         }
79
80         /**
81          * Tells this processor to restrict is proposals to those starting with
82          * matching cases.
83          * 
84          * @param restrict
85          *            <code>true</code> if proposals should be restricted
86          */
87         public void restrictProposalsToMatchingCases(boolean restrict) {
88                 fRestrictToMatchingCase = restrict;
89         }
90
91         /**
92          * @see IContentAssistProcessor#getErrorMessage()
93          */
94         public String getErrorMessage() {
95                 return null;
96         }
97
98         /**
99          * @see IContentAssistProcessor#getContextInformationValidator()
100          */
101         public IContextInformationValidator getContextInformationValidator() {
102                 return null;
103         }
104
105         /**
106          * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
107          */
108         public char[] getContextInformationAutoActivationCharacters() {
109                 return null;
110         }
111
112         /**
113          * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
114          */
115         public char[] getCompletionProposalAutoActivationCharacters() {
116                 return fProposalAutoActivationSet;
117         }
118
119         /**
120          * Sets this processor's set of characters triggering the activation of the
121          * completion proposal computation.
122          * 
123          * @param activationSet
124          *            the activation set
125          */
126         public void setCompletionProposalAutoActivationCharacters(
127                         char[] activationSet) {
128                 fProposalAutoActivationSet = activationSet;
129         }
130
131         /**
132          * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
133          */
134         public IContextInformation[] computeContextInformation(ITextViewer viewer,
135                         int offset) {
136                 return null;
137         }
138
139         /**
140          * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
141          */
142         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
143                         int documentOffset) {
144                 ICompilationUnit unit = fManager.getWorkingCopy(fEditor
145                                 .getEditorInput());
146                 IDocument document = viewer.getDocument();
147
148                 IPHPCompletionProposal[] results = new IPHPCompletionProposal[0];
149
150                 // try {
151                 // if (unit != null) {
152                 //                              
153                 // int offset= documentOffset;
154                 // int length= 0;
155                 //                              
156                 // Point selection= viewer.getSelectedRange();
157                 // if (selection.y > 0) {
158                 // offset= selection.x;
159                 // length= selection.y;
160                 // }
161                 //                              
162                 // JavaDocCompletionEvaluator evaluator= new
163                 // JavaDocCompletionEvaluator(unit, document, offset, length);
164                 // evaluator.restrictProposalsToMatchingCases(fRestrictToMatchingCase);
165                 // results= evaluator.computeProposals();
166                 // }
167                 // } catch (JavaModelException e) {
168                 // JavaPlugin.log(e);
169                 // }
170
171                 if (fTemplateEngine != null) {
172                         // try {
173                         fTemplateEngine.reset();
174                         fTemplateEngine.complete(viewer, documentOffset, unit);
175                         // } catch (JavaModelException x) {
176                         // }
177
178                         IPHPCompletionProposal[] templateResults = fTemplateEngine
179                                         .getResults();
180                         if (results.length == 0) {
181                                 results = templateResults;
182                         } else {
183                                 // concatenate arrays
184                                 IPHPCompletionProposal[] total = new IPHPCompletionProposal[results.length
185                                                 + templateResults.length];
186                                 System.arraycopy(templateResults, 0, total, 0,
187                                                 templateResults.length);
188                                 System.arraycopy(results, 0, total, templateResults.length,
189                                                 results.length);
190                                 results = total;
191                         }
192                 }
193
194                 /*
195                  * Order here and not in result collector to make sure that the order
196                  * applies to all proposals and not just those of the compilation unit.
197                  */
198                 return order(results);
199         }
200
201         /**
202          * Order the given proposals.
203          */
204         private IPHPCompletionProposal[] order(IPHPCompletionProposal[] proposals) {
205                 Arrays.sort(proposals, fComparator);
206                 return proposals;
207         }
208 }