Skip to content

Commit 2d1aa95

Browse files
committed
custom node formatting rather than
1 parent a3ed916 commit 2d1aa95

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

helix-term/src/commands/typed.rs

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

33
use super::*;
44

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

14781528
let callback = async move {
14791529
let call: job::Callback =

0 commit comments

Comments
 (0)