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