简体   繁体   English

VS 代码扩展。 如何获取当前打开文件的目录?

[英]VS Code Extension. How to get the directory of the current open file?

I ran into a problem while writing an extension for Visual Studio Code, I keep getting messages like this "Cannot read properties of undefined (reading 'uri')".我在为 Visual Studio Code 编写扩展时遇到问题,我不断收到这样的消息“无法读取未定义的属性(读取‘uri’)”。 etc.等等

 const defaultCompiler = 'g++';

 // define the command for compiling C++ code with each compiler
 const compilerCommands = {
   'g++': 'g++ -S -o "{oFile}" "{cFile}"',
   'clang++': 'clang++ -S -o "{oFile}" "{cFile}"'
 };

 let currentlyOpenFilePath = vscode.window.activeTextEditor;
 // let curDir = vscode.workspace.workspaceFolders[0].uri.path
 let cFile = currentlyOpenFilePath.document.uri.path;
 let dirPath = path.dirname(cFile);
 let oFile = path.join(dirPath, 'output.s');

 let command = compilerCommands[this.currentCompiler].replace('{cFile}', currentlyOpenFilePath).replace('{oFile}', oFile);

I surfed all over Google in search of a solution to the problem, but everything I found did not help me.我在谷歌上到处冲浪寻找问题的解决方案,但我发现的一切都没有帮助我。 I tried various combinations in variables currentlyOpenFilePath, curDir, cFile (currently open file), dirPath, oFile.我在变量 currentlyOpenFilePath、curDir、cFile(当前打开的文件)、dirPath、oFile 中尝试了各种组合。

My system is MacOS and also needs to work on Windows. And also the extension is local (the previous version of the code took the path not of the current open file, but the path to the extension)我的系统是 MacOS,也需要在 Windows 上工作。而且扩展名是本地的(以前版本的代码采用的不是当前打开文件的路径,而是扩展名的路径)

I need this plugin, when called, to successfully process the command "g++ -S -o {oFile} {cFile}" or "clang++ -S -o {oFile} {cFile}" by changing {cFile} to the current path to the open.cpp file in editor a in {oFile}, same path as {cFile} but saved as output.s我需要这个插件,当被调用时,通过将 {cFile} 更改为当前路径来成功处理命令“g++ -S -o {oFile} {cFile}”或“clang++ -S -o {oFile} {cFile}” {oFile} 中编辑器 a 中的 open.cpp 文件,与 {cFile} 相同的路径,但另存为 output.s

let workspacePath = '';
if (vscode.workspace.workspaceFolders?.length) {
    workspacePath = workspace.workspaceFolders[0].uri.fsPath;
    workspacePath = path.normalize(workspacePath);
}

Try to see if it's what you want试试看是不是你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM