问题
I am onto writing a VSCode extension and need to create files / remove files based in a folder / subfolder selected by the user.
Is there a way to find selected folder from the explorer tree through visual studio code api? One can track the opened file through active text editor or onDidOpenTextDocument()
. I am interested in finding if user has selected a folder and if so the path to selected folder.
For example: workspace.rootPath
provides a root of the opened project. Similarly, how do I identify which subfolder is selected by the user (if any).
回答1:
I finally figured it out, I've been looking for a while and found that the documentation states that :
Note: When a command is invoked from a (context) menu, VS Code tries to infer the currently selected resource and passes that as a parameter when invoking the command. For instance, a menu item inside the Explorer is passed the URI of the selected resource and a menu item inside an editor is passed the URI of the document. vscode extension contribution points
That let me to this question/bug on github Get selected file folder, which in turn led me to the github repo of stef-levesque/vscode-hexdump from which I got the following:
let disposable = Commands.registerCommand('extension.superExtension', async (fileUri) => {
console.log(fileUri);
})
Note: when the context menu is used in the editor this will be empty.
来源:https://stackoverflow.com/questions/38270751/vscode-api-for-file-explorer