4f55f8a3b8b36b8ac57728dc235b03390f108fe3
[phpeclipse.git] /
1 package net.sourceforge.phpdt.internal.ui.text.template;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.Iterator;
7 import java.util.List;
8
9 import net.sourceforge.phpdt.internal.corext.template.ContextType;
10 import net.sourceforge.phpdt.internal.corext.template.TemplateVariable;
11
12 import org.eclipse.jface.text.ITextViewer;
13 import org.eclipse.jface.text.contentassist.ICompletionProposal;
14 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
15 import org.eclipse.jface.text.contentassist.IContextInformation;
16 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
17
18 public class TemplateVariableProcessor implements IContentAssistProcessor {     
19
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;
24                         
25                         return proposal0.getDisplayString().compareTo(proposal1.getDisplayString());
26                 }
27
28                 public boolean equals(Object arg0) {
29                         return false;
30                 }
31         };
32
33         
34         /** the context type */
35         private ContextType fContextType;
36         
37         /**
38          * Sets the context type.
39          */
40         public void setContextType(ContextType contextType) {
41                 fContextType= contextType;      
42         }
43         
44         /*
45          * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
46          */
47         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,     int documentOffset) {
48
49                 if (fContextType == null)
50                         return null;
51
52                 List proposals= new ArrayList();                
53                 
54                 String text= viewer.getDocument().get();
55                 int start= getStart(text, documentOffset);
56                 int end= documentOffset;
57
58                 String string= text.substring(start, end);
59                 String prefix= (string.length() >= 2)
60                         ? string.substring(2)
61                         : null;
62
63                 int offset= start;
64                 int length= end - start;
65
66                 for (Iterator iterator= fContextType.variableIterator(); iterator.hasNext(); ) {
67                         TemplateVariable variable= (TemplateVariable) iterator.next();
68
69                         if (prefix == null || variable.getName().startsWith(prefix))
70                                 proposals.add(new TemplateVariableProposal(variable, offset, length, viewer));
71                 }
72
73                 Collections.sort(proposals, fgTemplateVariableProposalComparator);
74                 return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
75         }
76
77         /* Guesses the start position of the completion */
78         private int getStart(String string, int end) {
79                 int start= end;
80
81                 if (start >= 1 && string.charAt(start - 1) == '$')
82                         return start - 1;
83                                 
84                 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
85                         start--;
86
87                 if (start >= 2 && string.charAt(start - 1) == '{' && string.charAt(start - 2) == '$')
88                         return start - 2;
89                         
90                 return end;
91         }
92
93         /*
94          * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
95          */
96         public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
97                 return null;
98         }
99
100         /*
101          * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
102          */
103         public char[] getCompletionProposalAutoActivationCharacters() {
104                 return new char[] {'$'};
105         }
106
107         /*
108          * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
109          */
110         public char[] getContextInformationAutoActivationCharacters() {
111                 return null;
112         }
113
114         /*
115          * @see IContentAssistProcessor#getErrorMessage()
116          */
117         public String getErrorMessage() {
118                 return null;
119         }
120
121         /*
122          * @see IContentAssistProcessor#getContextInformationValidator()
123          */
124         public IContextInformationValidator getContextInformationValidator() {
125                 return null;
126         }
127
128 }
129