diff --git a/types/src/main.ts b/types/src/main.ts index 654dd107f..c336b43cb 100644 --- a/types/src/main.ts +++ b/types/src/main.ts @@ -1460,11 +1460,14 @@ export interface TextDocument { readonly version: number; /** - * Get the text of this document. + * Get the text of this document. A substring can be retrieved by + * providing a range. * - * @return The text of this document. + * @param range The interested range within the document to return. + * @return The text of this document or a substring of the text if a + * range is provided. */ - getText(): string; + getText(range?: Range): string; /** * Converts a zero-based offset to a position. @@ -1607,7 +1610,12 @@ class FullTextDocument implements TextDocument { return this._version; } - public getText(): string { + public getText(range?: Range): string { + if (range) { + let start = this.offsetAt(range.start); + let end = this.offsetAt(range.end); + return this._content.substring(start, end); + } return this._content; } diff --git a/types/src/test/textdocument.test.ts b/types/src/test/textdocument.test.ts index 6164b4972..c230e08c4 100644 --- a/types/src/test/textdocument.test.ts +++ b/types/src/test/textdocument.test.ts @@ -5,7 +5,7 @@ 'use strict'; import assert = require('assert'); -import { TextDocument, Position } from '../main'; +import { TextDocument, Range, Position } from '../main'; suite('Text Document Lines Model Validator', () => { function newDocument(str: string) { @@ -62,6 +62,17 @@ suite('Text Document Lines Model Validator', () => { assert.equal(newDocument(str).lineCount, 3); }) + test('getText(Range)', () => { + var str = "12345\n12345\n12345"; + var lm = newDocument(str); + assert.equal(lm.getText(), str); + assert.equal(lm.getText(Range.create(-1, 0, 0, 5)), "12345"); + assert.equal(lm.getText(Range.create(0, 0, 0, 5)), "12345"); + assert.equal(lm.getText(Range.create(0, 4, 1, 1)), "5\n1"); + assert.equal(lm.getText(Range.create(0, 4, 2, 1)), "5\n12345\n1"); + assert.equal(lm.getText(Range.create(0, 4, 3, 1)), "5\n12345\n12345"); + assert.equal(lm.getText(Range.create(0, 0, 3, 5)), str); + }); test('Invalid inputs', () => { var str = "Hello World";