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.DocumentTemplateContext;
|
|
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.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.BadLocationException;
|
|
40 import dwtx.jface.text.IDocument;
|
|
41 import dwtx.jface.text.Position;
|
|
42
|
|
43 /**
|
|
44 * Instances of this class describe the context of a template as a region of
|
|
45 * a document. That region may be either specified by its offset and length, or
|
|
46 * by a <code>Position</code> which may or may not be registered with the
|
|
47 * document.
|
|
48 * <p>
|
|
49 * Clients may instantiate and extend this class.
|
|
50 * </p>
|
|
51 *
|
|
52 * @since 3.0
|
|
53 */
|
|
54 public class DocumentTemplateContext : TemplateContext {
|
|
55
|
|
56 /** The text of the document. */
|
|
57 private final IDocument fDocument;
|
|
58 /**
|
|
59 * The region of the document described by this context. We store a
|
|
60 * position since clients may specify the document region as (updateable)
|
|
61 * Positions.
|
|
62 */
|
|
63 private final Position fPosition;
|
|
64 /**
|
|
65 * The original offset of this context. Will only be updated by the setter
|
|
66 * method.
|
|
67 */
|
|
68 private int fOriginalOffset;
|
|
69 /**
|
|
70 * The original length of this context. Will only be updated by the setter
|
|
71 * method.
|
|
72 */
|
|
73 private int fOriginalLength;
|
|
74
|
|
75 /**
|
|
76 * Creates a document template context.
|
|
77 *
|
|
78 * @param type the context type
|
|
79 * @param document the document this context applies to
|
|
80 * @param offset the offset of the document region
|
|
81 * @param length the length of the document region
|
|
82 */
|
|
83 public DocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length) {
|
|
84 this(type, document, new Position(offset, length));
|
|
85 }
|
|
86
|
|
87 /**
|
|
88 * Creates a document template context. The supplied <code>Position</code>
|
|
89 * will be queried to compute the <code>getStart</code> and
|
|
90 * <code>getEnd</code> methods, which will therefore answer updated
|
|
91 * position data if it is registered with the document.
|
|
92 *
|
|
93 * @param type the context type
|
|
94 * @param document the document this context applies to
|
|
95 * @param position the position describing the area of the document which
|
|
96 * forms the template context
|
|
97 * @since 3.1
|
|
98 */
|
|
99 public DocumentTemplateContext(TemplateContextType type, IDocument document, Position position) {
|
|
100 super(type);
|
|
101
|
|
102 Assert.isNotNull(document);
|
|
103 Assert.isNotNull(position);
|
|
104 Assert.isTrue(position.getOffset() <= document.getLength());
|
|
105
|
|
106 fDocument= document;
|
|
107 fPosition= position;
|
|
108 fOriginalOffset= fPosition.getOffset();
|
|
109 fOriginalLength= fPosition.getLength();
|
|
110 }
|
|
111
|
|
112 /**
|
|
113 * Returns the document.
|
|
114 *
|
|
115 * @return the document
|
|
116 */
|
|
117 public IDocument getDocument() {
|
|
118 return fDocument;
|
|
119 }
|
|
120
|
|
121 /**
|
|
122 * Returns the completion offset within the string of the context.
|
|
123 *
|
|
124 * @return the completion offset within the string of the context
|
|
125 */
|
|
126 public int getCompletionOffset() {
|
|
127 return fOriginalOffset;
|
|
128 }
|
|
129
|
|
130 /**
|
|
131 * Sets the completion offset.
|
|
132 *
|
|
133 * @param newOffset the new completion offset
|
|
134 */
|
|
135 protected void setCompletionOffset(int newOffset) {
|
|
136 fOriginalOffset= newOffset;
|
|
137 fPosition.setOffset(newOffset);
|
|
138 }
|
|
139
|
|
140 /**
|
|
141 * Returns the completion length within the string of the context.
|
|
142 *
|
|
143 * @return the completion length within the string of the context
|
|
144 */
|
|
145 public int getCompletionLength() {
|
|
146 return fOriginalLength;
|
|
147 }
|
|
148
|
|
149 /**
|
|
150 * Sets the completion length.
|
|
151 *
|
|
152 * @param newLength the new completion length
|
|
153 */
|
|
154 protected void setCompletionLength(int newLength) {
|
|
155 fOriginalLength= newLength;
|
|
156 fPosition.setLength(newLength);
|
|
157 }
|
|
158
|
|
159 /**
|
|
160 * Returns the keyword which triggered template insertion.
|
|
161 *
|
|
162 * @return the keyword which triggered template insertion
|
|
163 */
|
|
164 public String getKey() {
|
|
165 int offset= getStart();
|
|
166 int length= getEnd() - offset;
|
|
167 try {
|
|
168 return fDocument.get(offset, length);
|
|
169 } catch (BadLocationException e) {
|
|
170 return ""; //$NON-NLS-1$
|
|
171 }
|
|
172 }
|
|
173
|
|
174 /**
|
|
175 * Returns the beginning offset of the keyword.
|
|
176 *
|
|
177 * @return the beginning offset of the keyword
|
|
178 */
|
|
179 public int getStart() {
|
|
180 return fPosition.getOffset();
|
|
181 }
|
|
182
|
|
183 /**
|
|
184 * Returns the end offset of the keyword.
|
|
185 *
|
|
186 * @return the end offset of the keyword
|
|
187 */
|
|
188 public int getEnd() {
|
|
189 return fPosition.getOffset() + fPosition.getLength();
|
|
190 }
|
|
191
|
|
192 /*
|
|
193 * @see dwtx.jface.text.templates.TemplateContext#canEvaluate(dwtx.jface.text.templates.Template)
|
|
194 */
|
|
195 public bool canEvaluate(Template template) {
|
|
196 return true;
|
|
197 }
|
|
198
|
|
199 /*
|
|
200 * @see dwtx.jface.text.templates.TemplateContext#evaluate(dwtx.jface.text.templates.Template)
|
|
201 */
|
|
202 public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
|
|
203 if (!canEvaluate(template))
|
|
204 return null;
|
|
205
|
|
206 TemplateTranslator translator= new TemplateTranslator();
|
|
207 TemplateBuffer buffer= translator.translate(template);
|
|
208
|
|
209 getContextType().resolve(buffer, this);
|
|
210
|
|
211 return buffer;
|
|
212 }
|
|
213 }
|