How to focus a custom view when writing a VS Code extension?

和自甴很熟 提交于 2021-01-27 07:24:16

问题


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?
}));

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?


回答1:


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});

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.

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



来源:https://stackoverflow.com/questions/51237836/how-to-focus-a-custom-view-when-writing-a-vs-code-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!