Skip to main content
summaryrefslogtreecommitdiffstats
blob: ebabbf18feac66558364580ba7d7761bb8caca58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.text.templates;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;

/**
 * Instances of this class describe the context of a template as a region of
 * a document. That region may be either specified by its offset and length, or
 * by a <code>Position</code> which may or may not be registered with the
 * document.
 * <p>
 * Clients may instantiate and extend this class.
 * </p>
 *
 * @since 3.0
 */
public class DocumentTemplateContext extends TemplateContext {

	/** The text of the document. */
	private final IDocument fDocument;
	/**
	 * The region of the document described by this context. We store a
	 * position since clients may specify the document region as (updateable)
	 * Positions.
	 */
	private final Position fPosition;
	/**
	 * The original offset of this context. Will only be updated by the setter
	 * method.
	 */
	private int fOriginalOffset;
	/**
	 * The original length of this context. Will only be updated by the setter
	 * method.
	 */
	private int fOriginalLength;

	/**
	 * Creates a document template context.
	 *
	 * @param type the context type
	 * @param document the document this context applies to
	 * @param offset the offset of the document region
	 * @param length the length of the document region
	 */
	public DocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length) {
		this(type, document, new Position(offset, length));
	}

	/**
	 * Creates a document template context. The supplied <code>Position</code>
	 * will be queried to compute the <code>getStart</code> and
	 * <code>getEnd</code> methods, which will therefore answer updated
	 * position data if it is registered with the document.
	 *
	 * @param type the context type
	 * @param document the document this context applies to
	 * @param position the position describing the area of the document which
	 *        forms the template context
	 * @since 3.1
	 */
	public DocumentTemplateContext(TemplateContextType type, IDocument document, Position position) {
		super(type);

		Assert.isNotNull(document);
		Assert.isNotNull(position);
		Assert.isTrue(position.getOffset() <= document.getLength());

		fDocument= document;
		fPosition= position;
		fOriginalOffset= fPosition.getOffset();
		fOriginalLength= fPosition.getLength();
	}

	/**
	 * Returns the document.
	 *
	 * @return the document
	 */
	public IDocument getDocument() {
		return fDocument;
	}

	/**
	 * Returns the completion offset within the string of the context.
	 *
	 * @return the completion offset within the string of the context
	 */
	public int getCompletionOffset() {
		return fOriginalOffset;
	}

	/**
	 * Sets the completion offset.
	 *
	 * @param newOffset the new completion offset
	 */
	protected void setCompletionOffset(int newOffset) {
		fOriginalOffset= newOffset;
		fPosition.setOffset(newOffset);
	}

	/**
	 * Returns the completion length within the string of the context.
	 *
	 * @return the completion length within the string of the context
	 */
	public int getCompletionLength() {
		return fOriginalLength;
	}

	/**
	 * Sets the completion length.
	 *
	 * @param newLength the new completion length
	 */
	protected void setCompletionLength(int newLength) {
		fOriginalLength= newLength;
		fPosition.setLength(newLength);
	}

	/**
	 * Returns the keyword which triggered template insertion.
	 *
	 * @return the keyword which triggered template insertion
	 */
	public String getKey() {
		int offset= getStart();
		int length= getEnd() - offset;
		try {
			return fDocument.get(offset, length);
		} catch (BadLocationException e) {
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * Returns the beginning offset of the keyword.
	 *
	 * @return the beginning offset of the keyword
	 */
	public int getStart() {
		return fPosition.getOffset();
	}

	/**
	 * Returns the end offset of the keyword.
	 *
	 * @return the end offset of the keyword
	 */
	public int getEnd() {
		return fPosition.getOffset() + fPosition.getLength();
	}

	@Override
	public boolean canEvaluate(Template template) {
		return true;
	}

	@Override
	public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
		if (!canEvaluate(template))
			return null;

		TemplateTranslator translator= new TemplateTranslator();
		TemplateBuffer buffer= translator.translate(template);

		getContextType().resolve(buffer, this);

		return buffer;
	}
}

Back to the top