Beno Odr
Beno Odr

Reputation: 1273

Vscode Ext display extension only when project have some file

I've created a vscode extension which handle some Rakefile tasks. Now I want to limit that the extension will be displayed in the explorer only when user open a Rakefile

for that I use the following entry in the package.json

 "contributes": {
    "views": {
      "explorer": [
        {
          "id": "rakeView",
          "name": "Rakee",
          "when": "resourceFilename ='Rakefile'"
        }
      ]
    },
    "commands": [
      {
        "command": "rakeView.executeTask",
        "title": "Execute",
        "icon": "images/icon.png"
      }
    ],

when adding the following line

"when": "resourceFilename ='Rakefile'"

the extension removed from the explorer view, when I remove it I was able to see the extension. I want it to display only when a project have a Rakefile how could I do it ?

https://github.com/microsoft/vscode-extension-samples/tree/1aae138e311fb87cc3ed2782be287f5d2f78e327/task-provider-sample

update

After trying the answer below it's still not working, this is the all code:

import * as vscode from "vscode";
import { rakeTaskProvider } from "./rakeCmd";
import { TaskTreeDataProvider } from "./rakeView";

let rakeTaskProvider: vscode.Disposable | undefined;

export function activate(_context: vscode.ExtensionContext): void {
  const workspaceRoot = vscode.workspace.rootPath;

  const onChangeActiveTextEditor = () => {
    let editor = vscode.window.activeTextEditor;
    vscode.commands.executeCommand('setContext', 'rakeView:fileIsRake', editor && editor.document.languageId === 'rakefile');
  };
  vscode.window.onDidChangeActiveTextEditor(onChangeActiveTextEditor, null, _context.subscriptions);
  onChangeActiveTextEditor();

  rakeTaskProvider = vscode.tasks.registerTaskProvider(
    rakeTaskProvider.rakeType,
    new rakeTaskProvider(workspaceRoot)
  );
  vscode.window.registerTreeDataProvider(
    "rakeView",
    new TaskTreeDataProvider(_context)
  );
  vscode.commands.registerCommand("rakeView.executeTask", function (task) {
    console.log(task);
    vscode.tasks.executeTask(task).then(
      function (value) {
        return value;
      },
      function (e) {
        console.error(
          "error",
          e
        );
      }
    );
  });
}

export function deactivate(): void {
  if (rakeTaskProvider) {
    rakeTaskProvider.dispose();
  }
}

package.json

"contributes": {
    "views": {
      "explorer": [
        {
          "id": "rakeView",
          "name": "Rakke",
          "when": "rakeView:fileIsRake"
        }
      ]
    },

Upvotes: 0

Views: 345

Answers (1)

rioV8
rioV8

Reputation: 28673

You need to use the context, when the editor changes set a context variable to signal the current file is a Rakefile

In your activate function

  const onChangeActiveTextEditor = () => {
    let editor = vscode.window.activeTextEditor;
    vscode.commands.executeCommand('setContext', 'myExtenstion:fileIsRake', editor && editor.document.languageId === 'rake');
  };
  vscode.window.onDidChangeActiveTextEditor(onChangeActiveTextEditor, null, context.subscriptions);
  onChangeActiveTextEditor();

In package.json add this context variable

    "views": {
      "explorer": [
        {
          "id": "rakeView",
          "name": "Rakee",
          "when": "myExtenstion:fileIsRake"
        }
      ]
    },

Upvotes: 2

Related Questions