Skip to content

Commit

Permalink
Don't flatten error nodes
Browse files Browse the repository at this point in the history
As of b9d44ae (released in 0.38), difftastic prefers a textual
diff if any parse errors are present. As a result, users have to
opt-in to allowing parse errors. We can now preserve structure to
produce diffs that are more granular, at the slight risk of changes
being missed if parsers discard tokens.

Fixes #703
  • Loading branch information
Wilfred committed Apr 29, 2024
1 parent 7f5c11c commit 38f148d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
## 0.58 (unreleased)

### Parsing

Difftastic now preserves tree-sitter parse tree structure on parse
error nodes. This reverts the flattening behaviour introduced in
0.38. Preserving structure tends to produce better diffs, although it
increases the risk that difftastic will show fewer changes in the
presence of parse errors.

Since difftastic is now conservative with parse errors
(DFT_PARSE_ERROR_LIMIT is 0 by default), this seems like a better
tradeoff.

### Diffing

`--strip-cr` now defaults to `on`, so comparing a file with CRLF
Expand Down
4 changes: 2 additions & 2 deletions sample_files/compare.expected
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ sample_files/lua_before.lua sample_files/lua_after.lua
9f5c85cd6806c724c84afa805da76bb7 -

sample_files/makefile_before.mk sample_files/makefile_after.mk
7eea0d919b71161c72a4ef496fff749d -
20cf6b508d1707e41e5450572ee51838 -

sample_files/many_newlines_before.txt sample_files/many_newlines_after.txt
afc8628f89076423d1453d8e3c84ac92 -
Expand Down Expand Up @@ -254,7 +254,7 @@ sample_files/tab_before.txt sample_files/tab_after.txt
c7fcee21dd5ebd251e2cf4cd7d4446d2 -

sample_files/tailwind_before.css sample_files/tailwind_after.css
db01dde1e8455a144e5bf0511273f674 -
be0cba204957b62a30e99fd03f945de5 -

sample_files/text_before.txt sample_files/text_after.txt
5fa6a472ccdeda0142eda3edcaeaa189 -
Expand Down
5 changes: 2 additions & 3 deletions src/parse/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,10 +1608,9 @@ fn syntax_from_cursor<'a>(

if node.is_error() {
*error_count += 1;
}

// Treat error nodes as atoms, even if they have children.
atom_from_cursor(arena, src, nl_pos, cursor, highlights, ignore_comments)
} else if config.atom_nodes.contains(node.kind()) {
if config.atom_nodes.contains(node.kind()) {
// Treat nodes like string literals as atoms, regardless
// of whether they have children.
atom_from_cursor(arena, src, nl_pos, cursor, highlights, ignore_comments)
Expand Down

0 comments on commit 38f148d

Please sign in to comment.