From a4a9ba708441abfbd1131a91fc8730fb2937f8d7 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Thu, 6 Jan 2022 10:32:17 -0600 Subject: [PATCH 1/3] add show_subtree command for viewing tree-sitter subtree in Popup --- helix-term/src/commands.rs | 31 +++++++++++++++++++++++++++++++ languages.toml | 1 + 2 files changed, 32 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 5c26a5b2a5f5..260519a8cb78 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -400,6 +400,7 @@ impl MappableCommand { decrement, "Decrement", record_macro, "Record macro", replay_macro, "Replay macro", + show_subtree, "Show tree-sitter subtree under primary selection", ); } @@ -6210,3 +6211,33 @@ 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().slice(..); + 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("tree", contents); + if let Some(doc_popup) = compositor.find_id("tree") { + *doc_popup = popup; + } else { + compositor.push(Box::new(popup)); + } + }, + )); + } + } +} diff --git a/languages.toml b/languages.toml index 3e2e7b15c730..92602f0a7adb 100644 --- a/languages.toml +++ b/languages.toml @@ -377,6 +377,7 @@ scope = "source.tsq" file-types = ["scm"] roots = [] comment-token = ";" +injection-regex = "tsq" indent = { tab-width = 2, unit = " " } [[language]] From a711829715abbfb5eb303132c296f2836630d051 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Fri, 7 Jan 2022 18:36:35 -0600 Subject: [PATCH 2/3] remove '.slice(..)' from show_subtree command --- helix-term/src/commands.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 260519a8cb78..84794d0dbeef 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -6217,7 +6217,7 @@ fn show_subtree(cx: &mut Context) { if let Some(syntax) = doc.syntax() { let primary_selection = doc.selection(view.id).primary(); - let text = doc.text().slice(..); + 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 From 0d4f59c98d4ed3332733bd2d2a06b57c723d0e0d Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 10 Jan 2022 09:30:49 -0600 Subject: [PATCH 3/3] name docs and subtree Popups 'hover' --- helix-term/src/commands.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 84794d0dbeef..a1a7b88cc099 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5389,8 +5389,8 @@ fn hover(cx: &mut Context) { // skip if contents empty let contents = ui::Markdown::new(contents, editor.syn_loader.clone()); - let popup = Popup::new("documentation", contents); - if let Some(doc_popup) = compositor.find_id("documentation") { + let popup = Popup::new("hover", contents); + if let Some(doc_popup) = compositor.find_id("hover") { *doc_popup = popup; } else { compositor.push(Box::new(popup)); @@ -6230,8 +6230,8 @@ fn show_subtree(cx: &mut Context) { 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("tree", contents); - if let Some(doc_popup) = compositor.find_id("tree") { + let popup = Popup::new("hover", contents); + if let Some(doc_popup) = compositor.find_id("hover") { *doc_popup = popup; } else { compositor.push(Box::new(popup));