我正在开发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})
});我猜问题是先打开文件,然后将文本文档显示到编辑器中,然后更改光标位置。
发布于 2021-10-25 10:06:51
您可以使用cursorMove内置的VSCode命令:
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,
})
);
});
});这个片段首先打开文档并在编辑器中显示它,然后按给定的行数移动光标,然后按给定的字符数量移动它。
https://stackoverflow.com/questions/69671964
复制相似问题