问题
I am trying to get currently opened file's project folder path in visual studio code extension but not working for Multi-root Workspaces.
Now i am getting path value is empty.
Currently i opened project-ws folder files app.component.html then press ctrl+shift+p on the files tab then box will open. if i click "Get project folder path" from extension box path value should be like C:\xampp\htdocs\projects\project-ws.
If i opened project-bh folder any files then press ctrl+shift+p on the files tab then box will open. if i click "Get project folder path" from extension box path value should be like C:\xampp\htdocs\projects\project-bh.
I could not find the mistake from my code.Anyone can resolve this issue?
Check this url then you can test in your vs code extension you can easily understand : https://github.com/jasonnutter/vscode-search-node-modules same concept i have used from this for my extension.
.
Press Ctrl+Shift+p = to see extension and click "Get project folder path" then path message should be come.
C:/Users/ADMIN/.vscode/extensions/getprojectpath/extension.js:
const vscode = require('vscode');
const path = require('path');
exports.activate = context => {
const searchMyModules = vscode.commands.registerCommand('extension.search', () => {
const preferences = vscode.workspace.getConfiguration('files.exclude');
const getCurrentlyopenedtabprojectfolderpath = async() => {
const editor = vscode.window.activeTextEditor;
if (!editor || !vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length < 2) {
return null;
}
let path;
const resource = editor.document.uri;
if (resource.scheme === 'file') {
const folder = vscode.workspace.getWorkspaceFolder(resource);
if (!folder) {
path = `$(alert) <outside workspace> → ${basename(resource.fsPath)}`;
} else {
path = `$(file-submodule) ${basename(folder.uri.fsPath)} (${folder.index + 1} of ${vscode.workspace.workspaceFolders.length}) → $(file-code) ${basename(resource.fsPath)}`;
}
}
return {
path
};
}
getCurrentlyopenedprojectpath().then(result => {
vscode.window.showInformationMessage(result.path)
})
})
}
exports.deactivate = () => {};
来源:https://stackoverflow.com/questions/57811209/how-to-get-currently-opened-files-project-folder-path-in-visual-studio-code-ext