Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change TextDocument's getText to take an optional range #298

Merged
merged 1 commit into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions types/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 12 additions & 1 deletion types/src/test/textdocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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";
Expand Down