From de620366d6a9e2d3d4397805750c00901c673e33 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/display/json.rs | 1 + src/display/style.rs | 2 +- src/parse/syntax.rs | 15 +++++++++++++-- src/parse/tree_sitter_parser.rs | 9 +++------ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/display/json.rs b/src/display/json.rs index 2dc867dee1..25dac1d715 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 ff63c4da2b..ff55d0fde3 100644 --- a/src/display/style.rs +++ b/src/display/style.rs @@ -346,7 +346,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 bdde5934d5..901c10940c 100644 --- a/src/parse/syntax.rs +++ b/src/parse/syntax.rs @@ -391,8 +391,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()), @@ -579,6 +585,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 `{`. diff --git a/src/parse/tree_sitter_parser.rs b/src/parse/tree_sitter_parser.rs index 5ae72535cd..6245338a7f 100644 --- a/src/parse/tree_sitter_parser.rs +++ b/src/parse/tree_sitter_parser.rs @@ -1875,15 +1875,12 @@ fn list_from_cursor<'a>( if should_ignore_last_child(config, &root_node, &between_delim) { if let Some(last_child) = between_delim.pop() { if let Syntax::Atom { - position, - content, - kind, - .. + position, content, .. } = last_child { let position = position.clone(); - // TODO: New unused kind for atoms. - let new_last_child = Syntax::new_atom(arena, position, content, *kind); + let new_last_child = + Syntax::new_atom(arena, position, content, AtomKind::CanIgnore); between_delim.push(new_last_child); } }