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