Skip to content

Commit 64d3e7b

Browse files
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'
1 parent dd1f64d commit 64d3e7b

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

helix-term/src/commands.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ impl MappableCommand {
400400
decrement, "Decrement",
401401
record_macro, "Record macro",
402402
replay_macro, "Replay macro",
403+
show_subtree, "Show tree-sitter subtree under primary selection",
403404
);
404405
}
405406

@@ -5388,8 +5389,8 @@ fn hover(cx: &mut Context) {
53885389
// skip if contents empty
53895390

53905391
let contents = ui::Markdown::new(contents, editor.syn_loader.clone());
5391-
let popup = Popup::new("documentation", contents);
5392-
if let Some(doc_popup) = compositor.find_id("documentation") {
5392+
let popup = Popup::new("hover", contents);
5393+
if let Some(doc_popup) = compositor.find_id("hover") {
53935394
*doc_popup = popup;
53945395
} else {
53955396
compositor.push(Box::new(popup));
@@ -6210,3 +6211,33 @@ fn replay_macro(cx: &mut Context) {
62106211
},
62116212
));
62126213
}
6214+
6215+
fn show_subtree(cx: &mut Context) {
6216+
let (view, doc) = current!(cx.editor);
6217+
6218+
if let Some(syntax) = doc.syntax() {
6219+
let primary_selection = doc.selection(view.id).primary();
6220+
let text = doc.text();
6221+
let from = text.char_to_byte(primary_selection.from());
6222+
let to = text.char_to_byte(primary_selection.to());
6223+
if let Some(selected_node) = syntax
6224+
.tree()
6225+
.root_node()
6226+
.descendant_for_byte_range(from, to)
6227+
{
6228+
let contents = format!("```tsq\n{}\n```", selected_node.to_sexp());
6229+
6230+
cx.callback = Some(Box::new(
6231+
move |compositor: &mut Compositor, cx: &mut compositor::Context| {
6232+
let contents = ui::Markdown::new(contents, cx.editor.syn_loader.clone());
6233+
let popup = Popup::new("hover", contents);
6234+
if let Some(doc_popup) = compositor.find_id("hover") {
6235+
*doc_popup = popup;
6236+
} else {
6237+
compositor.push(Box::new(popup));
6238+
}
6239+
},
6240+
));
6241+
}
6242+
}
6243+
}

languages.toml

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ scope = "source.tsq"
378378
file-types = ["scm"]
379379
roots = []
380380
comment-token = ";"
381+
injection-regex = "tsq"
381382
indent = { tab-width = 2, unit = " " }
382383

383384
[[language]]

0 commit comments

Comments
 (0)