Krithi k
Krithi k

Reputation: 13

How to get path for currently open tab file's root path

Tried to get project folder path in multi root workspace condition in visual studio code but facing very difficult to find the path. Below script only will work single root workspace But How to change for multi root workspace.

var vscode = require("vscode");
var path = require("path");
exports.activate = context => {
const searchoption = 
vscode.commands.registerCommand('extension.search', () => {

   let folderPath = vscode.workspace.rootPath; // get the open file's project folder path

});
}

Upvotes: 1

Views: 2651

Answers (1)

Scott McPeak
Scott McPeak

Reputation: 12749

Instead of workspace.rootPath, use workspace.workspaceFolders to get all of the folders.

But the title of your question refers to the "currently open tab". That isn't a meaningful concept in the activate function, which only runs when the extension first initializes. You'll want to instead write, say, a command handler (see the extension tutorial), and within that handler, use window.activeTextEditor, and eventually call window.getWorkspaceFolder.

Upvotes: 3

Related Questions