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