2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.template;
7 import net.sourceforge.phpdt.internal.corext.Assert;
9 import org.eclipse.jface.text.BadLocationException;
10 import org.eclipse.jface.text.IDocument;
14 * A typical text based document template context.
16 public abstract class DocumentTemplateContext extends TemplateContext {
18 /** The text of the document. */
19 private final IDocument fDocument;
20 /** The completion offset. */
21 private final int fCompletionOffset;
22 /** The completion length. */
23 private final int fCompletionLength;
26 * Creates a document template context.
28 protected DocumentTemplateContext(ContextType type, IDocument document,
29 int completionOffset, int completionLength)
33 Assert.isNotNull(document);
34 Assert.isTrue(completionOffset >= 0 && completionOffset <= document.getLength());
35 Assert.isTrue(completionLength >= 0);
38 fCompletionOffset= completionOffset;
39 fCompletionLength= completionLength;
42 public IDocument getDocument() {
47 * Returns the completion offset within the string of the context.
49 public int getCompletionOffset() {
50 return fCompletionOffset;
54 * Returns the completion length within the string of the context.
56 public int getCompletionLength() {
57 return fCompletionLength;
61 * Returns the keyword which triggered template insertion.
63 public String getKey() {
64 int offset= getStart();
65 int length= getEnd() - offset;
67 return fDocument.get(offset, length);
68 } catch (BadLocationException e) {
69 return ""; //$NON-NLS-1$
74 * Returns the beginning offset of the keyword.
76 public int getStart() {
77 return fCompletionOffset;
81 * Returns the end offset of the keyword.
84 return fCompletionOffset + fCompletionLength;