當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript ITextDocument.getText方法代碼示例

本文整理匯總了TypeScript中vscode-languageserver.ITextDocument.getText方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript ITextDocument.getText方法的具體用法?TypeScript ITextDocument.getText怎麽用?TypeScript ITextDocument.getText使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在vscode-languageserver.ITextDocument的用法示例。


在下文中一共展示了ITextDocument.getText方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: getEOL

function getEOL(document: ITextDocument): string {
	let text = document.getText();
	if (document.lineCount > 1) {
		let to = document.offsetAt(Position.create(1, 0));
		let from = to;
		while (from > 0 && isEOL(text, from - 1)) {
			from--;
		}
		return text.substr(from, to - from);
	}
	return '\n';
}
開發者ID:1424667164,項目名稱:vscode,代碼行數:12,代碼來源:jsonFormatter.ts

示例2: applyEdits

export function applyEdits(document: ITextDocument, edits: TextEdit[]) : string {
	let formatted = document.getText();
	let sortedEdits = edits.sort((a, b) => document.offsetAt(b.range.start) - document.offsetAt(a.range.start));
	let lastOffset = formatted.length;
	sortedEdits.forEach(e => {
		let startOffset = document.offsetAt(e.range.start);
		let endOffset = document.offsetAt(e.range.end);
		assert.ok(startOffset <= endOffset);
		assert.ok(endOffset <= lastOffset);
		formatted = formatted.substring(0, startOffset) + e.newText + formatted.substring(endOffset, formatted.length);
		lastOffset = startOffset;
	});
	return formatted;
}
開發者ID:13572293130,項目名稱:vscode,代碼行數:14,代碼來源:textEditSupport.ts

示例3: validateTextDocument

function validateTextDocument(textDocument: ITextDocument): void {
	let diagnostics: Diagnostic[] = [];
	let lines = textDocument.getText().split(/\r?\n/g);
	let problems = 0;
	for (var i = 0; i < lines.length && problems < maxNumberOfProblems; i++) {
		let line = lines[i];
		let index = line.indexOf('typescript');
		if (index >= 0) {
			problems++;
			diagnostics.push({
				severity: DiagnosticSeverity.Warning,
				range: {
					start: { line: i, character: index},
					end: { line: i, character: index + 10 }
				},
				message: `${line.substr(index, 10)} should be spelled TypeScript`
			});
		}
	}
	// Send the computed diagnostics to VSCode.
	connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
}
開發者ID:babakness,項目名稱:vscode-pgsql,代碼行數:22,代碼來源:server.ts

示例4: format

