Is there a way to access editorHasDefinitionProvider or similar context flags through the API?

Viewed 367

I'm writing an extension that is dependent on the vscode.executeDefinitionProvider command. This is the relevant code:

  const symbolDefinitions = (await vscode.commands.executeCommand(
    'vscode.executeDefinitionProvider',
    symbolMeta.uri,
    symbolMeta.position
  )) as vscode.LocationLink[];

In package.json I've added when clauses to make sure the difinition provider was loaded before the extension can be activated:

 "menus": {
      "editor/context": [
        {
          "command": "vscode-ext-gtad.gtad",
          "group": "navigation",
          "when": "editorHasTypeDefinitionProvider && editorHasDefinitionProvider && editorTextFocus && !isInEmbeddedEditor"
        }
      ]
    },
    "keybindings": [
      {
        "command": "vscode-ext-gtad.gtad",
        "key": "undefined",
        "when": "editorHasTypeDefinitionProvider && editorHasDefinitionProvider && editorTextFocus && !isInEmbeddedEditor"
        
      }

That is all well and fine for the end user.

The issue arises when doing unit tests that run the first code above without activating the extension. The tests are hit and miss, i.e. most of the time symbolDefinitions comes empty, while occasionally it contains the right definitions.

That made me suspect it could be due to the tests running too quickly, and calling executeCommand before the definition provider was fully loaded.

So I added a 5000ms delay before that call and voilĂ . Works 100%. But of course this is totally arbitrary and brittle.

I'm really looking here for a check, equivalent to the ones used for the when clauses, but that can be used in a script.

The closest I've got was this question. The accepted answer points to an issue where it's stated that you can only setContext (also the same in the official docs), while my problem would be solved by a getContext that doesn't exist yet.

0 Answers
Related