visual-studio-codevscode-extensions

Unable to use viewitem == and contextValue for TreeItem


I have the following TreeItem:

class Benchmark extends vscode.TreeItem {
    
    results: BenchmarkResult[] = [];

    constructor(name: string) {
        super(name);
        this.contextValue = 'benchmark';
    }

    addResult(real_time: number, time_unit: string, iterations: number) {
        let result = new BenchmarkResult(real_time, time_unit, iterations);
        this.results.push(result);
        this.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
    }
}

I've set the contextValue as I then wish to show different menus depending upon the TreeItem. I'm trying to then set this in my package.json like so:

"menus": {
  "view/item/context": [
    {
      "command": "benchmark-vscode.runBenchmarkBinary",
      "title": "Discover/run benchmarks",
      "when": "view == benchmarks && viewitem == benchmarkbinary"
    },
    {
      "command": "benchmark-vscode.runBenchmark",
      "title": "Run benchmark",
      "when": "view == benchmarks && viewitem == benchmark"
    }
  ]
}

But no combination of "when" values is doing what I expect. I either get no menu at all, or all menus under one item. What am I missing here?

N.B I also tried the inspect the context keys and I can't see my contextValue there.


Solution

  • As hinted to by the comments, it should be viewItem and not viewitem