inMemoryDocument.ts 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';

export class InMemoryDocument implements vscode.TextDocument {
	private readonly _lines: string[];

	constructor(
		public readonly uri: vscode.Uri,
M
Matt Bierner 已提交
13 14
		private readonly _contents: string,
		public readonly version = 1,
15 16 17 18
	) {
		this._lines = this._contents.split(/\n/g);
	}

19

20 21 22 23 24
	isUntitled: boolean = false;
	languageId: string = '';
	isDirty: boolean = false;
	isClosed: boolean = false;
	eol: vscode.EndOfLine = vscode.EndOfLine.LF;
J
Johannes Rieken 已提交
25
	notebook: undefined;
26

27 28 29 30
	get fileName(): string {
		return this.uri.fsPath;
	}

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	get lineCount(): number {
		return this._lines.length;
	}

	lineAt(line: any): vscode.TextLine {
		return {
			lineNumber: line,
			text: this._lines[line],
			range: new vscode.Range(0, 0, 0, 0),
			firstNonWhitespaceCharacterIndex: 0,
			rangeIncludingLineBreak: new vscode.Range(0, 0, 0, 0),
			isEmptyOrWhitespace: false
		};
	}
	offsetAt(_position: vscode.Position): never {
		throw new Error('Method not implemented.');
	}
48 49 50 51 52 53
	positionAt(offset: number): vscode.Position {
		const before = this._contents.slice(0, offset);
		const newLines = before.match(/\n/g);
		const line = newLines ? newLines.length : 0;
		const preCharacters = before.match(/(\n|^).*$/g);
		return new vscode.Position(line, preCharacters ? preCharacters[0].length : 0);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	}
	getText(_range?: vscode.Range | undefined): string {
		return this._contents;
	}
	getWordRangeAtPosition(_position: vscode.Position, _regex?: RegExp | undefined): never {
		throw new Error('Method not implemented.');
	}
	validateRange(_range: vscode.Range): never {
		throw new Error('Method not implemented.');
	}
	validatePosition(_position: vscode.Position): never {
		throw new Error('Method not implemented.');
	}
	save(): never {
		throw new Error('Method not implemented.');
	}
J
Johannes Rieken 已提交
70
}