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 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());
40 // private IEditorPart fEditor;
41 // private IWorkingCopyManager fManager;
42 private char[] fProposalAutoActivationSet;
44 private PHPCompletionProposalComparator fComparator;
46 private TemplateEngine fTemplateEngine;
48 private boolean fRestrictToMatchingCase;
50 private IEditorPart fEditor;
52 protected IWorkingCopyManager fManager;
54 public PHPDocCompletionProcessor(IEditorPart editor) {
56 fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
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;
65 fComparator = new PHPCompletionProposalComparator();
69 * Tells this processor to order the proposals alphabetically.
72 * <code>true</code> if proposals should be ordered.
74 public void orderProposalsAlphabetically(boolean order) {
75 fComparator.setOrderAlphabetically(order);
79 * Tells this processor to restrict is proposals to those starting with matching cases.
82 * <code>true</code> if proposals should be restricted
84 public void restrictProposalsToMatchingCases(boolean restrict) {
85 fRestrictToMatchingCase = restrict;
89 * @see IContentAssistProcessor#getErrorMessage()
91 public String getErrorMessage() {
96 * @see IContentAssistProcessor#getContextInformationValidator()
98 public IContextInformationValidator getContextInformationValidator() {
103 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
105 public char[] getContextInformationAutoActivationCharacters() {
110 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
112 public char[] getCompletionProposalAutoActivationCharacters() {
113 return fProposalAutoActivationSet;
117 * Sets this processor's set of characters triggering the activation of the completion proposal computation.
119 * @param activationSet
122 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
123 fProposalAutoActivationSet = activationSet;
127 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
129 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
134 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
136 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
137 ICompilationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
138 IDocument document = viewer.getDocument();
140 IPHPCompletionProposal[] results = new IPHPCompletionProposal[0];
143 // if (unit != null) {
145 // int offset= documentOffset;
148 // Point selection= viewer.getSelectedRange();
149 // if (selection.y > 0) {
150 // offset= selection.x;
151 // length= selection.y;
154 // JavaDocCompletionEvaluator evaluator= new JavaDocCompletionEvaluator(unit, document, offset, length);
155 // evaluator.restrictProposalsToMatchingCases(fRestrictToMatchingCase);
156 // results= evaluator.computeProposals();
158 // } catch (JavaModelException e) {
159 // JavaPlugin.log(e);
162 if (fTemplateEngine != null) {
164 fTemplateEngine.reset();
165 fTemplateEngine.complete(viewer, documentOffset, unit);
166 // } catch (JavaModelException x) {
169 IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
170 if (results.length == 0) {
171 results = templateResults;
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);
182 * Order here and not in result collector to make sure that the order applies to all proposals and not just those of the
185 return order(results);
189 * Order the given proposals.
191 private IPHPCompletionProposal[] order(IPHPCompletionProposal[] proposals) {
192 Arrays.sort(proposals, fComparator);