1 package net.sourceforge.phpdt.internal.ui.text.phpdoc;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import java.util.Arrays;
9 import java.util.Comparator;
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;
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;
25 * Simple PHPDoc completion processor.
27 public class PHPDocCompletionProcessor implements IContentAssistProcessor {
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());
37 // private IEditorPart fEditor;
38 // private IWorkingCopyManager fManager;
39 private char[] fProposalAutoActivationSet;
40 private PHPCompletionProposalComparator fComparator;
41 private TemplateEngine fTemplateEngine;
43 private boolean fRestrictToMatchingCase;
46 public PHPDocCompletionProcessor() {// (IEditorPart 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;
55 fComparator= new PHPCompletionProposalComparator();
59 * Tells this processor to order the proposals alphabetically.
61 * @param order <code>true</code> if proposals should be ordered.
63 public void orderProposalsAlphabetically(boolean order) {
64 fComparator.setOrderAlphabetically(order);
68 * Tells this processor to restrict is proposals to those
69 * starting with matching cases.
71 * @param restrict <code>true</code> if proposals should be restricted
73 public void restrictProposalsToMatchingCases(boolean restrict) {
74 fRestrictToMatchingCase= restrict;
78 * @see IContentAssistProcessor#getErrorMessage()
80 public String getErrorMessage() {
85 * @see IContentAssistProcessor#getContextInformationValidator()
87 public IContextInformationValidator getContextInformationValidator() {
92 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
94 public char[] getContextInformationAutoActivationCharacters() {
99 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
101 public char[] getCompletionProposalAutoActivationCharacters() {
102 return fProposalAutoActivationSet;
106 * Sets this processor's set of characters triggering the activation of the
107 * completion proposal computation.
109 * @param activationSet the activation set
111 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
112 fProposalAutoActivationSet= activationSet;
116 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
118 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
123 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
125 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
126 // ICompilationUnit unit= fManager.getWorkingCopy(fEditor.getEditorInput());
127 IDocument document= viewer.getDocument();
129 IPHPCompletionProposal[] results= new IPHPCompletionProposal[0];
132 // if (unit != null) {
134 // int offset= documentOffset;
137 // Point selection= viewer.getSelectedRange();
138 // if (selection.y > 0) {
139 // offset= selection.x;
140 // length= selection.y;
143 // JavaDocCompletionEvaluator evaluator= new JavaDocCompletionEvaluator(unit, document, offset, length);
144 // evaluator.restrictProposalsToMatchingCases(fRestrictToMatchingCase);
145 // results= evaluator.computeProposals();
147 // } catch (JavaModelException e) {
148 // JavaPlugin.log(e);
151 if (fTemplateEngine != null) {
153 fTemplateEngine.reset();
154 fTemplateEngine.complete(viewer, documentOffset); //, unit);
155 // } catch (JavaModelException x) {
158 IPHPCompletionProposal[] templateResults= fTemplateEngine.getResults();
159 if (results.length == 0) {
160 results= templateResults;
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);
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.
174 return order(results);
178 * Order the given proposals.
180 private IPHPCompletionProposal[] order(IPHPCompletionProposal[] proposals) {
181 Arrays.sort(proposals, fComparator);