编写 VS Code 扩展时如何聚焦自定义视图? [英] How to focus a custom view when writing a VS Code extension?

查看:35
本文介绍了编写 VS Code 扩展时如何聚焦自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关 VS Code 扩展的帮助.我已经编写了一个工作正常的自定义视图,但是我想通过使用键盘快捷键或上下文菜单命令来激活/聚焦/进入该视图.我找不到如何使用 VS 代码 API 来实现这一点.

I need help with my VS Code extension. I've written a custom view which works just fine, however I'd like to activate / focus / bring into view that view by using a keyboard shortcut or a context menu command. I am unable to find how to use the VS code API to achieve that.

context.subscriptions.push(vscode.commands.registerCommand('extensionId.showView', () =>
{   
    // how to do that?
}));

我知道这可以做到,因为可以使用以下代码 snppet 显示文件资源管理器:

I know this can be done, because one can display the file explorer by using this code snppet:

vscode.commands.executeCommand('workbench.view.search');

但是您将如何为自定义树视图执行此操作?

But how would you do that for a custom tree view?

推荐答案

您应该能够使用 新的 focus 选项已在 1.25 中添加到 TreeView.reveal() 中.该方法要求您传递要显示的树项,因此它更像是一种无法直接聚焦视图本身的解决方法,但您可以简单地传递第一个/根节点.

You should be able to use the new focus option that was added to TreeView.reveal() in 1.25 for that. The method requires you to pass a tree item to be revealed, so it's more of a workaround for not being able to focus the view itself directly, but you could simply pass the first / root node.

treeView.reveal(item, {focus: true});

请注意,本例中的焦点表示键盘焦点.如果你只是想把它显示出来,调用 reveal() 没有焦点选项就足够了.

Note that focus in this case means keyboard focus. If you just want to bring it into view, calling reveal() without the focus option is good enough.

要获得 TreeView 实例,您需要使用您的视图 ID 和提供程序调用 vscode.window.createTreeView().

To obtain a TreeView instance, you need to call vscode.window.createTreeView() with your view ID and provider.

这篇关于编写 VS Code 扩展时如何聚焦自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