on master help sign in
change show_subtree command into ':tree-sitter-subtree' typable command (#1524)
f453f87Michael Davis
book/src/generated/typable-cmd.md+1
@@ -45,3 +45,4 @@
45| `:set-option`, `:set` | Set a config option at runtime |
45| `:set-option`, `:set` | Set a config option at runtime |
46| `:sort` | Sort ranges in selection. |
46| `:sort` | Sort ranges in selection. |
47| `:rsort` | Sort ranges in selection in reverse order. |
47| `:rsort` | Sort ranges in selection in reverse order. |
48+| `:tree-sitter-subtree`, `:ts-subtree` | Display tree sitter subtree under cursor, primarily for debugging queries. |
helix-term/src/commands.rs+47−31
@@ -403,7 +403,6 @@ impl MappableCommand {
403 decrement, "Decrement",
403 decrement, "Decrement",
404 record_macro, "Record macro",
404 record_macro, "Record macro",
405 replay_macro, "Replay macro",
405 replay_macro, "Replay macro",
406− show_subtree, "Show tree-sitter subtree under primary selection",
407 );
406 );
408}
407}
409
408
@@ -2766,6 +2765,46 @@ pub mod cmd {
2766 Ok(())
2765 Ok(())
2767 }
2766 }
2768
2767
2768+ fn tree_sitter_subtree(
2769+ cx: &mut compositor::Context,
2770+ _args: &[Cow<str>],
2771+ _event: PromptEvent,
2772+ ) -> anyhow::Result<()> {
2773+ let (view, doc) = current!(cx.editor);
2774+
2775+ if let Some(syntax) = doc.syntax() {
2776+ let primary_selection = doc.selection(view.id).primary();
2777+ let text = doc.text();
2778+ let from = text.char_to_byte(primary_selection.from());
2779+ let to = text.char_to_byte(primary_selection.to());
2780+ if let Some(selected_node) = syntax
2781+ .tree()
2782+ .root_node()
2783+ .descendant_for_byte_range(from, to)
2784+ {
2785+ let contents = format!("```tsq\n{}\n```", selected_node.to_sexp());
2786+
2787+ let callback = async move {
2788+ let call: job::Callback =
2789+ Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
2790+ let contents = ui::Markdown::new(contents, editor.syn_loader.clone());
2791+ let popup = Popup::new("hover", contents);
2792+ if let Some(doc_popup) = compositor.find_id("hover") {
2793+ *doc_popup = popup;
2794+ } else {
2795+ compositor.push(Box::new(popup));
2796+ }
2797+ });
2798+ Ok(call)
2799+ };
2800+
2801+ cx.jobs.callback(callback);
2802+ }
2803+ }
2804+
2805+ Ok(())
2806+ }
2807+
2769 pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
2808 pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
2770 TypableCommand {
2809 TypableCommand {
2771 name: "quit",
2810 name: "quit",
@@ -3082,6 +3121,13 @@ pub mod cmd {
3082 fun: sort_reverse,
3121 fun: sort_reverse,
3083 completer: None,
3122 completer: None,
3084 },
3123 },
3124+ TypableCommand {
3125+ name: "tree-sitter-subtree",
3126+ aliases: &["ts-subtree"],
3127+ doc: "Display tree sitter subtree under cursor, primarily for debugging queries.",
3128+ fun: tree_sitter_subtree,
3129+ completer: None,
3130+ },
3085 ];
3131 ];
3086
3132
3087 pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
3133 pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
@@ -6252,33 +6298,3 @@ fn replay_macro(cx: &mut Context) {
6252 },
6298 },
6253 ));
6299 ));
6254}
6300}
6255−
6256−fn show_subtree(cx: &mut Context) {
6257− let (view, doc) = current!(cx.editor);
6258−
6259− if let Some(syntax) = doc.syntax() {
6260− let primary_selection = doc.selection(view.id).primary();
6261− let text = doc.text();
6262− let from = text.char_to_byte(primary_selection.from());
6263− let to = text.char_to_byte(primary_selection.to());
6264− if let Some(selected_node) = syntax
6265− .tree()
6266− .root_node()
6267− .descendant_for_byte_range(from, to)
6268− {
6269− let contents = format!("```tsq\n{}\n```", selected_node.to_sexp());
6270−
6271− cx.callback = Some(Box::new(
6272− move |compositor: &mut Compositor, cx: &mut compositor::Context| {
6273− let contents = ui::Markdown::new(contents, cx.editor.syn_loader.clone());
6274− let popup = Popup::new("hover", contents);
6275− if let Some(doc_popup) = compositor.find_id("hover") {
6276− *doc_popup = popup;
6277− } else {
6278− compositor.push(Box::new(popup));
6279− }
6280− },
6281− ));
6282− }
6283− }
6284−}