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