本文整理匯總了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),
});
}
示例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.");
}
}
}
示例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(); });
});
示例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;
}
示例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);
});
});
}
示例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;
}
示例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;
}
示例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;
}
示例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(); });
});
示例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))));
})
});
});