129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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.TemplateVariableResolver;
|
|
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.TemplateVariable; // packageimport
|
|
21 import dwtx.jface.text.templates.PositionBasedCompletionProposal; // packageimport
|
|
22 import dwtx.jface.text.templates.TemplateException; // packageimport
|
|
23 import dwtx.jface.text.templates.TemplateTranslator; // packageimport
|
|
24 import dwtx.jface.text.templates.DocumentTemplateContext; // packageimport
|
|
25 import dwtx.jface.text.templates.GlobalTemplateVariables; // packageimport
|
|
26 import dwtx.jface.text.templates.InclusivePositionUpdater; // packageimport
|
|
27 import dwtx.jface.text.templates.TemplateProposal; // packageimport
|
|
28 import dwtx.jface.text.templates.ContextTypeRegistry; // packageimport
|
|
29 import dwtx.jface.text.templates.JFaceTextTemplateMessages; // packageimport
|
|
30 import dwtx.jface.text.templates.TemplateCompletionProcessor; // packageimport
|
|
31 import dwtx.jface.text.templates.TextTemplateMessages; // packageimport
|
|
32 import dwtx.jface.text.templates.TemplateVariableType; // packageimport
|
|
33
|
|
34
|
129
|
35 import dwt.dwthelper.utils;
|
|
36
|
|
37 import dwtx.core.runtime.Assert;
|
|
38
|
|
39 /**
|
|
40 * A <code>TemplateVariableResolver</code> resolves <code>TemplateVariables</code>
|
|
41 * of a certain type inside a <code>TemplateContext</code>.
|
|
42 * <p>
|
|
43 * Clients may instantiate and extend this class.
|
|
44 * </p>
|
|
45 *
|
|
46 * @see TemplateVariable
|
|
47 * @since 3.0
|
|
48 */
|
|
49 public class TemplateVariableResolver {
|
|
50
|
|
51 /** Type of this resolver. */
|
|
52 private String fType= null;
|
|
53
|
|
54 /** Description of the type resolved by this resolver. */
|
|
55 private String fDescription= null;
|
|
56
|
|
57 /**
|
|
58 * Creates an instance of <code>TemplateVariableResolver</code>.
|
|
59 *
|
|
60 * @param type the name of the type
|
|
61 * @param description the description for the type
|
|
62 */
|
|
63 protected TemplateVariableResolver(String type, String description) {
|
|
64 setType(type);
|
|
65 setDescription(description);
|
|
66 }
|
|
67
|
|
68 /**
|
|
69 * Creates an empty instance.
|
|
70 * <p>
|
|
71 * This is a framework-only constructor that exists only so that resolvers
|
|
72 * can be contributed via an extension point and that should not be called
|
|
73 * in client code except for subclass constructors; use
|
|
74 * {@link #TemplateVariableResolver(String, String)} instead.
|
|
75 * </p>
|
|
76 */
|
|
77 public TemplateVariableResolver() {
|
|
78 }
|
|
79
|
|
80 /**
|
|
81 * Returns the type of this resolver.
|
|
82 *
|
|
83 * @return the type
|
|
84 */
|
|
85 public String getType() {
|
|
86 return fType;
|
|
87 }
|
|
88
|
|
89 /**
|
|
90 * Returns the description for the resolver.
|
|
91 *
|
|
92 * @return the description for the resolver
|
|
93 */
|
|
94 public String getDescription() {
|
|
95 return fDescription;
|
|
96 }
|
|
97
|
|
98 /**
|
|
99 * Returns an instance of the type resolved by the receiver available in <code>context</code>.
|
|
100 * To resolve means to provide a binding to a concrete text object (a
|
|
101 * <code>String</code>) in the given context.
|
|
102 * <p>
|
|
103 * The default implementation looks up the type in the context.</p>
|
|
104 *
|
|
105 * @param context the context in which to resolve the type
|
|
106 * @return the name of the text object of this type, or <code>null</code> if it cannot be determined
|
|
107 */
|
|
108 protected String resolve(TemplateContext context) {
|
|
109 return context.getVariable(getType());
|
|
110 }
|
|
111
|
|
112 /**
|
|
113 * Returns all possible bindings available in <code>context</code>. The default
|
|
114 * implementation simply returns an array which contains the result of
|
|
115 * {@link #resolve(TemplateContext)}, or an empty array if that call returns
|
|
116 * <code>null</code>.
|
|
117 *
|
|
118 * @param context the context in which to resolve the type
|
|
119 * @return an array of possible bindings of this type in <code>context</code>
|
|
120 */
|
|
121 protected String[] resolveAll(TemplateContext context) {
|
|
122 String binding= resolve(context);
|
|
123 if (binding is null)
|
|
124 return new String[0];
|
|
125 return new String[] { binding };
|
|
126 }
|
|
127
|
|
128 /**
|
|
129 * Resolves <code>variable</code> in <code>context</code>. To resolve
|
|
130 * means to find a valid binding of the receiver's type in the given <code>TemplateContext</code>.
|
|
131 * If the variable can be successfully resolved, its value is set using
|
|
132 * {@link TemplateVariable#setValues(String[])}.
|
|
133 *
|
|
134 * @param context the context in which variable is resolved
|
|
135 * @param variable the variable to resolve
|
|
136 */
|
|
137 public void resolve(TemplateVariable variable, TemplateContext context) {
|
|
138 String[] bindings= resolveAll(context);
|
|
139 if (bindings.length !is 0)
|
|
140 variable.setValues(bindings);
|
|
141 if (bindings.length > 1)
|
|
142 variable.setUnambiguous(false);
|
|
143 else
|
|
144 variable.setUnambiguous(isUnambiguous(context));
|
|
145 variable.setResolved(true);
|
|
146 }
|
|
147
|
|
148 /**
|
|
149 * Returns whether this resolver is able to resolve unambiguously. When
|
|
150 * resolving a <code>TemplateVariable</code>, its <code>isUmambiguous</code>
|
|
151 * state is set to the one of this resolver. By default, this method
|
|
152 * returns <code>false</code>. Clients can overwrite this method to give
|
|
153 * a hint about whether there should be e.g. prompting for input values for
|
|
154 * ambiguous variables.
|
|
155 *
|
|
156 * @param context the context in which the resolved check should be
|
|
157 * evaluated
|
|
158 * @return <code>true</code> if the receiver is unambiguously resolvable
|
|
159 * in <code>context</code>, <code>false</code> otherwise
|
|
160 */
|
|
161 protected bool isUnambiguous(TemplateContext context) {
|
|
162 return false;
|
|
163 }
|
|
164
|
|
165 /**
|
|
166 * Sets the description.
|
|
167 * <p>
|
|
168 * This is a framework-only method that exists only so that resolvers
|
|
169 * can be contributed via an extension point and that should not be called
|
|
170 * in client code; use {@link #TemplateVariableResolver(String, String)} instead.
|
|
171 * </p>
|
|
172 *
|
|
173 * @param description the description of this resolver
|
|
174 */
|
|
175 public final void setDescription(String description) {
|
|
176 Assert.isNotNull(description);
|
|
177 Assert.isTrue(fDescription is null); // may only be called once when initialized
|
|
178 fDescription= description;
|
|
179 }
|
|
180
|
|
181 /**
|
|
182 * Sets the type name.
|
|
183 * <p>
|
|
184 * This is a framework-only method that exists only so that resolvers
|
|
185 * can be contributed via an extension point and that should not be called
|
|
186 * in client code; use {@link #TemplateVariableResolver(String, String)} instead.
|
|
187 * </p>
|
|
188 *
|
|
189 * @param type the type name of this resolver
|
|
190 */
|
|
191 public final void setType(String type) {
|
|
192 Assert.isNotNull(type);
|
|
193 Assert.isTrue(fType is null); // may only be called once when initialized
|
|
194 fType= type;
|
|
195 }
|
|
196 }
|