1 package net.sourceforge.phpdt.internal.ui.text.template;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.Iterator;
9 import org.eclipse.jface.text.ITextViewer;
10 import org.eclipse.jface.text.contentassist.ICompletionProposal;
11 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
12 import org.eclipse.jface.text.contentassist.IContextInformation;
13 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
15 import net.sourceforge.phpdt.internal.corext.template.ContextType;
16 import net.sourceforge.phpdt.internal.corext.template.TemplateVariable;
18 public class TemplateVariableProcessor implements IContentAssistProcessor {
20 private static Comparator fgTemplateVariableProposalComparator= new Comparator() {
21 public int compare(Object arg0, Object arg1) {
22 TemplateVariableProposal proposal0= (TemplateVariableProposal) arg0;
23 TemplateVariableProposal proposal1= (TemplateVariableProposal) arg1;
25 return proposal0.getDisplayString().compareTo(proposal1.getDisplayString());
28 public boolean equals(Object arg0) {
34 /** the context type */
35 private ContextType fContextType;
38 * Sets the context type.
40 public void setContextType(ContextType contextType) {
41 fContextType= contextType;
45 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
47 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
49 if (fContextType == null)
52 List proposals= new ArrayList();
54 String text= viewer.getDocument().get();
55 int start= getStart(text, documentOffset);
56 int end= documentOffset;
58 String string= text.substring(start, end);
59 String prefix= (string.length() >= 2)
64 int length= end - start;
66 for (Iterator iterator= fContextType.variableIterator(); iterator.hasNext(); ) {
67 TemplateVariable variable= (TemplateVariable) iterator.next();
69 if (prefix == null || variable.getName().startsWith(prefix))
70 proposals.add(new TemplateVariableProposal(variable, offset, length, viewer));
73 Collections.sort(proposals, fgTemplateVariableProposalComparator);
74 return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
77 /* Guesses the start position of the completion */
78 private int getStart(String string, int end) {
81 if (start >= 1 && string.charAt(start - 1) == '$')
84 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
87 if (start >= 2 && string.charAt(start - 1) == '{' && string.charAt(start - 2) == '$')
94 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
96 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
101 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
103 public char[] getCompletionProposalAutoActivationCharacters() {
104 return new char[] {'$'};
108 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
110 public char[] getContextInformationAutoActivationCharacters() {
115 * @see IContentAssistProcessor#getErrorMessage()
117 public String getErrorMessage() {
122 * @see IContentAssistProcessor#getContextInformationValidator()
124 public IContextInformationValidator getContextInformationValidator() {