diff options
author | Michael Davis | 2022-01-16 01:26:09 +0000 |
---|---|---|
committer | GitHub | 2022-01-16 01:26:09 +0000 |
commit | 64d3e7b705341fef8af7f6cb5bbff5848489abd6 (patch) | |
tree | 6ffd4c8f5327e4df6098b0a71de953fe17ad0efc /helix-term | |
parent | dd1f64d4dc6fa7a5a8f0b6404e7d30cedcb5b10a (diff) |
add show_subtree command for viewing tree-sitter subtree in Popup (#1453)
* add show_subtree command for viewing tree-sitter subtree in Popup
* remove '.slice(..)' from show_subtree command
* name docs and subtree Popups 'hover'
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/commands.rs | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 71ac8f09..1a53f14e 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", ); } @@ -5388,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)); @@ -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(); + 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)); + } + }, + )); + } + } +} |