ea9665cf52a71321fd00a7642ad2112cc16b1021
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / template / preferences / TemplateVariableProcessor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.template.preferences;
12
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.Iterator;
17 import java.util.List;
18
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.jface.text.templates.TemplateVariableResolver;
26
27 public class TemplateVariableProcessor implements IContentAssistProcessor {
28
29         private static Comparator fgTemplateVariableProposalComparator = new Comparator() {
30                 public int compare(Object arg0, Object arg1) {
31                         TemplateVariableProposal proposal0 = (TemplateVariableProposal) arg0;
32                         TemplateVariableProposal proposal1 = (TemplateVariableProposal) arg1;
33
34                         return proposal0.getDisplayString().compareTo(
35                                         proposal1.getDisplayString());
36                 }
37
38                 public boolean equals(Object arg0) {
39                         return false;
40                 }
41         };
42
43         /** the context type */
44         private TemplateContextType fContextType;
45
46         /**
47          * Sets the context type.
48          */
49         public void setContextType(TemplateContextType contextType) {
50                 fContextType = contextType;
51         }
52
53         /**
54          * Gets the context type.
55          */
56         public TemplateContextType getContextType() {
57                 return fContextType;
58         }
59
60         /*
61          * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
62          */
63         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
64                         int documentOffset) {
65
66                 if (fContextType == null)
67                         return null;
68
69                 List proposals = new ArrayList();
70
71                 String text = viewer.getDocument().get();
72                 int start = getStart(text, documentOffset);
73                 int end = documentOffset;
74
75                 String string = text.substring(start, end);
76                 String prefix = (string.length() >= 2) ? string.substring(2) : null;
77
78                 int offset = start;
79                 int length = end - start;
80
81                 for (Iterator iterator = fContextType.resolvers(); iterator.hasNext();) {
82                         TemplateVariableResolver variable = (TemplateVariableResolver) iterator
83                                         .next();
84
85                         if (prefix == null || variable.getType().startsWith(prefix))
86                                 proposals.add(new TemplateVariableProposal(variable, offset,
87                                                 length, viewer));
88                 }
89
90                 Collections.sort(proposals, fgTemplateVariableProposalComparator);
91                 return (ICompletionProposal[]) proposals
92                                 .toArray(new ICompletionProposal[proposals.size()]);
93         }
94
95         /* Guesses the start position of the completion */
96         private int getStart(String string, int end) {
97                 int start = end;
98
99                 if (start >= 1 && string.charAt(start - 1) == '$')
100                         return start - 1;
101
102                 while ((start != 0)
103                                 && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
104                         start--;
105
106                 if (start >= 2 && string.charAt(start - 1) == '{'
107                                 && string.charAt(start - 2) == '$')
108                         return start - 2;
109
110                 return end;
111         }
112
113         /*
114          * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
115          */
116         public IContextInformation[] computeContextInformation(ITextViewer viewer,
117                         int documentOffset) {
118                 return null;
119         }
120
121         /*
122          * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
123          */
124         public char[] getCompletionProposalAutoActivationCharacters() {
125                 return new char[] { '$' };
126         }
127
128         /*
129          * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
130          */
131         public char[] getContextInformationAutoActivationCharacters() {
132                 return null;
133         }
134
135         /*
136          * @see IContentAssistProcessor#getErrorMessage()
137          */
138         public String getErrorMessage() {
139                 return null;
140         }
141
142         /*
143          * @see IContentAssistProcessor#getContextInformationValidator()
144          */
145         public IContextInformationValidator getContextInformationValidator() {
146                 return null;
147         }
148
149 }