[英]How to put cursor on specified line in vscode editor extension programatically?
我正在研究 vscode TextEditorEdit 扩展。 这里我需要在以编辑器打开文件后,根据api响应内容将光标放在指定的行号上。 我怎样才能做到这一点? 或者什么类或接口提供了这样做的功能?
以下是我试图实现这一目标的代码。 但两者都不起作用。 甚至不引发任何异常或错误。
虽然这是打开并在编辑器上正确显示文件。 但是当我尝试更改光标位置时没有任何效果。
vscode.window.showTextDocument()
并使用自定义选择分配editor.selection
。 这对编辑器没有影响。vscode.workspace.openTextDocument(openPath).then(async doc => {
let pos1 = new vscode.Position(57, 40)
let pos2 = new vscode.Position(57, 42)
let sel = new vscode.Selection(pos1,pos2)
let rng = new vscode.Range(pos1, pos2)
vscode.window.showTextDocument(doc, vscode.ViewColumn.One).then((editor: vscode.TextEditor) => {
editor.selection = sel
//editor.revealRange(rng, vscode.TextEditorRevealType.InCenter)
});
});
我还尝试使用editor.revealRange(rng, vscode.TextEditorRevealType.InCenter)
显示范围,但也没有效果。
vscode.window.showTextDocument()
添加options?:
参数。 但这也没有任何作用。 请看下面的代码-vscode.workspace.openTextDocument(openPath).then(async doc => {
let pos1 = new vscode.Position(57, 40)
let pos2 = new vscode.Position(57, 42)
let rng = new vscode.Range(pos1, pos2)
vscode.window.showTextDocument(doc, {selection: rng, viewColumn: vscode.ViewColumn.One})
});
我想,问题在于首先打开文件并将文本文档显示到编辑器中,然后更改光标位置。
您可以使用cursorMove
内置命令:
let openPath = URI.file(mainPath);
vscode.workspace.openTextDocument(openPath).then(async (doc) => {
let line = 56;
let char = 10;
let pos1 = new vscode.Position(0, 0);
let pos2 = new vscode.Position(0, 0);
let sel = new vscode.Selection(pos1, pos2);
vscode.window.showTextDocument(doc, vscode.ViewColumn.One).then((e) => {
e.selection = sel;
vscode.commands
.executeCommand("cursorMove", {
to: "down",
by: "line",
value: line,
})
.then(() =>
vscode.commands.executeCommand("cursorMove", {
to: "right",
by: "character",
value: char,
})
);
});
});
此代码段首先打开文档并将其显示在编辑器中,然后将光标移动给定的行数,然后将其移动给定的字符数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.