OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

Extension tip to bring Visual Studio Code to the foreground

Development Visual Studio Code Devhack
post

This post is over a year old, some of this information may be out of date.

In one of the extensions I am developing with a custom authentication provider, I required to bring the last Visual Studio Code instance to the front once signed in.

When you can control the full authentication flow, you typically redirect the user to vscode://..., which triggers your browser from opening Visual Studio Code.

In my case, I do not own the authentication flow but still wanted to give a similar experience to the developer, so I came up with the following trick:

import { exec } from 'child_process';
function getWorkspaceFolder(): Promise<WorkspaceFolder | undefined> {
let folder: WorkspaceFolder | undefined;
if (!workspace.workspaceFolders || workspace.workspaceFolders.length === 0) {
return undefined;
} else if (workspace.workspaceFolders.length === 1) {
folder = workspace.workspaceFolders[0];
} else {
folder = await window.showWorkspaceFolderPick({ placeHolder: `Select the workspace folder` });
if (!folder) {
return undefined;
}
}
return folder;
}
const wsFolder = await getWorkspaceFolder();
exec(`code .`, { cwd: wsFolder?.uri.fsPath });

All the above code does is open the project again in Visual Studio Code. As the project/solution is already opened, it would not get reloaded. All it does is bring your Visual Studio Code instance to the front, which we needed.

Happy developing

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code