Mercurial > projects > dwt-addons
annotate dwtx/jface/text/templates/TemplateContextType.d @ 134:51e6e63f930e
Regex fix for casts
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 24 Aug 2008 01:46:20 +0200 |
parents | 7d818bd32d63 |
children | 6dcb0baaa031 |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 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.TemplateContextType; | |
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.Template; // packageimport | |
19 import dwtx.jface.text.templates.TemplateVariable; // 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 import java.util.ArrayList; | |
38 import java.util.Collections; | |
39 import java.util.HashMap; | |
40 import java.util.Iterator; | |
41 import java.util.List; | |
42 import java.util.Map; | |
43 | |
44 import dwtx.core.runtime.Assert; | |
45 import dwtx.jface.text.BadLocationException; | |
46 import dwtx.jface.text.Document; | |
47 import dwtx.jface.text.IDocument; | |
48 import dwtx.text.edits.MalformedTreeException; | |
49 import dwtx.text.edits.MultiTextEdit; | |
50 import dwtx.text.edits.RangeMarker; | |
51 import dwtx.text.edits.ReplaceEdit; | |
52 import dwtx.text.edits.TextEdit; | |
53 | |
54 | |
55 /** | |
56 * A context type defines a context within which templates are resolved. It | |
57 * stores a number of <code>TemplateVariableResolver</code>s. A | |
58 * <code>TemplateBuffer</code> can be resolved in a | |
59 * <code>TemplateContext</code> using the | |
60 * {@link #resolve(TemplateBuffer, TemplateContext)} method. | |
61 * <p> | |
62 * Clients may extend this class. | |
63 * </p> | |
64 * | |
65 * @since 3.0 | |
66 */ | |
67 public class TemplateContextType { | |
68 | |
69 /** The id of the context type. */ | |
70 private /* final */ String fId= null; | |
71 | |
72 /** Variable resolvers used by this content type. */ | |
73 private final Map fResolvers= new HashMap(); | |
74 | |
75 /** The name of the context type. */ | |
76 private String fName= null; | |
77 | |
78 /** | |
79 * Creates a context type with an identifier. The identifier must be unique, | |
80 * a qualified name is suggested. The id is also used as name. | |
81 * | |
82 * @param id the unique identifier of the context type | |
83 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
84 public this(String id) { |
129 | 85 this(id, id); |
86 } | |
87 | |
88 /** | |
89 * Creates a context type with an identifier. The identifier must be unique, a qualified name is suggested. | |
90 * | |
91 * @param id the unique identifier of the context type | |
92 * @param name the name of the context type | |
93 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
94 public this(String id, String name) { |
129 | 95 Assert.isNotNull(id); |
96 Assert.isNotNull(name); | |
97 fId= id; | |
98 fName= name; | |
99 } | |
100 | |
101 /** | |
102 * Returns the id of the context type. | |
103 * | |
104 * @return the id of the receiver | |
105 */ | |
106 public String getId() { | |
107 return fId; | |
108 } | |
109 | |
110 | |
111 /** | |
112 * Returns the name of the context type. | |
113 * | |
114 * @return the name of the context type | |
115 */ | |
116 public String getName() { | |
117 return fName; | |
118 } | |
119 | |
120 /** | |
121 * Creates a context type with a <code>null</code> identifier. | |
122 * <p> | |
123 * This is a framework-only constructor that exists only so that context | |
124 * types can be contributed via an extension point and that should not be | |
125 * called in client code except for subclass constructors; use | |
126 * {@link #TemplateContextType(String)} instead. | |
127 * </p> | |
128 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
129 public this() { |
129 | 130 } |
131 | |
132 /** | |
133 * Sets the id of this context. | |
134 * <p> | |
135 * This is a framework-only method that exists solely so that context types | |
136 * can be contributed via an extension point and that should not be called | |
137 * in client code; use {@link #TemplateContextType(String)} instead. | |
138 * </p> | |
139 * | |
140 * @param id the identifier of this context | |
141 * @throws RuntimeException an unspecified exception if the id has already | |
142 * been set on this context type | |
143 */ | |
144 public final void setId(String id) throws RuntimeException { | |
145 Assert.isNotNull(id); | |
146 Assert.isTrue(fId is null); // may only be called once when the context is instantiated | |
147 fId= id; | |
148 } | |
149 | |
150 /** | |
151 * Sets the name of the context type. | |
152 * | |
153 * <p> | |
154 * This is a framework-only method that exists solely so that context types | |
155 * can be contributed via an extension point and that should not be called | |
156 * in client code; use {@link #TemplateContextType(String, String)} instead. | |
157 * </p> | |
158 * | |
159 * @param name the name of the context type | |
160 */ | |
161 public final void setName(String name) { | |
162 Assert.isTrue(fName is null); // only initialized by extension code | |
163 fName= name; | |
164 } | |
165 | |
166 /** | |
167 * Adds a variable resolver to the context type. If there already is a resolver | |
168 * for the same type, the previous one gets replaced by <code>resolver</code>. | |
169 * | |
170 * @param resolver the resolver to be added under its name | |
171 */ | |
172 public void addResolver(TemplateVariableResolver resolver) { | |
173 Assert.isNotNull(resolver); | |
174 fResolvers.put(resolver.getType(), resolver); | |
175 } | |
176 | |
177 /** | |
178 * Removes a template variable from the context type. | |
179 * | |
180 * @param resolver the variable to be removed | |
181 */ | |
182 public void removeResolver(TemplateVariableResolver resolver) { | |
183 Assert.isNotNull(resolver); | |
184 fResolvers.remove(resolver.getType()); | |
185 } | |
186 | |
187 /** | |
188 * Removes all template variables from the context type. | |
189 */ | |
190 public void removeAllResolvers() { | |
191 fResolvers.clear(); | |
192 } | |
193 | |
194 /** | |
195 * Returns an iterator for the variables known to the context type. | |
196 * | |
197 * @return an iterator over the variables in this context type | |
198 */ | |
199 public Iterator resolvers() { | |
200 return Collections.unmodifiableMap(fResolvers).values().iterator(); | |
201 } | |
202 | |
203 /** | |
204 * Returns the resolver for the given type. | |
205 * | |
206 * @param type the type for which a resolver is needed | |
207 * @return a resolver for the given type, or <code>null</code> if none is registered | |
208 */ | |
209 protected TemplateVariableResolver getResolver(String type) { | |
134 | 210 return cast(TemplateVariableResolver) fResolvers.get(type); |
129 | 211 } |
212 | |
213 /** | |
214 * Validates a pattern, a <code>TemplateException</code> is thrown if | |
215 * validation fails. | |
216 * | |
217 * @param pattern the template pattern to validate | |
218 * @throws TemplateException if the pattern is invalid | |
219 */ | |
220 public void validate(String pattern) throws TemplateException { | |
221 TemplateTranslator translator= new TemplateTranslator(); | |
222 TemplateBuffer buffer= translator.translate(pattern); | |
223 validateVariables(buffer.getVariables()); | |
224 } | |
225 | |
226 /** | |
227 * Validates the variables in this context type. If a variable is not valid, | |
228 * e.g. if its type is not known in this context type, a | |
229 * <code>TemplateException</code> is thrown. | |
230 * <p> | |
231 * The default implementation does nothing. | |
232 * </p> | |
233 * | |
234 * @param variables the variables to validate | |
235 * @throws TemplateException if one of the variables is not valid in this | |
236 * context type | |
237 */ | |
238 protected void validateVariables(TemplateVariable[] variables) throws TemplateException { | |
239 } | |
240 | |
241 /** | |
242 * Resolves the variables in <code>buffer</code> within <code>context</code> | |
243 * and edits the template buffer to reflect the resolved variables. | |
244 * | |
245 * @param buffer the template buffer | |
246 * @param context the template context | |
247 * @throws MalformedTreeException if the positions in the buffer overlap | |
248 * @throws BadLocationException if the buffer cannot be successfully modified | |
249 */ | |
250 public void resolve(TemplateBuffer buffer, TemplateContext context) throws MalformedTreeException, BadLocationException { | |
251 Assert.isNotNull(context); | |
252 TemplateVariable[] variables= buffer.getVariables(); | |
253 | |
254 List positions= variablesToPositions(variables); | |
255 List edits= new ArrayList(5); | |
256 | |
257 // iterate over all variables and try to resolve them | |
258 for (int i= 0; i !is variables.length; i++) { | |
259 TemplateVariable variable= variables[i]; | |
260 | |
261 if (!variable.isResolved()) | |
262 resolve(variable, context); | |
263 | |
264 String value= variable.getDefaultValue(); | |
265 int[] offsets= variable.getOffsets(); | |
266 // update buffer to reflect new value | |
267 for (int k= 0; k !is offsets.length; k++) | |
268 edits.add(new ReplaceEdit(offsets[k], variable.getInitialLength(), value)); | |
269 | |
270 } | |
271 | |
272 IDocument document= new Document(buffer.getString()); | |
273 MultiTextEdit edit= new MultiTextEdit(0, document.getLength()); | |
274 edit.addChildren((TextEdit[]) positions.toArray(new TextEdit[positions.size()])); | |
275 edit.addChildren((TextEdit[]) edits.toArray(new TextEdit[edits.size()])); | |
276 edit.apply(document, TextEdit.UPDATE_REGIONS); | |
277 | |
278 positionsToVariables(positions, variables); | |
279 | |
280 buffer.setContent(document.get(), variables); | |
281 } | |
282 | |
283 /** | |
284 * Resolves a single variable in a context. Resolving is delegated to the registered resolver. | |
285 * | |
286 * @param variable the variable to resolve | |
287 * @param context the context in which to resolve the variable | |
288 * @since 3.3 | |
289 */ | |
290 public void resolve(TemplateVariable variable, TemplateContext context) { | |
291 String type= variable.getType(); | |
134 | 292 TemplateVariableResolver resolver= cast(TemplateVariableResolver) fResolvers.get(type); |
129 | 293 if (resolver is null) |
294 resolver= new TemplateVariableResolver(type, ""); //$NON-NLS-1$ | |
295 resolver.resolve(variable, context); | |
296 } | |
297 | |
298 private static List variablesToPositions(TemplateVariable[] variables) { | |
299 List positions= new ArrayList(5); | |
300 for (int i= 0; i !is variables.length; i++) { | |
301 int[] offsets= variables[i].getOffsets(); | |
302 for (int j= 0; j !is offsets.length; j++) | |
303 positions.add(new RangeMarker(offsets[j], 0)); | |
304 } | |
305 | |
306 return positions; | |
307 } | |
308 | |
309 private static void positionsToVariables(List positions, TemplateVariable[] variables) { | |
310 Iterator iterator= positions.iterator(); | |
311 | |
312 for (int i= 0; i !is variables.length; i++) { | |
313 TemplateVariable variable= variables[i]; | |
314 | |
315 int[] offsets= new int[variable.getOffsets().length]; | |
316 for (int j= 0; j !is offsets.length; j++) | |
134 | 317 offsets[j]= (cast(TextEdit) iterator.next()).getOffset(); |
129 | 318 |
319 variable.setOffsets(offsets); | |
320 } | |
321 } | |
322 } |