From 72d8f2e595fa048fe529931e97c06cafc903db7f Mon Sep 17 00:00:00 2001 From: Ozan Date: Fri, 13 Dec 2024 01:56:42 +0100 Subject: [PATCH] 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. --- crates/editor/src/editor.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 6dcd2c17a82e8e0c17422a378b8d2d31aa067006..f86acc72b7f55f3738499ee2d27cf2519dca6411 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -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 }