Skip to content

Commit 97dd38e

Browse files
committed
custom node formatting rather than
1 parent a3ed916 commit 97dd38e

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

helix-term/src/commands/typed.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::ops::Deref;
1+
use std::{fmt::Write, ops::Deref};
22

33
use super::*;
44

5+
use helix_core::tree_sitter::TreeCursor;
56
use helix_view::{
67
apply_transaction,
78
editor::{Action, CloseError, ConfigEvent},
@@ -1473,7 +1474,55 @@ fn tree_sitter_subtree(
14731474
.root_node()
14741475
.descendant_for_byte_range(from, to)
14751476
{
1476-
let contents = format!("```tsq\n{}\n```", selected_node.to_sexp());
1477+
fn pretty_print_node(
1478+
node: Node<'_>,
1479+
sexpr: &mut String,
1480+
is_root: bool,
1481+
field_name: Option<&str>,
1482+
depth: usize,
1483+
) {
1484+
fn is_visible(node: Node<'_>) -> bool {
1485+
node.is_missing()
1486+
|| (node.is_named() && node.language().node_kind_is_visible(node.kind_id()))
1487+
}
1488+
1489+
if is_visible(node) {
1490+
write!(sexpr, "{:depth$}", "");
1491+
1492+
if let Some(field_name) = field_name {
1493+
write!(sexpr, "{}: ", field_name);
1494+
}
1495+
1496+
write!(sexpr, "({}", node.kind());
1497+
} else if is_root {
1498+
write!(sexpr, "(\"{}\")", node.kind());
1499+
}
1500+
1501+
for child_idx in 0..node.child_count() {
1502+
if let Some(child) = node.child(child_idx) {
1503+
if is_visible(child) {
1504+
sexpr.push('\n');
1505+
}
1506+
1507+
pretty_print_node(
1508+
child,
1509+
sexpr,
1510+
false,
1511+
node.field_name_for_child(child_idx as u32),
1512+
depth + 2,
1513+
);
1514+
}
1515+
}
1516+
1517+
if is_visible(node) {
1518+
write!(sexpr, ")");
1519+
}
1520+
}
1521+
1522+
let mut sexpr = String::new();
1523+
pretty_print_node(selected_node, &mut sexpr, true, None, 0);
1524+
1525+
let contents = format!("```tsq\n{}\n```", sexpr);
14771526

14781527
let callback = async move {
14791528
let call: job::Callback =

0 commit comments

Comments
 (0)