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