Karthi
Karthi

Reputation: 585

How to add the custom "when clause" in VS Code?

Currently, I am developing a simple add-in for the .NET Core 3.0 blazor web projects. In this case, I want to enable the add-in if the .net core 3.0 blazor projects and if the project is not the .net core 3.0 blazor project, my custom add-in will not be shown.

I have googled regarding vscode add-in extension and found the default when clause for vscode like "when": "explorerResourceIsFolder" etc. But I want the add-in in workspace header with the condition like if the project is .net core 3.0 blazor. I don't know about how and where to add the logic for this.

enter image description here

I need to add my own condition for When clause to show my add-in. Also, If the project has my custom assemblies, I need to show another add-in in the explorer heading context menu with my own customized when clause.

Could you please suggest me how can I achieve this?

Below is my coding part:

Package.Json

"commands": [
            {
                "command": "extension.openTemplatesFolder",
                "title": "Open Templates Folder",
                "category": "Project"
            },
            {
                "command": "extension.saveProjectAsTemplate",
                "title": "Save Project as Template",
                "category": "Project"
            },
            {
                "command": "extension.deleteTemplate",
                "title": "Delete Existing Template",
                "category": "Project"
            },
            {
                "command": "extension.createProjectFromTemplate",
                "title": "Create Project from Template",
                "category": "Project"
            }
        ],
        "menus": {
            "explorer/context": [
                {
                    "command": "extension.saveProjectAsTemplate",
                    "when": "myContext == success && explorerResourceIsRoot",
                    "group": "projectTemplates@1"
                }
            ]
        }

extension.ts

const value = "success";
vscode.commands.executeCommand('setContext', 'myContext', `${value}`);



export function activate(context: vscode.ExtensionContext) {



    // create manager and initialize template folder
    let projectTemplatesPlugin = new ProjectTemplatesPlugin(context, vscode.workspace.getConfiguration('projectTemplates'));
    projectTemplatesPlugin.createTemplatesDirIfNotExists();

    // register commands



    // open templates folder
    let openTemplatesFolder = vscode.commands.registerCommand('extension.openTemplatesFolder', 
        OpenTemplatesFolderCommand.run.bind(undefined, projectTemplatesPlugin));
    context.subscriptions.push(openTemplatesFolder);

    // save as template
    let saveProjectAsTemplate = vscode.commands.registerCommand('extension.saveProjectAsTemplate', 
        SaveProjectAsTemplateCommand.run.bind(undefined, projectTemplatesPlugin));
    context.subscriptions.push(saveProjectAsTemplate);
}

Note:

The vscode.commands.executeCommand('setContext', 'myContext', value); is not executed before show the add-in. It was executed after click the add-in

Upvotes: 6

Views: 2251

Answers (1)

Matt Bierner
Matt Bierner

Reputation: 65503

You can use the setContext command for this:

vscode.commands.executeCommand('setContext', 'myContext', `value`);

Then use myContext in your when clauses.

This command is not currently well documented but here's an example usage of this command in the git extension

Upvotes: 5

Related Questions