1 11 package org.eclipse.jface.text.templates; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jface.text.BadLocationException; 16 import org.eclipse.jface.text.IDocument; 17 import org.eclipse.jface.text.Position; 18 19 30 public class DocumentTemplateContext extends TemplateContext { 31 32 33 private final IDocument fDocument; 34 39 private final Position fPosition; 40 44 private int fOriginalOffset; 45 49 private int fOriginalLength; 50 51 59 public DocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length) { 60 this(type, document, new Position(offset, length)); 61 } 62 63 75 public DocumentTemplateContext(TemplateContextType type, IDocument document, Position position) { 76 super(type); 77 78 Assert.isNotNull(document); 79 Assert.isNotNull(position); 80 Assert.isTrue(position.getOffset() <= document.getLength()); 81 82 fDocument= document; 83 fPosition= position; 84 fOriginalOffset= fPosition.getOffset(); 85 fOriginalLength= fPosition.getLength(); 86 } 87 88 93 public IDocument getDocument() { 94 return fDocument; 95 } 96 97 102 public int getCompletionOffset() { 103 return fOriginalOffset; 104 } 105 106 111 protected void setCompletionOffset(int newOffset) { 112 fOriginalOffset= newOffset; 113 fPosition.setOffset(newOffset); 114 } 115 116 121 public int getCompletionLength() { 122 return fOriginalLength; 123 } 124 125 130 protected void setCompletionLength(int newLength) { 131 fOriginalLength= newLength; 132 fPosition.setLength(newLength); 133 } 134 135 140 public String getKey() { 141 int offset= getStart(); 142 int length= getEnd() - offset; 143 try { 144 return fDocument.get(offset, length); 145 } catch (BadLocationException e) { 146 return ""; } 148 } 149 150 155 public int getStart() { 156 return fPosition.getOffset(); 157 } 158 159 164 public int getEnd() { 165 return fPosition.getOffset() + fPosition.getLength(); 166 } 167 168 171 public boolean canEvaluate(Template template) { 172 return true; 173 } 174 175 178 public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { 179 if (!canEvaluate(template)) 180 return null; 181 182 TemplateTranslator translator= new TemplateTranslator(); 183 TemplateBuffer buffer= translator.translate(template); 184 185 getContextType().resolve(buffer, this); 186 187 return buffer; 188 } 189 } 190 | Popular Tags |