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