Skip to content

Commit

Permalink
Use CanIgnore atom kind
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Aug 1, 2024
1 parent f1e2de9 commit 5478323
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/diff/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/display/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/display/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub(crate) fn color_positions(
style = style.bold();
}
AtomKind::TreeSitterError => style = style.purple(),
AtomKind::Normal => {}
AtomKind::Normal | AtomKind::CanIgnore => {}
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/parse/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,14 @@ fn set_content_id(nodes: &[&Syntax], existing: &mut DftHashMap<ContentKey, u32>)
// 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()),
Expand Down Expand Up @@ -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 `{`.
Expand Down
9 changes: 3 additions & 6 deletions src/parse/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 5478323

Please sign in to comment.