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.ui.WebUI;
17 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 //import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.contentassist.ICompletionProposal;
22 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
23 import org.eclipse.jface.text.contentassist.IContextInformation;
24 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
25 import org.eclipse.jface.text.templates.TemplateContextType;
26 import org.eclipse.ui.IEditorPart;
29 * Simple PHPDoc completion processor.
31 public class PHPDocCompletionProcessor implements IContentAssistProcessor {
33 // private static class PHPDocCompletionProposalComparator implements
35 // public int compare(Object o1, Object o2) {
36 // ICompletionProposal c1 = (ICompletionProposal) o1;
37 // ICompletionProposal c2 = (ICompletionProposal) o2;
38 // return c1.getDisplayString().compareTo(c2.getDisplayString());
42 // private IEditorPart fEditor;
43 // private IWorkingCopyManager fManager;
44 private char[] fProposalAutoActivationSet;
46 private PHPCompletionProposalComparator fComparator;
48 private TemplateEngine fTemplateEngine;
50 //private boolean fRestrictToMatchingCase;
52 private IEditorPart fEditor;
54 protected IWorkingCopyManager fManager;
56 public PHPDocCompletionProcessor(IEditorPart editor) {
58 fManager = WebUI.getDefault().getWorkingCopyManager();
61 // fManager= JavaPlugin.getDefault().getWorkingCopyManager();
62 TemplateContextType contextType = WebUI.getDefault()
63 .getTemplateContextRegistry().getContextType("phpdoc"); //$NON-NLS-1$
64 if (contextType != null)
65 fTemplateEngine = new TemplateEngine(contextType);
66 //fRestrictToMatchingCase = false;
68 fComparator = new PHPCompletionProposalComparator();
72 * Tells this processor to order the proposals alphabetically.
75 * <code>true</code> if proposals should be ordered.
77 public void orderProposalsAlphabetically(boolean order) {
78 fComparator.setOrderAlphabetically(order);
82 * Tells this processor to restrict is proposals to those starting with
86 * <code>true</code> if proposals should be restricted
88 public void restrictProposalsToMatchingCases(boolean restrict) {
89 //fRestrictToMatchingCase = restrict;
93 * @see IContentAssistProcessor#getErrorMessage()
95 public String getErrorMessage() {
100 * @see IContentAssistProcessor#getContextInformationValidator()
102 public IContextInformationValidator getContextInformationValidator() {
107 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
109 public char[] getContextInformationAutoActivationCharacters() {
114 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
116 public char[] getCompletionProposalAutoActivationCharacters() {
117 return fProposalAutoActivationSet;
121 * Sets this processor's set of characters triggering the activation of the
122 * completion proposal computation.
124 * @param activationSet
127 public void setCompletionProposalAutoActivationCharacters(
128 char[] activationSet) {
129 fProposalAutoActivationSet = activationSet;
133 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
135 public IContextInformation[] computeContextInformation(ITextViewer viewer,
141 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
143 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
144 int documentOffset) {
145 ICompilationUnit unit = fManager.getWorkingCopy(fEditor
147 //IDocument document = viewer.getDocument();
149 IPHPCompletionProposal[] results = new IPHPCompletionProposal[0];
152 // if (unit != null) {
154 // int offset= documentOffset;
157 // Point selection= viewer.getSelectedRange();
158 // if (selection.y > 0) {
159 // offset= selection.x;
160 // length= selection.y;
163 // JavaDocCompletionEvaluator evaluator= new
164 // JavaDocCompletionEvaluator(unit, document, offset, length);
165 // evaluator.restrictProposalsToMatchingCases(fRestrictToMatchingCase);
166 // results= evaluator.computeProposals();
168 // } catch (JavaModelException e) {
169 // JavaPlugin.log(e);
172 if (fTemplateEngine != null) {
174 fTemplateEngine.reset();
175 fTemplateEngine.complete(viewer, documentOffset, unit);
176 // } catch (JavaModelException x) {
179 IPHPCompletionProposal[] templateResults = fTemplateEngine
181 if (results.length == 0) {
182 results = templateResults;
184 // concatenate arrays
185 IPHPCompletionProposal[] total = new IPHPCompletionProposal[results.length
186 + templateResults.length];
187 System.arraycopy(templateResults, 0, total, 0,
188 templateResults.length);
189 System.arraycopy(results, 0, total, templateResults.length,
196 * Order here and not in result collector to make sure that the order
197 * applies to all proposals and not just those of the compilation unit.
199 return order(results);
203 * Order the given proposals.
205 private IPHPCompletionProposal[] order(IPHPCompletionProposal[] proposals) {
206 Arrays.sort(proposals, fComparator);