vscode插件开发----获得当前打开文档对应的工作区根目录

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

代码如下:

export function activate(context:any) {
  // 注册一个命令
  let disposable = vscode.commands.registerCommand('codeStat.countCurFile', function () {
    let editor = vscode.window.activeTextEditor;
        if (editor) {
          const currentDocumentUri = editor.document.uri;
          const selectedWorkspaceFolder = vscode.workspace.getWorkspaceFolder(currentDocumentUri);
          if (selectedWorkspaceFolder) {
            vscode.window.showInformationMessage(selectedWorkspaceFolder.uri.fsPath);
          }
        }
  });
  context.subscriptions.push(disposable);   // 插件退出时释放资源
}