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


TypeScript TextDocument.offsetAt方法代码示例

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


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

示例1: provideDocumentRangeFormattingEdits

 provideDocumentRangeFormattingEdits(
     document: TextDocument,
     range: Range,
     options: FormattingOptions,
     token: CancellationToken
 ): Promise<TextEdit[]> {
     return this._provideEdits(document, {
         rangeStart: document.offsetAt(range.start),
         rangeEnd: document.offsetAt(range.end),
     });
 }
开发者ID:JulioC,项目名称:prettier-vscode,代码行数:11,代码来源:PrettierEditProvider.ts

示例2: getLocation

	private async getLocation(document: TextDocument, position: Position): Promise<{ range: Range, placeholder: string }> {
		const resp = await this.analyzer.editGetRefactoring({
			file: fsPath(document.uri),
			kind: "RENAME",
			length: 0,
			offset: document.offsetAt(position),
			validateOnly: true,
		});

		const feedback = (resp.feedback as as.RenameFeedback);

		// The dart server returns -1 when the old name doesn't exist (for ex. renaming an unprefixed import to add a prefix)
		// so we use a zero-character range at the requested position in this case.
		const range = feedback.offset === -1
			? new Range(position, position)
			: toRange(document, feedback.offset, feedback.length);

		if (feedback) {
			return {
				placeholder: feedback.oldName,
				range,
			};
		} else {
			const fatalProblems = resp.initialProblems
				.concat(resp.optionsProblems)
				.concat(resp.finalProblems)
				.filter((p) => p.severity === "FATAL");

			if (fatalProblems && fatalProblems.length) {
				throw new Error(fatalProblems[0].message);
			} else {
				throw new Error("This rename is not supported.");
			}
		}
	}
开发者ID:DanTup,项目名称:Dart-Code,代码行数:35,代码来源:dart_rename_provider.ts

示例3: Range

		return new Promise<Hover>((resolve, reject) => {
			this.analyzer.analysisGetHover({
				file: document.fileName,
				offset: document.offsetAt(position)
			}).then(resp => {
				if (resp.hovers.length == 0) {
					resolve(null);
				} else {
					let hover = resp.hovers[0];
					let data = this.getHoverData(hover);
					if (data) {
						let range = new Range(
							document.positionAt(hover.offset),
							document.positionAt(hover.offset + hover.length)
						);
						resolve(new Hover(
							[{ language: 'dart', value: data.displayString }, data.documentation],
							range
						));
					} else {
						resolve(null);
					}
				}
			}, e => { logError(e); reject(); });
		});
开发者ID:devoncarew,项目名称:Dart-Code,代码行数:25,代码来源:dart_hover_provider.ts

示例4: provideCompletionItems

	public provideCompletionItems(document: TextDocument, position: Position, token: CancellationToken): Thenable<CompletionList> {

		let fileName = basename(document.fileName);

		let currentWord = this.getCurrentWord(document, position);
		let overwriteRange : Range;

		let items: CompletionItem[] = [];
		let isIncomplete = false;

		let offset = document.offsetAt(position);
		let location = getLocation(document.getText(), offset);

		let node = location.previousNode;
		if (node && node.offset <= offset && offset <= node.offset + node.length && (node.type === 'property' || node.type === 'string' || node.type === 'number' || node.type === 'boolean' || node.type === 'null')) {
			overwriteRange = new Range(document.positionAt(node.offset), document.positionAt(node.offset + node.length));
		} else {
			overwriteRange = new Range(document.positionAt(offset - currentWord.length), position);
		}
		let filterText = document.getText(new Range(overwriteRange.start, position));

		let proposed: { [key: string]: boolean } = {};
		let collector: ISuggestionsCollector = {
			add: (suggestion: CompletionItem) => {
				if (!proposed[suggestion.label]) {
					proposed[suggestion.label] = true;
					suggestion.textEdit = TextEdit.replace(overwriteRange, suggestion.insertText);
					suggestion.filterText = filterText;

					items.push(suggestion);
				}
			},
			setAsIncomplete: () => isIncomplete = true,
			error: (message: string) => console.error(message),
			log: (message: string) => console.log(message)
		};

		let collectPromise : Thenable<any> = null;

		if (location.isAtPropertyKey) {
			let addValue = !location.previousNode || !location.previousNode.columnOffset;
			let isLast = this.isLast(document, position);
			collectPromise = this.jsonContribution.collectPropertySuggestions(fileName, location, currentWord, addValue, isLast, collector);
		} else {
			if (location.path.length === 0) {
				collectPromise = this.jsonContribution.collectDefaultSuggestions(fileName, collector);
			} else {
				collectPromise = this.jsonContribution.collectValueSuggestions(fileName, location, collector);
			}
		}
		if (collectPromise) {
			return collectPromise.then(() => {
				if (items.length > 0) {
					return new CompletionList(items, isIncomplete);
				}
				return null;
			});
		}
		return null;
	}
开发者ID:Aarushi-Ign,项目名称:vscode,代码行数:60,代码来源:jsonContributions.ts

