Skip to content

Commit

Permalink
add show_subtree command for viewing tree-sitter subtree in Popup (#1453
Browse files Browse the repository at this point in the history
)

* add show_subtree command for viewing tree-sitter subtree in Popup

* remove '.slice(..)' from show_subtree command

* name docs and subtree Popups 'hover'
  • Loading branch information
the-mikedavis authored Jan 16, 2022
1 parent dd1f64d commit 64d3e7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
35 changes: 33 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
}
},
));
}
}
}
1 change: 1 addition & 0 deletions languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ scope = "source.tsq"
file-types = ["scm"]
roots = []
comment-token = ";"
injection-regex = "tsq"
indent = { tab-width = 2, unit = " " }

[[language]]
Expand Down

0 comments on commit 64d3e7b

Please sign in to comment.