Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>Extra ContextNew to Visual Studio Code? Get it now.

Extra Context

rioj7

|
186 installs
| (0) | Free
Add a few context variables for the when clause of key bindings. And set or toggle your own
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Add a few extra context variables to be used in key binding when clause. Or set a user defined context variable or toggle a user defined boolean context variable.

Automatic context variables

The following extra context variables are set:

  • extraContext:editorSelectionStartLine : the start line of the selection as a string
  • extraContext:editorSelectionEndLine : the end line of the selection as a string
  • extraContext:editorSelectionHasMultipleLines : boolean that is true if the selection spans multiple lines
  • extraContext:editorSelectionStartVisible : boolean that is true if the selection start is visible (*)
  • extraContext:editorSelectionStartLineRelativeVisibleTop : number: selection start line relative to visible range top (0 is first visible line)
  • extraContext:editorSelectionStartLineRelativeVisibleBottom : number: selection start line relative to visible range bottom (0 is last visible line)
  • extraContext:editorCursorNextChar : string: the character after the active position (cursor) of the first selection. The empty string when the active position is at the end of a line.

editorSelectionHasMultipleLines

Sometimes it is needed to adjust your command based on if there are more than 1 line selected.

The following example uses a terminal echo command for demonstration that the context variables exist.

The example uses the extension multi-command by ryuta46.

In settings.json:

  "multiCommand.commands": [
    {
      "command": "multiCommand.terminalSingleLine",
      "sequence": [
        { "command": "workbench.action.terminal.sendSequence",
          "args": { "text": "echo Single Line\u000D" }
        }
      ]
    },
    {
      "command": "multiCommand.terminalMultipleLine",
      "sequence": [
        { "command": "workbench.action.terminal.sendSequence",
          "args": { "text": "echo Multiple Lines\u000D" }
        }
      ]
    }
  ]

In keybindings.json:

  {
    "key": "ctrl+k f5", // or any other key combo
    "command": "multiCommand.terminalSingleLine",
    "when": "editorTextFocus && !extraContext:editorSelectionHasMultipleLines"
  },
  {
    "key": "ctrl+k f5",
    "command": "multiCommand.terminalMultipleLine",
    "when": "editorTextFocus && extraContext:editorSelectionHasMultipleLines"
  }

editorCursorNextChar

You can select different commands for a key binding based on the character to the right of the active position (cursor) of the first selection.

If you do not want to insert a tab, or insert spaces to the next tab stop, when the cursor is before a ] character use:

  {
    "key": "tab",
    "command": "cursorRight",
    "when": "editorTextFocus && extraContext:editorCursorNextChar == ']'"
  }

Actual name used in setVariable and toggleVariable

The actual context variable name used is extraContext:name. This ensures the name is unique in VSCode. You can't change the value of existing context variables because they are set by other part of VSCode. This is the name to use in when clauses for key bindings, menu options, ....

Together with a few commands from Select By you can setup a Multi Cursor Placement by keyboard.

  {
    "key": "ctrl+alt+right",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.addNewSelection",
    "args": {"offset": 1}
  },
  {
    "key": "shift+right",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.moveLastSelectionActive",
    "args": {"offset": 1}
  },
  {
    "key": "shift+left",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.moveLastSelectionActive",
    "args": {"offset": -1}
  },
  {
    "key": "right",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.moveLastSelection",
    "args": {"offset": 1}
  },
  {
    "key": "left",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.moveLastSelection",
    "args": {"offset": -1}
  }

Set a context variable

With the command Extra Context: Set a context variable to a value (extra-context.setVariable) you can set a user defined context variable. The command has an object as argument with the following properties:

  • name : The name of the property to set
  • value : The value to set the property

If the command argument is not defined or the command is called from the Command Palette you are presented with a QuickPick list to choose a predefined option. The predefined options are taken from the extra-context.setVariables setting. The name and value defined in the picked item are used.

Toggle a context variable

With the command Extra Context: Toggle a context variable true/false value (extra-context.toggleVariable) you can toggle the boolean value of a context variable. The command has an object as argument with the following property:

  • name : The name of the property to toggle

If the command argument is not defined or the command is called from the Command Palette you are presented with a QuickPick list to choose a predefined variable. The predefined options are taken from the extra-context.toggleVariables setting. The name defined in the picked item is used.

Settings

The extension has the following settings:

  • extra-context.toggleVariables : Array of QuickPickItems with name property of context variables we can toggle
  • extra-context.setVariables : Array of QuickPickItems with name and value properties of context variables we can set

A QuickPickItem is an object with the properties described in the VSCode API documentation. You can use the label, detail and description properties to show a more user friendly item in the pick list. If label is the same as name you don't have to mention name.

An example:

  "extra-context.toggleVariables": [
    {"name": "multiCursorByKeyboard", "label":"Multi Cursor by Keyboard"}
  ],
  "extra-context.setVariables": [
    {
      "name": "multiCursorByKeyboard", "value": true,
      "label":"Multi Cursor by Keyboard", "description": ": On"
    },
    {
      "name": "multiCursorByKeyboard", "value": false,
      "label":"Multi Cursor by Keyboard", "description": ": Off"
    },
    {
      "name": "line-threshold", "value": 3,
      "label":"Line threshold", "description": ": 3"
    }
  ]
  • Contact us
  • Jobs
  • Privacy
  • Terms of use
  • Trademarks
© 2023 Microsoft