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.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;
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;
28 * Simple PHPDoc completion processor.
30 public class PHPDocCompletionProcessor implements IContentAssistProcessor {
32 private static class PHPDocCompletionProposalComparator implements
34 public int compare(Object o1, Object o2) {
35 ICompletionProposal c1 = (ICompletionProposal) o1;
36 ICompletionProposal c2 = (ICompletionProposal) o2;
37 return c1.getDisplayString().compareTo(c2.getDisplayString());
41 // private IEditorPart fEditor;
42 // private IWorkingCopyManager fManager;
43 private char[] fProposalAutoActivationSet;
45 private PHPCompletionProposalComparator fComparator;
47 private TemplateEngine fTemplateEngine;
49 private boolean fRestrictToMatchingCase;
51 private IEditorPart fEditor;
53 protected IWorkingCopyManager fManager;
55 public PHPDocCompletionProcessor(IEditorPart editor) {
57 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
60 // fManager= JavaPlugin.getDefault().getWorkingCopyManager();
61 TemplateContextType contextType = PHPeclipsePlugin.getDefault()
62 .getTemplateContextRegistry().getContextType("phpdoc"); //$NON-NLS-1$
63 if (contextType != null)
64 fTemplateEngine = new TemplateEngine(contextType);
65 fRestrictToMatchingCase = false;
67 fComparator = new PHPCompletionProposalComparator();
71 * Tells this processor to order the proposals alphabetically.
74 * <code>true</code> if proposals should be ordered.
76 public void orderProposalsAlphabetically(boolean order) {
77 fComparator.setOrderAlphabetically(order);
81 * Tells this processor to restrict is proposals to those starting with
85 * <code>true</code> if proposals should be restricted
87 public void restrictProposalsToMatchingCases(boolean restrict) {
88 fRestrictToMatchingCase = restrict;
92 * @see IContentAssistProcessor#getErrorMessage()
94 public String getErrorMessage() {
99 * @see IContentAssistProcessor#getContextInformationValidator()
101 public IContextInformationValidator getContextInformationValidator() {
106 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
108 public char[] getContextInformationAutoActivationCharacters() {
113 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
115 public char[] getCompletionProposalAutoActivationCharacters() {
116 return fProposalAutoActivationSet;
120 * Sets this processor's set of characters triggering the activation of the
121 * completion proposal computation.
123 * @param activationSet
126 public void setCompletionProposalAutoActivationCharacters(
127 char[] activationSet) {
128 fProposalAutoActivationSet = activationSet;
132 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
134 public IContextInformation[] computeContextInformation(ITextViewer viewer,
140 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
142 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
143 int documentOffset) {
144 ICompilationUnit unit = fManager.getWorkingCopy(fEditor
146 IDocument document = viewer.getDocument();
148 IPHPCompletionProposal[] results = new IPHPCompletionProposal[0];
151 // if (unit != null) {
153 // int offset= documentOffset;
156 // Point selection= viewer.getSelectedRange();
157 // if (selection.y > 0) {
158 // offset= selection.x;
159 // length= selection.y;
162 // JavaDocCompletionEvaluator evaluator= new
163 // JavaDocCompletionEvaluator(unit, document, offset, length);
164 // evaluator.restrictProposalsToMatchingCases(fRestrictToMatchingCase);
165 // results= evaluator.computeProposals();
167 // } catch (JavaModelException e) {
168 // JavaPlugin.log(e);
171 if (fTemplateEngine != null) {
173 fTemplateEngine.reset();
174 fTemplateEngine.complete(viewer, documentOffset, unit);
175 // } catch (JavaModelException x) {
178 IPHPCompletionProposal[] templateResults = fTemplateEngine
180 if (results.length == 0) {
181 results = templateResults;
183 // concatenate arrays
184 IPHPCompletionProposal[] total = new IPHPCompletionProposal[results.length
185 + templateResults.length];
186 System.arraycopy(templateResults, 0, total, 0,
187 templateResults.length);
188 System.arraycopy(results, 0, total, templateResults.length,
195 * Order here and not in result collector to make sure that the order
196 * applies to all proposals and not just those of the compilation unit.
198 return order(results);
202 * Order the given proposals.
204 private IPHPCompletionProposal[] order(IPHPCompletionProposal[] proposals) {
205 Arrays.sort(proposals, fComparator);