From 555579a82ed99d46ae4ef681a3cd474d983470d6 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Sat, 15 Jan 2022 19:56:02 -0600 Subject: [PATCH 1/6] add default keymap for show_subtree command --- book/src/keymap.md | 1 + helix-term/src/keymap.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/book/src/keymap.md b/book/src/keymap.md index ea890575e14d..5d6a3eb428e9 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -238,6 +238,7 @@ This layer is a kludge of mappings, mostly pickers. | `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` | +| `t` | Show syntax tree for item under cursor in a [popup](#popup) | `show_subtree` | > 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/keymap.rs b/helix-term/src/keymap.rs index c4bd25edaa08..2808d068ff58 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -677,6 +677,7 @@ impl Default for Keymaps { "R" => replace_selections_with_clipboard, "/" => global_search, "k" => hover, + "t" => show_subtree, "r" => rename_symbol, }, "z" => { "View" From a828a76c2c41eed3d92a916aa4c390dc747fef5d Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 17 Jan 2022 08:34:00 -0600 Subject: [PATCH 2/6] remove space+t keymap --- book/src/keymap.md | 1 - helix-term/src/keymap.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 5d6a3eb428e9..ea890575e14d 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -238,7 +238,6 @@ This layer is a kludge of mappings, mostly pickers. | `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` | -| `t` | Show syntax tree for item under cursor in a [popup](#popup) | `show_subtree` | > 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/keymap.rs b/helix-term/src/keymap.rs index 2808d068ff58..c4bd25edaa08 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -677,7 +677,6 @@ impl Default for Keymaps { "R" => replace_selections_with_clipboard, "/" => global_search, "k" => hover, - "t" => show_subtree, "r" => rename_symbol, }, "z" => { "View" From 470db596bc96a3c740e5f0bf787c02ab80f7fd0b Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 17 Jan 2022 08:34:09 -0600 Subject: [PATCH 3/6] add a typable command ':show-subtree' --- helix-term/src/commands.rs | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index c14216c0e253..0150412ee232 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2763,6 +2763,46 @@ pub mod cmd { Ok(()) } + fn show_subtree_typable( + cx: &mut compositor::Context, + _args: &[Cow], + _event: PromptEvent, + ) -> anyhow::Result<()> { + let (view, doc) = current!(cx.editor); + + if let Some(syntax) = doc.syntax() { + let primary_selection = doc.selection(view.id).primary(); + let text = doc.text(); + let from = text.char_to_byte(primary_selection.from()); + let to = text.char_to_byte(primary_selection.to()); + if let Some(selected_node) = syntax + .tree() + .root_node() + .descendant_for_byte_range(from, to) + { + let contents = format!("```tsq\n{}\n```", selected_node.to_sexp()); + + let callback = async move { + let call: job::Callback = + Box::new(move |editor: &mut Editor, compositor: &mut Compositor| { + let contents = ui::Markdown::new(contents, editor.syn_loader.clone()); + let popup = Popup::new("hover", contents); + if let Some(doc_popup) = compositor.find_id("hover") { + *doc_popup = popup; + } else { + compositor.push(Box::new(popup)); + } + }); + Ok(call) + }; + + cx.jobs.callback(callback); + } + } + + Ok(()) + } + pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ TypableCommand { name: "quit", @@ -3079,6 +3119,13 @@ pub mod cmd { fun: sort_reverse, completer: None, }, + TypableCommand { + name: "show-subtree", + aliases: &[], + doc: "Display tree sitter subtree under cursor, primarily for debugging queries.", + fun: show_subtree_typable, + completer: None, + }, ]; pub static TYPABLE_COMMAND_MAP: Lazy> = From 4ef2b443b1ba68444840861ccd588f0ec0bc4f54 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 17 Jan 2022 08:38:54 -0600 Subject: [PATCH 4/6] generate documentation for ':show-subtree' --- book/src/generated/typable-cmd.md | 1 + 1 file changed, 1 insertion(+) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index 65b2dc5f1709..defe64e06bd4 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -45,3 +45,4 @@ | `:set-option`, `:set` | Set a config option at runtime | | `:sort` | Sort ranges in selection. | | `:rsort` | Sort ranges in selection in reverse order. | +| `:show-subtree` | Display tree sitter subtree under cursor, primarily for debugging queries. | From 06d3dd11da773e39a05abbaa752a11900e08a1f6 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 18 Jan 2022 08:57:47 -0600 Subject: [PATCH 5/6] remove non-typable show_subtree command --- helix-term/src/commands.rs | 35 ++--------------------------------- 1 file changed, 2 insertions(+), 33 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 0150412ee232..564190e5fc11 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -400,7 +400,6 @@ impl MappableCommand { decrement, "Decrement", record_macro, "Record macro", replay_macro, "Replay macro", - show_subtree, "Show tree-sitter subtree under primary selection", ); } @@ -2763,7 +2762,7 @@ pub mod cmd { Ok(()) } - fn show_subtree_typable( + fn show_subtree( cx: &mut compositor::Context, _args: &[Cow], _event: PromptEvent, @@ -3123,7 +3122,7 @@ pub mod cmd { name: "show-subtree", aliases: &[], doc: "Display tree sitter subtree under cursor, primarily for debugging queries.", - fun: show_subtree_typable, + fun: show_subtree, completer: None, }, ]; @@ -6272,33 +6271,3 @@ fn replay_macro(cx: &mut Context) { }, )); } - -fn show_subtree(cx: &mut Context) { - let (view, doc) = current!(cx.editor); - - if let Some(syntax) = doc.syntax() { - let primary_selection = doc.selection(view.id).primary(); - let text = doc.text(); - let from = text.char_to_byte(primary_selection.from()); - let to = text.char_to_byte(primary_selection.to()); - if let Some(selected_node) = syntax - .tree() - .root_node() - .descendant_for_byte_range(from, to) - { - let contents = format!("```tsq\n{}\n```", selected_node.to_sexp()); - - cx.callback = Some(Box::new( - move |compositor: &mut Compositor, cx: &mut compositor::Context| { - let contents = ui::Markdown::new(contents, cx.editor.syn_loader.clone()); - let popup = Popup::new("hover", contents); - if let Some(doc_popup) = compositor.find_id("hover") { - *doc_popup = popup; - } else { - compositor.push(Box::new(popup)); - } - }, - )); - } - } -} From e1fdae3ae4fdf00781b1eed4f781b9e2f3fffae6 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Thu, 20 Jan 2022 10:35:51 -0600 Subject: [PATCH 6/6] ':show-subtree'->':tree-sitter-subtree' --- book/src/generated/typable-cmd.md | 2 +- helix-term/src/commands.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index defe64e06bd4..aed75cbd1a7c 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -45,4 +45,4 @@ | `:set-option`, `:set` | Set a config option at runtime | | `:sort` | Sort ranges in selection. | | `:rsort` | Sort ranges in selection in reverse order. | -| `:show-subtree` | Display tree sitter subtree under cursor, primarily for debugging queries. | +| `:tree-sitter-subtree`, `:ts-subtree` | Display tree sitter subtree under cursor, primarily for debugging queries. | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 564190e5fc11..f31d2bb7c32b 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2762,7 +2762,7 @@ pub mod cmd { Ok(()) } - fn show_subtree( + fn tree_sitter_subtree( cx: &mut compositor::Context, _args: &[Cow], _event: PromptEvent, @@ -3119,10 +3119,10 @@ pub mod cmd { completer: None, }, TypableCommand { - name: "show-subtree", - aliases: &[], + name: "tree-sitter-subtree", + aliases: &["ts-subtree"], doc: "Display tree sitter subtree under cursor, primarily for debugging queries.", - fun: show_subtree, + fun: tree_sitter_subtree, completer: None, }, ];