From 38e8382b01e1b3ddcc8e55b5705deeebf4644e79 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Thu, 2 Jan 2025 15:33:48 -0500 Subject: [PATCH] Escape double quotes for anonymous nodes in :tree-sitter-subtree If the anonymous node contained a double quote it would throw off the highlighting. --- helix-core/src/syntax.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 6ddf433cb7c6..58b6de343f0b 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -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 { + if kind.contains('"') { + Cow::Owned(kind.replace('"', "\\\"")) + } else { + Cow::Borrowed(kind) + } +} + pub fn pretty_print_tree(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) @@ -2696,7 +2704,7 @@ fn pretty_print_tree_impl( write!(fmt, "({}", node.kind())?; } else { - write!(fmt, " \"{}\"", node.kind())?; + write!(fmt, " \"{}\"", format_anonymous_node_kind(node.kind()))?; } // Handle children.