Recently diving into the world of VS Code extension development, I stumbled upon a challenge. My issue lies in accessing objects that I create and store within the workspaceState property housed in ExtensionContext.
export async function activate(context: vscode.ExtensionContext) {
// Some code
const linksToDisplay = await initializeLinks(context.workspaceState); // Sets up and stores all created objects
// Some other code
let visualizer = vscode.commands.registerCommand('codePlumber.visualize', displayLinksCommand);
context.subscriptions.push(visualizer);
}
function displayLinksCommand() {
// How can I access workspace state here?
}
In essence, I craft objects during launch time for my extension and then desire for my commands to interact with these established objects.
I attempted passing context.workspaceState into the registerCommand function as an argument like so:
let visualizer = vscode.commands.registerCommand('codePlumber.visualize', displayLinksCommand, context.workspaceState);
However, this approach was unsuccessful.
A couple of questions spring to mind:
Is there a way to retrieve workspaceState from my commands? If so, how would the relevant code appear?
Could my method of storing objects in ExtensionContext be incorrect? If yes, are there alternative strategies worth exploring?