vscode 扩展,设置 window.activeTextEditor [英] Vscode extension, setting window.activeTextEditor
问题描述
如何使用vscode扩展api设置activeTextEditor
?
How can I use the vscode extension api to set the activeTextEditor
?
我需要这个的原因是因为我的扩展依赖于对扩展 B 的外部调用,而扩展 B 不接受编辑器的参数.它只是查看 vscode.window.activeTextEditor
.
The reason I need this is because my extension depends on an external call to extension B, and extension B does not take an argument for editor. It just looks at vscode.window.activeTextEditor
.
我的扩展程序是一个 Web 视图,当在 Web 视图上单击某些内容时,它会调用扩展程序 B.那时,活动选项卡"是 webview,它不是我想要的 activeTextEditor
.
My extension is a webview which calls Extension B when something is clicked on the webview. At that moment in time, the 'active tab' is the webview, which is not activeTextEditor
that I want.
我想做类似的事情
vscode.window.activeTextEditor = editor
在我的点击处理程序开始时,但这不起作用,因为 activeTextEditor
不是一个 setter.
at the start of my click handler, but this doesn't work because activeTextEditor
is not a setter.
我尝试对相关编辑器进行虚假
I tried making a fake edit on the editor in question:
function changeFocus(editor: vscode.TextEditor) {
return editor.edit((editBuilder) => {
const pos = new vscode.Position(0, 0);
const nxt = new vscode.Position(0, 1);
editBuilder.insert(pos, "\\");
editBuilder.delete(new vscode.Range(pos, nxt));
});
}
但这不会改变 vscode.window.activeTextEditor
变量(它仍然是 undefined
).
but this does not change vscode.window.activeTextEditor
variable (it is still undefined
).
推荐答案
如果我理解正确,这会打开并聚焦另一个文件:
If I understand correctly, this opens and focuses another file:
const currentWorkSpace = await vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri);
vscode.window.showTextDocument(vscode.Uri.joinPath(currentWorkSpace.uri, 'search/oldSearch.js'), { preview: false });
它在之前保存的光标位置聚焦和可写.
It is focussed and writable at its previously saved cursor position.
showTextDocument()
有另外两种形式,其中一种形式可以将 TextDocument
作为要打开的文件,而不是 Uri
.
showTextDocument()
has two other forms, one of which can take a TextDocument
as the file to be opened, rather than a Uri
.
还有 openTextDocument()
可以根据需要创建一个新的无标题文件.
There is also openTextDocument()
which can create a new untitled file if you wish.
如果文件已经打开,此代码仍将用于聚焦它,因此您拥有 activeTextEditor
.
If the file is already open, this code will still serve to focus it and thus you have your activeTextEditor
.
这篇关于vscode 扩展,设置 window.activeTextEditor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!