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;
23 import org.eclipse.ui.IEditorPart;
26 * Simple PHPDoc completion processor.
28 public class PHPDocCompletionProcessor implements IContentAssistProcessor {
30 private static class JavaDocCompletionProposalComparator 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());
38 // private IEditorPart fEditor;
39 // private IWorkingCopyManager fManager;
40 private char[] fProposalAutoActivationSet;
41 private PHPCompletionProposalComparator fComparator;
42 private TemplateEngine fTemplateEngine;
44 private boolean fRestrictToMatchingCase;
47 public PHPDocCompletionProcessor() {// (IEditorPart 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;
56 fComparator= new PHPCompletionProposalComparator();
60 * Tells this processor to order the proposals alphabetically.
62 * @param order <code>true</code> if proposals should be ordered.
64 public void orderProposalsAlphabetically(boolean order) {
65 fComparator.setOrderAlphabetically(order);
69 * Tells this processor to restrict is proposals to those
70 * starting with matching cases.
72 * @param restrict <code>true</code> if proposals should be restricted
74 public void restrictProposalsToMatchingCases(boolean restrict) {
75 fRestrictToMatchingCase= restrict;
79 * @see IContentAssistProcessor#getErrorMessage()
81 public String getErrorMessage() {
86 * @see IContentAssistProcessor#getContextInformationValidator()
88 public IContextInformationValidator getContextInformationValidator() {
93 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
95 public char[] getContextInformationAutoActivationCharacters() {
100 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
102 public char[] getCompletionProposalAutoActivationCharacters() {
103 return fProposalAutoActivationSet;
107 * Sets this processor's set of characters triggering the activation of the
108 * completion proposal computation.
110 * @param activationSet the activation set
112 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
113 fProposalAutoActivationSet= activationSet;
117 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
119 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
124 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
126 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
127 // ICompilationUnit unit= fManager.getWorkingCopy(fEditor.getEditorInput());
128 IDocument document= viewer.getDocument();
130 IPHPCompletionProposal[] results= new IPHPCompletionProposal[0];
133 // if (unit != null) {
135 // int offset= documentOffset;
138 // Point selection= viewer.getSelectedRange();
139 // if (selection.y > 0) {
140 // offset= selection.x;
141 // length= selection.y;
144 // JavaDocCompletionEvaluator evaluator= new JavaDocCompletionEvaluator(unit, document, offset, length);
145 // evaluator.restrictProposalsToMatchingCases(fRestrictToMatchingCase);
146 // results= evaluator.computeProposals();
148 // } catch (JavaModelException e) {
149 // JavaPlugin.log(e);
152 if (fTemplateEngine != null) {
154 fTemplateEngine.reset();
155 fTemplateEngine.complete(viewer, documentOffset); //, unit);
156 // } catch (JavaModelException x) {
159 IPHPCompletionProposal[] templateResults= fTemplateEngine.getResults();
160 if (results.length == 0) {
161 results= templateResults;
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);
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.
175 return order(results);
179 * Order the given proposals.
181 private IPHPCompletionProposal[] order(IPHPCompletionProposal[] proposals) {
182 Arrays.sort(proposals, fComparator);