如何获取当前打开文件的目录?

huangapple go评论94阅读模式
英文:

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

问题

我在为Visual Studio Code编写扩展时遇到了问题!我一直收到这样的消息:“Cannot read properties of undefined (reading 'uri')”等等。

 const defaultCompiler = 'g++';

 // 定义每个编译器编译C++代码的命令
 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);

我在Google上搜索了解决这个问题的方法,但找到的一切都没有帮助我。我尝试了各种组合在变量currentlyOpenFilePath,curDir,cFile(当前打开的文件),dirPath,oFile中。

我的系统是MacOS,还需要在Windows上工作。
而且这个扩展是本地的(之前版本的代码不是当前打开文件的路径,而是扩展的路径)。

我需要这个插件,在调用时能够成功处理命令“g++ -S -o {oFile} {cFile}”或“clang++ -S -o {oFile} {cFile}”,将{cFile}更改为打开编辑器中当前.cpp文件的当前路径,并将其保存为output.s,与{cFile}的路径相同。

英文:

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')", 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.

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)

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

答案1

得分: 0

尝试看看是否是你想要的。

英文:
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

huangapple
  • 本文由 发表于 2023年1月10日 21:06:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75070460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定