From 1fab9630cbdc8954d18b67edf0cd2fbcd9e88d73 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Sat, 29 Jun 2024 23:02:31 -0700 Subject: [PATCH] Use CanIgnore atom kind --- src/diff/changes.rs | 1 + src/display/json.rs | 1 + src/display/style.rs | 2 +- src/parse/syntax.rs | 15 +++++++++++++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/diff/changes.rs b/src/diff/changes.rs index dd6028079..eac05dc8c 100644 --- a/src/diff/changes.rs +++ b/src/diff/changes.rs @@ -47,6 +47,7 @@ pub(crate) fn insert_deep_unchanged<'a>( }, ) => { for (child, opposite_child) in node_children.iter().zip(opposite_children) { + // TODO: one side may now be CanIgnore insert_deep_unchanged(child, opposite_child, change_map); } } diff --git a/src/display/json.rs b/src/display/json.rs index 22851cd31..e637eea96 100644 --- a/src/display/json.rs +++ b/src/display/json.rs @@ -267,6 +267,7 @@ impl Highlight { AtomKind::Comment => Highlight::Comment, AtomKind::Type => Highlight::Type, AtomKind::Normal => Highlight::Normal, + AtomKind::CanIgnore => Highlight::Normal, AtomKind::TreeSitterError => Highlight::TreeSitterError, }, } diff --git a/src/display/style.rs b/src/display/style.rs index 8f31db145..4167b4cab 100644 --- a/src/display/style.rs +++ b/src/display/style.rs @@ -359,7 +359,7 @@ pub(crate) fn color_positions( style = style.bold(); } AtomKind::TreeSitterError => style = style.purple(), - AtomKind::Normal => {} + AtomKind::Normal | AtomKind::CanIgnore => {} } } } diff --git a/src/parse/syntax.rs b/src/parse/syntax.rs index aeefb2866..bd3e6d679 100644 --- a/src/parse/syntax.rs +++ b/src/parse/syntax.rs @@ -433,8 +433,14 @@ fn set_content_id(nodes: &[&Syntax], existing: &mut DftHashMap) // Recurse first, so children all have their content_id set. set_content_id(children, existing); - let children_content_ids: Vec<_> = - children.iter().map(|c| c.info().content_id.get()).collect(); + let children_content_ids: Vec<_> = children + .iter() + .filter(|c| match c { + List { .. } => true, + Atom { kind, .. } => *kind != AtomKind::CanIgnore, + }) + .map(|c| c.info().content_id.get()) + .collect(); ( Some(open_content.clone()), @@ -620,6 +626,11 @@ pub(crate) enum AtomKind { Comment, Keyword, TreeSitterError, + /// Trailing commas can be ignored in some positions, such as the + /// last comma in `[1, 2,]` in JS. However, it's not obligatory, + /// and it's useful when diffing `[1,]` against `[1, 2]` to be + /// able to match up the commas. + CanIgnore, } /// Unlike atoms, tokens can be delimiters like `{`.