export function format(document: ITextDocument, range: Range, options: FormattingOptions): TextEdit[] {
	const documentText = document.getText();
	let initialIndentLevel: number;
	let value: string;
	let rangeOffset: number;
	if (range) {
		let startPosition = Position.create(range.start.line, 0);
		rangeOffset = document.offsetAt(startPosition);

		let endOffset = document.offsetAt(Position.create(range.end.line + 1, 0));
		let endLineStart = document.offsetAt(Position.create(range.end.line, 0));
		while (endOffset > endLineStart && isEOL(documentText, endOffset - 1)) {
			endOffset--;
		}
		range = Range.create(startPosition, document.positionAt(endOffset));
		value = documentText.substring(rangeOffset, endOffset);
		initialIndentLevel = computeIndentLevel(value, 0, options);
	} else {
		value = documentText;
		range = Range.create(Position.create(0, 0), document.positionAt(value.length));
		initialIndentLevel = 0;
		rangeOffset = 0;
	}
	let eol = getEOL(document);

	let lineBreak = false;
	let indentLevel = 0;
	let indentValue: string;
	if (options.insertSpaces) {
		indentValue = repeat(' ', options.tabSize);
	} else {
		indentValue = '\t';
	}

	let scanner = Json.createScanner(value, false);

	function newLineAndIndent(): string {
		return eol + repeat(indentValue, initialIndentLevel + indentLevel);
	}
	function scanNext(): Json.SyntaxKind {
		let token = scanner.scan();
		lineBreak = false;
		while (token === Json.SyntaxKind.Trivia || token === Json.SyntaxKind.LineBreakTrivia) {
			lineBreak = lineBreak || (token === Json.SyntaxKind.LineBreakTrivia);
			token = scanner.scan();
		}
		return token;
	}
	let editOperations: TextEdit[] = [];
	function addEdit(text: string, startOffset: number, endOffset: number) {
		if (documentText.substring(startOffset, endOffset) !== text) {
			let replaceRange = Range.create(document.positionAt(startOffset), document.positionAt(endOffset));
			editOperations.push(TextEdit.replace(replaceRange, text));
		}
	}

	let firstToken = scanNext();
	if (firstToken !== Json.SyntaxKind.EOF) {
		let firstTokenStart = scanner.getTokenOffset() + rangeOffset;
		let initialIndent = repeat(indentValue, initialIndentLevel);
		addEdit(initialIndent, rangeOffset, firstTokenStart);
	}

	while (firstToken !== Json.SyntaxKind.EOF) {
		let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset;
		let secondToken = scanNext();

		let replaceContent = '';
		while (!lineBreak && (secondToken === Json.SyntaxKind.LineCommentTrivia || secondToken === Json.SyntaxKind.BlockCommentTrivia)) {
			// comments on the same line: keep them on the same line, but ignore them otherwise
			let commentTokenStart = scanner.getTokenOffset() + rangeOffset;
			addEdit(' ', firstTokenEnd, commentTokenStart);
			firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset;
			replaceContent = secondToken === Json.SyntaxKind.LineCommentTrivia ? newLineAndIndent() : '';
			secondToken = scanNext();
		}
		
		if (secondToken === Json.SyntaxKind.CloseBraceToken) {
			if (firstToken !== Json.SyntaxKind.OpenBraceToken) {
				indentLevel--;
				replaceContent = newLineAndIndent();
			}
		} else if (secondToken === Json.SyntaxKind.CloseBracketToken) {
			if (firstToken !== Json.SyntaxKind.OpenBracketToken) {
				indentLevel--;
				replaceContent = newLineAndIndent();
			}
		} else {
			switch (firstToken) {
				case Json.SyntaxKind.OpenBracketToken:
				case Json.SyntaxKind.OpenBraceToken:
					indentLevel++;
					replaceContent = newLineAndIndent();
					break;
				case Json.SyntaxKind.CommaToken:
				case Json.SyntaxKind.LineCommentTrivia:
					replaceContent = newLineAndIndent();
					break;
				case Json.SyntaxKind.BlockCommentTrivia:
					if (lineBreak) {
//.........這裏部分代碼省略.........
開發者ID:13572293130,項目名稱:vscode,代碼行數:101,代碼來源:jsonFormatter.ts

示例5: validateTextDocument

function validateTextDocument(textDocument: ITextDocument): void {
	let linterBinPath = path.join(__dirname, '../../server/glualint');
	connection.console.log("lel");
	connection.console.log(linterBinPath);
	
	let stdout = childProcess.execFileSync(linterBinPath, ['stdin'], {input: textDocument.getText()});
	
	let diagnostics: Diagnostic[] = [];

	let lines = stdout.toString().split('\n');	
	for (let i = 0; i < lines.length; i++) {
		let m = errorRegex.exec(lines[i]);
		
		if (m != null) {
			var severity = DiagnosticSeverity.Error;
			if (m[1] == 'Warning') {
				severity = DiagnosticSeverity.Warning;
			}
			
			diagnostics.push({
				severity: severity,
				range: {
					start: { line: parseInt(m[2]) - 1, character: parseInt(m[3]) - 1},
					end: { line: parseInt(m[2]) - 1, character: parseInt(m[3]) - 1 + 3 }
				},
				message: m[4],
				source: 'glualint'
			});
		}
	}
	
	// Send the computed diagnostics to VSCode.
	connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
}
開發者ID:wyozi-gmod,項目名稱:vscode-gluaserver,代碼行數:34,代碼來源:server.ts


注:本文中的vscode-languageserver.ITextDocument.getText方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。