From 080da87640b94c95a1cca285c30afe9d49bb2aba Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 2 Aug 2022 18:06:34 -0500 Subject: [PATCH 1/4] Add keybinds for toggling common settings --- book/src/keymap.md | 52 ++++++++++++----------- helix-term/src/commands.rs | 73 ++++++++++++++++++++++++++++++++ helix-term/src/keymap/default.rs | 1 + 3 files changed, 102 insertions(+), 24 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 25f1943de9d2..93e6f48e9111 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -251,30 +251,34 @@ This layer is similar to vim keybindings as kakoune does not support window. #### Space mode -This layer is a kludge of mappings, mostly pickers. - - -| Key | Description | Command | -| ----- | ----------- | ------- | -| `f` | Open file picker | `file_picker` | -| `b` | Open buffer picker | `buffer_picker` | -| `j` | Open jumplist picker | `jumplist_picker` | -| `k` | Show documentation for item under cursor in a [popup](#popup) (**LSP**) | `hover` | -| `s` | Open document symbol picker (**LSP**) | `symbol_picker` | -| `S` | Open workspace symbol picker (**LSP**) | `workspace_symbol_picker` | -| `g` | Open document diagnostics picker (**LSP**) | `diagnostics_picker` | -| `G` | Open workspace diagnostics picker (**LSP**) | `workspace_diagnostics_picker` -| `r` | Rename symbol (**LSP**) | `rename_symbol` | -| `a` | Apply code action (**LSP**) | `code_action` | -| `'` | Open last fuzzy picker | `last_picker` | -| `w` | Enter [window mode](#window-mode) | N/A | -| `p` | Paste system clipboard after selections | `paste_clipboard_after` | -| `P` | Paste system clipboard before selections | `paste_clipboard_before` | -| `y` | Join and yank selections to clipboard | `yank_joined_to_clipboard` | -| `Y` | Yank main selection to clipboard | `yank_main_selection_to_clipboard` | -| `R` | Replace selections by clipboard contents | `replace_selections_with_clipboard` | -| `/` | Global search in workspace folder | `global_search` | -| `?` | Open command palette | `command_palette` | +This layer contains high-level commands like pickers, some LSP/DAP operations, +clipboard interaction, window management and other miscellaneous commands. + +| Key | Description | Command | +| ----- | ----------- | ------- | +| `f` | Open file picker | `file_picker` | +| `F` | Open file picker at current working directory | `file_picker_in_current_directory` | +| `b` | Open buffer picker | `buffer_picker` | +| `j` | Open jumplist picker | `jumplist_picker` | +| `s` | Open document symbol picker (**LSP**) | `symbol_picker` | +| `S` | Open workspace symbol picker (**LSP**) | `workspace_symbol_picker` | +| `g` | Open document diagnostics picker (**LSP**) | `diagnostics_picker` | +| `G` | Open workspace diagnostics picker (**LSP**) | `workspace_diagnostics_picker` | +| `a` | Apply code action (**LSP**) | `code_action` | +| `'` | Open last fuzzy picker | `last_picker` | +| `d` | Debug (experimental) | N/A | +| `w` | Enter [window mode](#window-mode) | N/A | +| `y` | Join and yank selections to clipboard | `yank_joined_to_clipboard` | +| `Y` | Yank main selection to clipboard | `yank_main_selection_to_clipboard` | +| `p` | Paste system clipboard after selections | `paste_clipboard_after` | +| `P` | Paste system clipboard before selections | `paste_clipboard_before` | +| `R` | Replace selections by clipboard contents | `replace_selections_with_clipboard` | +| `t` | Toggle setting | `toggle_setting` | +| `/` | Global search in workspace folder | `global_search` | +| `k` | Show documentation for item under cursor in a [popup](#popup) (**LSP**) | `hover` | +| `r` | Rename symbol (**LSP**) | `rename_symbol` | +| `h` | Select symbol references (**LSP**) | `select_references_to_symbol_under_cursor` | +| `?` | Open command palette | `command_palette` | > TIP: Global search displays results in a fuzzy picker, use `space + '` to bring it back up after opening a file. diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index f12c79f937a3..bec0b9005576 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -432,6 +432,7 @@ impl MappableCommand { record_macro, "Record macro", replay_macro, "Replay macro", command_palette, "Open command palette", + toggle_setting, "Toggle setting", ); } @@ -4845,3 +4846,75 @@ fn replay_macro(cx: &mut Context) { cx.editor.macro_replaying.pop(); })); } + +fn toggle_setting(cx: &mut Context) { + cx.on_next_key(move |cx, event| { + use helix_view::editor::ConfigEvent; + + cx.editor.autoinfo = None; + cx.editor.pseudo_pending = None; + + if let Some(ch) = event.char() { + let mut config = cx.editor.config().clone(); + + match ch { + 'w' => { + use helix_view::editor::{ + WhitespaceRender::Basic, WhitespaceRenderValue::All, + WhitespaceRenderValue::None, + }; + + config.whitespace.render = match config.whitespace.render { + Basic(None) => Basic(All), + _ => Basic(None), + }; + } + 'i' => config.indent_guides.render = !config.indent_guides.render, + 'c' => config.cursorline = !config.cursorline, + 'r' => { + config.rulers = match config.rulers.as_slice() { + [80] => vec![], + _ => vec![80], + }; + } + 'p' => { + use helix_core::syntax::AutoPairConfig::Enable; + + config.auto_pairs = match config.auto_pairs { + Enable(false) => Enable(true), + _ => Enable(false), + } + } + _ => cx.editor.set_error(format!("Unknown setting '{}'", ch)), + } + + if cx + .editor + .config_events + .0 + .send(ConfigEvent::Update(Box::new(config))) + .is_err() + { + cx.editor + .set_error(format!("Failed to update setting '{}'", ch)); + } + } + }); + + let help_text = [ + ("w", "Whitespace"), + ("i", "Indent guides"), + ("c", "Cursorline"), + ("r", "Ruler at 80 columns"), + ("p", "Auto-pairs"), + ]; + + cx.editor.autoinfo = Some(Info::new( + "Toggle setting", + help_text + .into_iter() + .map(|(col1, col2)| (col1.to_string(), col2.to_string())) + .collect(), + )); + cx.editor.pseudo_pending = Some("t".to_string()); +} diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index f6fb6140b6d3..0af1cdb30470 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -259,6 +259,7 @@ pub fn default() -> HashMap { "p" => paste_clipboard_after, "P" => paste_clipboard_before, "R" => replace_selections_with_clipboard, + "t" => toggle_setting, "/" => global_search, "k" => hover, "r" => rename_symbol, From 1fda5eb38e1621044c5e2ef23fba5cb107496ad8 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 3 Aug 2022 07:23:44 -0500 Subject: [PATCH 2/4] Fix toggle behavior for rulers The toggle should disable the feature if it's enabled in any way and should enable the feature to the hard-coded default if it's disabled. --- helix-term/src/commands.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index bec0b9005576..797b3066d8fb 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4872,9 +4872,10 @@ fn toggle_setting(cx: &mut Context) { 'i' => config.indent_guides.render = !config.indent_guides.render, 'c' => config.cursorline = !config.cursorline, 'r' => { - config.rulers = match config.rulers.as_slice() { - [80] => vec![], - _ => vec![80], + config.rulers = if config.rulers.is_empty() { + vec![80] + } else { + vec![] }; } 'p' => { From 7476c360d63edf052d28a9e362d6421ac622026a Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 3 Aug 2022 07:30:02 -0500 Subject: [PATCH 3/4] Add toggle for line numbers (relative, absolute) --- helix-term/src/commands.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 797b3066d8fb..ddd278ab3eb0 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4886,6 +4886,14 @@ fn toggle_setting(cx: &mut Context) { _ => Enable(false), } } + 'l' => { + use helix_view::editor::LineNumber::{Absolute, Relative}; + + config.line_number = match config.line_number { + Absolute => Relative, + Relative => Absolute, + }; + } _ => cx.editor.set_error(format!("Unknown setting '{}'", ch)), } @@ -4908,6 +4916,8 @@ fn toggle_setting(cx: &mut Context) { ("c", "Cursorline"), ("r", "Ruler at 80 columns"), ("p", "Auto-pairs"), + ("l", "Line-number (absolute/relative)"), + ("l", "Line-number (absolute, relative)"), ]; cx.editor.autoinfo = Some(Info::new( From 890e9ab801fec51d6cd70f759178e483a6e144ac Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 3 Aug 2022 07:31:12 -0500 Subject: [PATCH 4/4] Show available toggle states in help_text --- helix-term/src/commands.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index ddd278ab3eb0..8bdeb6950e22 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4911,12 +4911,11 @@ fn toggle_setting(cx: &mut Context) { }); let help_text = [ - ("w", "Whitespace"), - ("i", "Indent guides"), - ("c", "Cursorline"), - ("r", "Ruler at 80 columns"), - ("p", "Auto-pairs"), - ("l", "Line-number (absolute/relative)"), + ("w", "Whitespace (visible, invisible)"), + ("i", "Indent guides (on, off)"), + ("c", "Cursorline (on, off)"), + ("r", "Ruler (80 columns, none)"), + ("p", "Auto-pairs (on, off)"), ("l", "Line-number (absolute, relative)"), ];