vscode插件开发----获得资源管理器当前选中的文件或者目录

发布时间 2023-06-04 22:33:18作者: 顺其自然,道法自然

没有直接的API可以获取, 不过可以根据鼠标右键上下文菜单项获取对应的uri.
package.json中的设置如下:

"menus": {
      "explorer/context": [
        {
          "command": "codeStat.countCurFile"
        }
      ]
    }

扩展实现代码如下:

// 激活函数,是首先要调用的
export function activate(context:any) {
  // 注册一个命令
  let disposable = vscode.commands.registerCommand('codeStat.countCurFile', function (uri) {
    if (uri) {
      vscode.window.showInformationMessage(uri.fsPath)
    } else {
      console.log('No folder is selected.');
    }
  });
  context.subscriptions.push(disposable);   // 插件退出时释放资源
}