当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript TextDocument.offsetAt方法代码示例

本文整理汇总了TypeScript中vscode-languageserver.TextDocument.offsetAt方法的典型用法代码示例。如果您正苦于以下问题:TypeScript TextDocument.offsetAt方法的具体用法?TypeScript TextDocument.offsetAt怎么用?TypeScript TextDocument.offsetAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在vscode-languageserver.TextDocument的用法示例。


在下文中一共展示了TextDocument.offsetAt方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1:

	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;
	});
开发者ID:Huachao,项目名称:vscode,代码行数:8,代码来源:textEditSupport.ts

示例2: modifyRange

	// If the selection range just has whitespace before it in the line,
	// extend the range to account for that whitespace
	private modifyRange() {
		const { start } = this.range;
		const offset = this.document.offsetAt(start);
		const prefixLineText = this.originalText.substring(offset - start.character, offset);

		if (/^\s+$/.test(prefixLineText)) {
			this.range.start.character = 0;
		}
	}
开发者ID:rubyide,项目名称:vscode-ruby,代码行数:11,代码来源:BaseFormatter.ts

示例3: getEOL

function getEOL(document: TextDocument): 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:DiLRandI,项目名称:vscode,代码行数:12,代码来源:jsonFormatter.ts

示例4: format

export function format(document: TextDocument, 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:DiLRandI,项目名称:vscode,代码行数:101,代码来源:jsonFormatter.ts

示例5: extractText

function extractText(textDocument: TextDocument, range: LangServer.Range) {
    const { start, end } = range;
    const offStart = textDocument.offsetAt(start);
    const offEnd = textDocument.offsetAt(end);
    return textDocument.getText().slice(offStart, offEnd);
}
开发者ID:Jason-Rev,项目名称:vscode-spell-checker,代码行数:6,代码来源:codeActions.ts


注:本文中的vscode-languageserver.TextDocument.offsetAt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。