示例5: provideDocumentHighlights

	provideDocumentHighlights(
		document: TextDocument, position: Position, token: CancellationToken
	): Thenable<DocumentHighlight[]> {
		let file = document.fileName;
		let offset = document.offsetAt(position);

		this.analyzer.analysisSetSubscriptions({ subscriptions: { "OCCURRENCES": [file] } });

		return new Promise<DocumentHighlight[]>((resolve, reject) => {
			let disposable = this.analyzer.registerForAnalysisOccurrences(n => {
				if (n.file != file)
					return;

				this.analyzer.analysisSetSubscriptions({ subscriptions: { "OCCURRENCES": [] } });
				disposable.dispose();

				let highlights: DocumentHighlight[] = [];
				
				// The analysis server returns all items in the file that can have occurances, and
				// for each item, all the occurances of it in the file. We loop through each item
				// seeing if there's a match for the current cursor position. If there is, we create
				// highlights for those occurances, short circuit the search, and return the results.
				for (let occurrence of n.occurrences) {
					this.buildOccurrences(highlights, document, offset, occurrence);
					if (highlights.length > 0) {
						resolve(highlights);
						return;
					}
				}
				resolve(highlights);
			});
		});
	}
开发者ID:devoncarew,项目名称:Dart-Code,代码行数:33,代码来源:dart_highlighting_provider.ts

示例6: isLast

	private isLast(document: TextDocument, position: Position): boolean {
		const scanner = createScanner(document.getText(), true);
		scanner.setPosition(document.offsetAt(position));
		let nextToken = scanner.scan();
		if (nextToken === SyntaxKind.StringLiteral && scanner.getTokenError() === ScanError.UnexpectedEndOfString) {
			nextToken = scanner.scan();
		}
		return nextToken === SyntaxKind.CloseBraceToken || nextToken === SyntaxKind.EOF;
	}
开发者ID:developers23,项目名称:vscode,代码行数:9,代码来源:jsonContributions.ts

示例7: byteOffsetAt

export function byteOffsetAt(document: TextDocument, position: Position): number {
	let offset = document.offsetAt(position);
	let text = document.getText();
	let byteOffset = 0;
	for (let i = 0; i < offset; i++) {
		let clen = Buffer.byteLength(text[i]);
		byteOffset += clen;
	}
	return byteOffset;
}
开发者ID:Elflyy,项目名称:vscode-go,代码行数:10,代码来源:util.ts

示例8: doRename

	private async doRename(document: TextDocument, position: Position, newName: string, token: CancellationToken | undefined): Promise<WorkspaceEdit> {
		const outputChannel = channels.getChannel("Refactorings");
		outputChannel.appendLine("");

		const resp = await this.analyzer.editGetRefactoring({
			file: fsPath(document.uri),
			kind: "RENAME",
			length: 1,
			offset: document.offsetAt(position),
			options: {
				newName,
			},
			validateOnly: false,
		});
		const workspaceEdit = new WorkspaceEdit();

		if (resp.change && resp.change.message)
			outputChannel.appendLine(`[INFO] ${resp.change.message}…`);

		this.handleProblem(
			resp.initialProblems
				.concat(resp.optionsProblems)
				.concat(resp.finalProblems),
			outputChannel,
		);

		const promises: Array<Thenable<void>> = [];
		resp.change.edits.forEach((changeEdit) => {
			changeEdit.edits.forEach((fileEdit) => {
				const uri = Uri.file(changeEdit.file);
				const promise = workspace.openTextDocument(uri);
				promises.push(
					promise.then((document) =>
						workspaceEdit.replace(
							uri,
							new Range(
								document.positionAt(fileEdit.offset),
								document.positionAt(fileEdit.offset + fileEdit.length),
							),
							fileEdit.replacement,
						),
					),
				);
			});
		});

		// TODO: This class is inconsistent with other refactors (which are silent when they work, for ex).
		// We should review what we can extract share (though note that this method must return the edit whereas
		// the other refactors apply them).

		// Wait all openTextDocument to finish
		await Promise.all(promises);
		outputChannel.appendLine("[INFO] Rename successful.");
		return workspaceEdit;
	}
开发者ID:DanTup,项目名称:Dart-Code,代码行数:55,代码来源:dart_rename_provider.ts

示例9: reject

		return new Promise<Definition>((resolve, reject) => {
			this.analyzer.analysisGetNavigation({
				file: document.fileName,
				offset: document.offsetAt(position),
				length: 0
			}).then(resp => {
				if (resp.targets.length == 0)
					resolve(null)
				else
					resolve(resp.targets.map(t => this.convertResult(t, resp.files[t.fileIndex])));
			}, e => { util.logError(e); reject(); });
		});
开发者ID:devoncarew,项目名称:Dart-Code,代码行数:12,代码来源:dart_definition_provider.ts

示例10: CompletionList

		return new Promise<CompletionList>((resolve, reject) => {
			this.analyzer.completionGetSuggestions({
				file: document.fileName,
				offset: document.offsetAt(position)
			}).then(resp => {
				let disposable = this.analyzer.registerForCompletionResults(notification => {
					// Skip any results that are not ours (or are not the final results).
					if (notification.id != resp.id || !notification.isLast)
						return;

					disposable.dispose();
					resolve(new CompletionList(notification.results.map(r => this.convertResult(document, notification, r))));
				})
			});
		});
开发者ID:ikhwanhayat,项目名称:Dart-Code,代码行数:15,代码来源:dart_completion_item_provider.ts


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