Unnamed repository; edit this file 'description' to name the repository.
Escape double quotes for anonymous nodes in :tree-sitter-subtree
If the anonymous node contained a double quote it would throw off the highlighting.
Michael Davis 2025-01-03
parent c9cc147 · commit 38e8382
-rw-r--r--helix-core/src/syntax.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs
index 6ddf433c..58b6de34 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<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)
@@ -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.