How to add Context menu to a folder containing a specific type of file in Vscode?

90 views Asked by At

I'd like to add a context menu in the Vscode explorer window as part of my extension development. it should appear when right-clicking on a folder containing build files. The menu action should only be enabled if the folder contains any type of build files, such as pom.xml or build.gradle.

export async function hasPomXmlOrBuildGradle(): Promise<boolean> {
    const workspaceFolders = vscode.workspace.workspaceFolders;

    if (!workspaceFolders) {
        // No workspace folders found
        return false;
    }

    let hasPomXmlOrBuildGradle = false;

    // Iterate through each workspace folder
    for (const folder of workspaceFolders) {
        const folderPath = folder.uri.fsPath;

        // Check if either pom.xml or build.gradle exists in the folder
        const pomXmlPath = path.join(folderPath, 'pom.xml');
        const buildGradlePath = path.join(folderPath, 'build.gradle');

        if (fs.existsSync(pomXmlPath) || fs.existsSync(buildGradlePath)) {
            // Found either pom.xml or build.gradle
            hasPomXmlOrBuildGradle = true;
            break;
        }
    }
    return hasPomXmlOrBuildGradle;
}

"menus": {
            "explorer/context": [
                {
                    "command": "dev.add.project",
                    "when": "myExtension.hasPomXmlOrBuildGradle"
                },

I have called this function from extension.ts file, which is specified in the main: field in pacakge.json file.

vscode.commands.executeCommand('setContext', 'myExtension.hasPomXmlOrBuildGradle', devCommands.hasPomXmlOrBuildGradle());

But the items are not displayed in the context menu even though the value is set true.

0

There are 0 answers