editor: Add "selection" key context (#21927)

This change allows defining keybindings that are active when there is a
text selection.

This is especially useful, as an example, for Emacs-like keybindings
where movement keybindings expand the selection.

Here is a snippet from my keymap.json that implements Emacs movements
when selection is active:

```json
{
    "context": "Editor && selection",
    "bindings": {
      "ctrl-f": "editor::SelectRight",
      "ctrl-b": "editor::SelectLeft",
      "ctrl-n": "editor::SelectDown",
      "ctrl-p": "editor::SelectUp",
      "ctrl-a": "editor::SelectToBeginningOfLine",
      "ctrl-e": "editor::SelectToEndOfLine",
      "alt-f": "editor::SelectToNextWordEnd",
      "alt-b": "editor::SelectToPreviousWordStart",
      "alt-<": "editor::SelectToBeginning",
      "alt->": "editor::SelectToEnd"
    }
  }
  ```

What do you think about inclusion of this feature? Should I add more granular `selection=single` `selection=multi`? 

Release Notes:

- Added "selection" context for keybindings that are active when there is a text selection.
This commit is contained in:
Ozan 2024-12-13 01:56:42 +01:00 committed by GitHub
parent 3f6ac53856
commit 72d8f2e595
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1402,6 +1402,15 @@ impl Editor {
key_context.add("inline_completion");
}
if !self
.selections
.disjoint
.iter()
.all(|selection| selection.start == selection.end)
{
key_context.add("selection");
}
key_context
}