Skip to content

Commit

Permalink
Escape double quotes for anonymous nodes in :tree-sitter-subtree
Browse files Browse the repository at this point in the history
If the anonymous node contained a double quote it would throw off the
highlighting.
  • Loading branch information
the-mikedavis committed Jan 2, 2025
1 parent c9cc147 commit 38e8382
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2666,12 +2666,20 @@ fn node_is_visible(node: &Node) -> bool {
node.is_missing() || (node.is_named() && node.language().node_kind_is_visible(node.kind_id()))
}

fn format_anonymous_node_kind(kind: &str) -> Cow<str> {
if kind.contains('"') {
Cow::Owned(kind.replace('"', "\\\""))
} else {
Cow::Borrowed(kind)
}
}

pub fn pretty_print_tree<W: fmt::Write>(fmt: &mut W, node: Node) -> fmt::Result {
if node.child_count() == 0 {
if node_is_visible(&node) {
write!(fmt, "({})", node.kind())
} else {
write!(fmt, "\"{}\"", node.kind())
write!(fmt, "\"{}\"", format_anonymous_node_kind(node.kind()))
}
} else {
pretty_print_tree_impl(fmt, &mut node.walk(), 0)
Expand All @@ -2696,7 +2704,7 @@ fn pretty_print_tree_impl<W: fmt::Write>(

write!(fmt, "({}", node.kind())?;
} else {
write!(fmt, " \"{}\"", node.kind())?;
write!(fmt, " \"{}\"", format_anonymous_node_kind(node.kind()))?;
}

// Handle children.
Expand Down

0 comments on commit 38e8382

Please sign in to comment.