Mercurial > projects > dwt2
comparison org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateVariable.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children | 5feec68b4556 |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.jface.text.templates.TemplateVariable; | |
14 | |
15 import org.eclipse.jface.text.templates.SimpleTemplateVariableResolver; // packageimport | |
16 import org.eclipse.jface.text.templates.TemplateBuffer; // packageimport | |
17 import org.eclipse.jface.text.templates.TemplateContext; // packageimport | |
18 import org.eclipse.jface.text.templates.TemplateContextType; // packageimport | |
19 import org.eclipse.jface.text.templates.Template; // packageimport | |
20 import org.eclipse.jface.text.templates.PositionBasedCompletionProposal; // packageimport | |
21 import org.eclipse.jface.text.templates.TemplateException; // packageimport | |
22 import org.eclipse.jface.text.templates.TemplateTranslator; // packageimport | |
23 import org.eclipse.jface.text.templates.DocumentTemplateContext; // packageimport | |
24 import org.eclipse.jface.text.templates.GlobalTemplateVariables; // packageimport | |
25 import org.eclipse.jface.text.templates.InclusivePositionUpdater; // packageimport | |
26 import org.eclipse.jface.text.templates.TemplateProposal; // packageimport | |
27 import org.eclipse.jface.text.templates.ContextTypeRegistry; // packageimport | |
28 import org.eclipse.jface.text.templates.JFaceTextTemplateMessages; // packageimport | |
29 import org.eclipse.jface.text.templates.TemplateCompletionProcessor; // packageimport | |
30 import org.eclipse.jface.text.templates.TextTemplateMessages; // packageimport | |
31 import org.eclipse.jface.text.templates.TemplateVariableType; // packageimport | |
32 import org.eclipse.jface.text.templates.TemplateVariableResolver; // packageimport | |
33 | |
34 | |
35 import java.lang.all; | |
36 import java.util.Set; | |
37 | |
38 | |
39 import org.eclipse.core.runtime.Assert; | |
40 import org.eclipse.jface.text.TextUtilities; | |
41 | |
42 /** | |
43 * A <code>TemplateVariable</code> represents a set of positions into a | |
44 * <code>TemplateBuffer</code> with identical content each. <code>TemplateVariableResolver</code>s | |
45 * can be used to resolve a template variable to a symbol available from the | |
46 * <code>TemplateContext</code>. A resolved variable may have one or more possible | |
47 * {@link #getValues() values} which may be presented to the user as choices. If there is no user | |
48 * interaction the {@link #getDefaultValue() default value} is chosen as textual representation of | |
49 * the variable. | |
50 * <p> | |
51 * Clients may instantiate and extend this class. | |
52 * </p> | |
53 * | |
54 * @see TemplateVariableResolver | |
55 * @see TemplateBuffer | |
56 * @since 3.0 | |
57 */ | |
58 public class TemplateVariable { | |
59 | |
60 /** The type name of the variable */ | |
61 private const TemplateVariableType fType; | |
62 /** The name of the variable. */ | |
63 private const String fName; | |
64 /** The initial length in the template pattern. */ | |
65 private const int fInitialLength; | |
66 /** The offsets of the variable. */ | |
67 private int[] fOffsets; | |
68 /** Flag indicating if the variable has been resolved unambiguously. */ | |
69 private bool fIsUnambiguous; | |
70 /** Flag indicating if the variable has been resolved by a resolver. */ | |
71 private bool fIsResolved; | |
72 /** | |
73 * The proposal strings available for this variable. The first string is | |
74 * the default value. | |
75 */ | |
76 private String[] fValues; | |
77 | |
78 /** | |
79 * Creates a template variable. The type is used as the name of the | |
80 * variable. | |
81 * | |
82 * @param type the type of the variable | |
83 * @param defaultValue the default value of the variable | |
84 * @param offsets the array of offsets of the variable | |
85 */ | |
86 public this(String type, String defaultValue, int[] offsets) { | |
87 this(type, [ defaultValue ], offsets); | |
88 } | |
89 | |
90 /** | |
91 * Creates a template variable. | |
92 * | |
93 * @param type the type of the variable | |
94 * @param name the name of the variable | |
95 * @param defaultValue the default value of the variable | |
96 * @param offsets the array of offsets of the variable | |
97 */ | |
98 public this(String type, String name, String defaultValue, int[] offsets) { | |
99 this(type, name, [ defaultValue ], offsets); | |
100 } | |
101 | |
102 /** | |
103 * Creates a template variable. | |
104 * | |
105 * @param type the type of the variable | |
106 * @param name the name of the variable | |
107 * @param defaultValue the default value of the variable | |
108 * @param offsets the array of offsets of the variable | |
109 * @since 3.3 | |
110 */ | |
111 public this(TemplateVariableType type, String name, String defaultValue, int[] offsets) { | |
112 this(type, name, [ defaultValue ], offsets); | |
113 } | |
114 | |
115 /** | |
116 * Creates a template variable with multiple possible values. The type is | |
117 * used as the name of the template. | |
118 * | |
119 * @param type the type of the template variable | |
120 * @param values the values available at this variable, non-empty | |
121 * @param offsets the array of offsets of the variable | |
122 */ | |
123 public this(String type, String[] values, int[] offsets) { | |
124 this(type, type, values, offsets); | |
125 } | |
126 | |
127 /** | |
128 * Creates a template variable with multiple possible values. | |
129 * | |
130 * @param type the type of the variable | |
131 * @param name the name of the variable | |
132 * @param values the values available at this variable, non-empty | |
133 * @param offsets the array of offsets of the variable | |
134 */ | |
135 public this(String type, String name, String[] values, int[] offsets) { | |
136 this(new TemplateVariableType(type), name, values, offsets); | |
137 } | |
138 | |
139 /** | |
140 * Creates a template variable with multiple possible values. | |
141 * | |
142 * @param type the type of the variable | |
143 * @param name the name of the variable | |
144 * @param values the values available at this variable, non-empty | |
145 * @param offsets the array of offsets of the variable | |
146 * @since 3.3 | |
147 */ | |
148 this(TemplateVariableType type, String name, String[] values, int[] offsets) { | |
149 Assert.isNotNull(type); | |
150 Assert.isNotNull(name); | |
151 fType= type; | |
152 fName= name; | |
153 setValues(values); | |
154 setOffsets(offsets); | |
155 setUnambiguous(false); | |
156 setResolved(false); | |
157 fInitialLength= values[0].length(); | |
158 } | |
159 | |
160 /** | |
161 * Returns the type name of the variable. | |
162 * | |
163 * @return the type name of the variable | |
164 */ | |
165 public String getType() { | |
166 return fType.getName(); | |
167 } | |
168 | |
169 /** | |
170 * Returns the type of the variable. | |
171 * | |
172 * @return the type of the variable | |
173 * @since 3.3 | |
174 */ | |
175 public TemplateVariableType getVariableType() { | |
176 return fType; | |
177 } | |
178 | |
179 /** | |
180 * Returns the name of the variable. | |
181 * | |
182 * @return the name of the variable | |
183 */ | |
184 public String getName() { | |
185 return fName; | |
186 } | |
187 | |
188 /** | |
189 * Returns the default value of the variable. Typically, this is the first of | |
190 * the possible values (see {@link #getValues()}. | |
191 * | |
192 * @return the default value of the variable | |
193 */ | |
194 public String getDefaultValue() { | |
195 return getValues()[0]; | |
196 } | |
197 | |
198 /** | |
199 * Returns the possible values for this variable. The returned array is owned by this variable | |
200 * and must not be modified. The array is not empty. | |
201 * | |
202 * @return the possible values for this variable | |
203 */ | |
204 public String[] getValues() { | |
205 return fValues; | |
206 } | |
207 | |
208 /** | |
209 * Returns the length of the variable's default value. | |
210 * | |
211 * @return the length of the variable | |
212 */ | |
213 public int getLength() { | |
214 return getDefaultValue().length(); | |
215 } | |
216 | |
217 /** | |
218 * Returns the initial length of the variable. The initial length is the lenght as it occurred | |
219 * in the template pattern and is used when resolving a template to update the pattern with the | |
220 * resolved values of the variable. | |
221 * | |
222 * @return the initial length of the variable | |
223 * @since 3.3 | |
224 */ | |
225 final int getInitialLength() { | |
226 return fInitialLength; | |
227 } | |
228 | |
229 /** | |
230 * Sets the offsets of the variable. | |
231 * | |
232 * @param offsets the new offsets of the variable | |
233 */ | |
234 public void setOffsets(int[] offsets) { | |
235 fOffsets= TextUtilities.copy(offsets); | |
236 } | |
237 | |
238 /** | |
239 * Returns the offsets of the variable. The returned array is | |
240 * owned by this variable and must not be modified. | |
241 * | |
242 * @return the length of the variable | |
243 */ | |
244 public int[] getOffsets() { | |
245 return fOffsets; | |
246 } | |
247 | |
248 /** | |
249 * Resolves the variable to a single value. This is a shortcut for | |
250 * <code>setValues(new String[] { value })</code>. | |
251 * | |
252 * @param value the new default value | |
253 */ | |
254 public final void setValue(String value) { | |
255 setValues([ value ]); | |
256 } | |
257 | |
258 /** | |
259 * Resolves the variable to several possible values for this variable, with the first being the | |
260 * default value. | |
261 * | |
262 * @param values a non-empty array of values | |
263 */ | |
264 public void setValues(String[] values) { | |
265 Assert.isTrue(values.length > 0); | |
266 fValues= TextUtilities.copy(values); | |
267 setResolved(true); | |
268 } | |
269 | |
270 /** | |
271 * Sets the <em>isUnambiguous</em> flag of the variable. | |
272 * | |
273 * @param unambiguous the new unambiguous state of the variable | |
274 */ | |
275 public void setUnambiguous(bool unambiguous) { | |
276 fIsUnambiguous= unambiguous; | |
277 if (unambiguous) | |
278 setResolved(true); | |
279 } | |
280 | |
281 /** | |
282 * Returns <code>true</code> if the variable is unambiguously resolved, <code>false</code> otherwise. | |
283 * | |
284 * @return <code>true</code> if the variable is unambiguously resolved, <code>false</code> otherwise | |
285 */ | |
286 public bool isUnambiguous() { | |
287 return fIsUnambiguous; | |
288 } | |
289 | |
290 /** | |
291 * Sets the <em>resolved</em> flag of the variable. | |
292 * | |
293 * @param resolved the new <em>resolved</em> state | |
294 * @since 3.3 | |
295 */ | |
296 public void setResolved(bool resolved) { | |
297 fIsResolved= resolved; | |
298 } | |
299 | |
300 /** | |
301 * Returns <code>true</code> if the variable has been resolved, <code>false</code> | |
302 * otherwise. | |
303 * | |
304 * @return <code>true</code> if the variable has been resolved, <code>false</code> otherwise | |
305 * @since 3.3 | |
306 */ | |
307 public bool isResolved() { | |
308 return fIsResolved; | |
309 } | |
310 } |