Mercurial > projects > dwt2
comparison org.eclipse.text/src/org/eclipse/jface/text/templates/DocumentTemplateContext.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children | 5feec68b4556 |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
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 org.eclipse.jface.text.templates.DocumentTemplateContext; | |
14 | |
15 import org.eclipse.jface.text.templates.SimpleTemplateVariableResolver; // packageimport | |
16 import org.eclipse.jface.text.templates.TemplateBuffer; // packageimport | |
17 import org.eclipse.jface.text.templates.TemplateContext; // packageimport | |
18 import org.eclipse.jface.text.templates.TemplateContextType; // packageimport | |
19 import org.eclipse.jface.text.templates.Template; // packageimport | |
20 import org.eclipse.jface.text.templates.TemplateVariable; // packageimport | |
21 import org.eclipse.jface.text.templates.PositionBasedCompletionProposal; // packageimport | |
22 import org.eclipse.jface.text.templates.TemplateException; // packageimport | |
23 import org.eclipse.jface.text.templates.TemplateTranslator; // packageimport | |
24 import org.eclipse.jface.text.templates.GlobalTemplateVariables; // packageimport | |
25 import org.eclipse.jface.text.templates.InclusivePositionUpdater; // packageimport | |
26 import org.eclipse.jface.text.templates.TemplateProposal; // packageimport | |
27 import org.eclipse.jface.text.templates.ContextTypeRegistry; // packageimport | |
28 import org.eclipse.jface.text.templates.JFaceTextTemplateMessages; // packageimport | |
29 import org.eclipse.jface.text.templates.TemplateCompletionProcessor; // packageimport | |
30 import org.eclipse.jface.text.templates.TextTemplateMessages; // packageimport | |
31 import org.eclipse.jface.text.templates.TemplateVariableType; // packageimport | |
32 import org.eclipse.jface.text.templates.TemplateVariableResolver; // packageimport | |
33 | |
34 | |
35 import java.lang.all; | |
36 import java.util.Set; | |
37 | |
38 | |
39 import org.eclipse.core.runtime.Assert; | |
40 import org.eclipse.jface.text.BadLocationException; | |
41 import org.eclipse.jface.text.IDocument; | |
42 import org.eclipse.jface.text.Position; | |
43 | |
44 /** | |
45 * Instances of this class describe the context of a template as a region of | |
46 * a document. That region may be either specified by its offset and length, or | |
47 * by a <code>Position</code> which may or may not be registered with the | |
48 * document. | |
49 * <p> | |
50 * Clients may instantiate and extend this class. | |
51 * </p> | |
52 * | |
53 * @since 3.0 | |
54 */ | |
55 public class DocumentTemplateContext : TemplateContext { | |
56 | |
57 /** The text of the document. */ | |
58 private const IDocument fDocument; | |
59 /** | |
60 * The region of the document described by this context. We store a | |
61 * position since clients may specify the document region as (updateable) | |
62 * Positions. | |
63 */ | |
64 private const Position fPosition; | |
65 /** | |
66 * The original offset of this context. Will only be updated by the setter | |
67 * method. | |
68 */ | |
69 private int fOriginalOffset; | |
70 /** | |
71 * The original length of this context. Will only be updated by the setter | |
72 * method. | |
73 */ | |
74 private int fOriginalLength; | |
75 | |
76 /** | |
77 * Creates a document template context. | |
78 * | |
79 * @param type the context type | |
80 * @param document the document this context applies to | |
81 * @param offset the offset of the document region | |
82 * @param length the length of the document region | |
83 */ | |
84 public this(TemplateContextType type, IDocument document, int offset, int length) { | |
85 this(type, document, new Position(offset, length)); | |
86 } | |
87 | |
88 /** | |
89 * Creates a document template context. The supplied <code>Position</code> | |
90 * will be queried to compute the <code>getStart</code> and | |
91 * <code>getEnd</code> methods, which will therefore answer updated | |
92 * position data if it is registered with the document. | |
93 * | |
94 * @param type the context type | |
95 * @param document the document this context applies to | |
96 * @param position the position describing the area of the document which | |
97 * forms the template context | |
98 * @since 3.1 | |
99 */ | |
100 public this(TemplateContextType type, IDocument document, Position position) { | |
101 super(type); | |
102 | |
103 Assert.isNotNull(cast(Object)document); | |
104 Assert.isNotNull(position); | |
105 Assert.isTrue(position.getOffset() <= document.getLength()); | |
106 | |
107 fDocument= document; | |
108 fPosition= position; | |
109 fOriginalOffset= fPosition.getOffset(); | |
110 fOriginalLength= fPosition.getLength(); | |
111 } | |
112 | |
113 /** | |
114 * Returns the document. | |
115 * | |
116 * @return the document | |
117 */ | |
118 public IDocument getDocument() { | |
119 return fDocument; | |
120 } | |
121 | |
122 /** | |
123 * Returns the completion offset within the string of the context. | |
124 * | |
125 * @return the completion offset within the string of the context | |
126 */ | |
127 public int getCompletionOffset() { | |
128 return fOriginalOffset; | |
129 } | |
130 | |
131 /** | |
132 * Sets the completion offset. | |
133 * | |
134 * @param newOffset the new completion offset | |
135 */ | |
136 protected void setCompletionOffset(int newOffset) { | |
137 fOriginalOffset= newOffset; | |
138 fPosition.setOffset(newOffset); | |
139 } | |
140 | |
141 /** | |
142 * Returns the completion length within the string of the context. | |
143 * | |
144 * @return the completion length within the string of the context | |
145 */ | |
146 public int getCompletionLength() { | |
147 return fOriginalLength; | |
148 } | |
149 | |
150 /** | |
151 * Sets the completion length. | |
152 * | |
153 * @param newLength the new completion length | |
154 */ | |
155 protected void setCompletionLength(int newLength) { | |
156 fOriginalLength= newLength; | |
157 fPosition.setLength(newLength); | |
158 } | |
159 | |
160 /** | |
161 * Returns the keyword which triggered template insertion. | |
162 * | |
163 * @return the keyword which triggered template insertion | |
164 */ | |
165 public String getKey() { | |
166 int offset= getStart(); | |
167 int length= getEnd() - offset; | |
168 try { | |
169 return fDocument.get(offset, length); | |
170 } catch (BadLocationException e) { | |
171 return ""; //$NON-NLS-1$ | |
172 } | |
173 } | |
174 | |
175 /** | |
176 * Returns the beginning offset of the keyword. | |
177 * | |
178 * @return the beginning offset of the keyword | |
179 */ | |
180 public int getStart() { | |
181 return fPosition.getOffset(); | |
182 } | |
183 | |
184 /** | |
185 * Returns the end offset of the keyword. | |
186 * | |
187 * @return the end offset of the keyword | |
188 */ | |
189 public int getEnd() { | |
190 return fPosition.getOffset() + fPosition.getLength(); | |
191 } | |
192 | |
193 /* | |
194 * @see org.eclipse.jface.text.templates.TemplateContext#canEvaluate(org.eclipse.jface.text.templates.Template) | |
195 */ | |
196 public bool canEvaluate(Template template_) { | |
197 return true; | |
198 } | |
199 | |
200 /* | |
201 * @see org.eclipse.jface.text.templates.TemplateContext#evaluate(org.eclipse.jface.text.templates.Template) | |
202 */ | |
203 public TemplateBuffer evaluate(Template template_) { | |
204 if (!canEvaluate(template_)) | |
205 return null; | |
206 | |
207 TemplateTranslator translator= new TemplateTranslator(); | |
208 TemplateBuffer buffer= translator.translate(template_); | |
209 | |
210 getContextType().resolve(buffer, this); | |
211 | |
212 return buffer; | |
213 } | |
214 } |