Skip to content

Commit

Permalink
Experiment with an empty delimiter heuristc on changed lists
Browse files Browse the repository at this point in the history
See #587
  • Loading branch information
Wilfred committed Jul 21, 2024
1 parent 78650c3 commit 2fd9aa4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 17 deletions.
5 changes: 5 additions & 0 deletions a1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test(a, b):
if a:
print(a)
if b:
print(b)
5 changes: 5 additions & 0 deletions a2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test(a, b):
if a:
print(a)
if b:
print(b)
28 changes: 14 additions & 14 deletions sample_files/compare.expected
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ sample_files/css_1.css sample_files/css_2.css
4b7101d68f953344af5c8839e7b13a56 -

sample_files/dart_1.dart sample_files/dart_2.dart
631b39f8574990923d4ac3d541e2966d -
84c93c4ee4794c1164e0ccbe9796ea2a -

sample_files/devicetree_1.dts sample_files/devicetree_2.dts
77f13f1cff53b996778d142a94fe58f6 -
Expand All @@ -65,7 +65,7 @@ sample_files/elisp_contiguous_1.el sample_files/elisp_contiguous_2.el
beaf7d6c5136d3db7a36ff49a131b251 -

sample_files/elm_1.elm sample_files/elm_2.elm
33b71893107538cff574276f2837adbb -
75b2883821ac30f35707ecbf868927fa -

sample_files/elvish_1.elv sample_files/elvish_2.elv
f80b47646e7dd2bd3a49393d00657465 -
Expand All @@ -83,13 +83,13 @@ sample_files/hare_1.ha sample_files/hare_2.ha
6ac97614325dd9b7ebb2b3cc1e4d3296 -

sample_files/haskell_1.hs sample_files/haskell_2.hs
9e6e08f67a41c963250bc1748c4824ce -
b74430633efe6be3bf6330f6d8ab824f -

sample_files/hcl_1.hcl sample_files/hcl_2.hcl
4c3dc042f8cd57fec0ea883274b5f94e -

sample_files/hello_world_1.smali sample_files/hello_world_2.smali
296286b067e86cccfad1fa9a0440993b -
570555eaaada48189d921573a06b7a72 -

sample_files/helpful_1.el sample_files/helpful_2.el
056471124ae2e582942e214ce9222477 -
Expand Down Expand Up @@ -128,13 +128,13 @@ sample_files/json_1.json sample_files/json_2.json
d2c1aea9ec9be1078e4b70be4c9e1b13 -

sample_files/jsx_1.jsx sample_files/jsx_2.jsx
712c1c17f17f64745395e3d558ab53f3 -
2ab44912902865a6d408c6998bcd39ac -

sample_files/julia_1.jl sample_files/julia_2.jl
9782f864fc462c9f873760f71c43c48d -

sample_files/load_1.js sample_files/load_2.js
22365043359164f717d495336d86b996 -
557d6a9f82a116e75c22be5419053a5d -

sample_files/long_line_1.txt sample_files/long_line_2.txt
7fc50bd547f0c20fda89a1931e5eb61e -
Expand All @@ -161,13 +161,13 @@ sample_files/multiline_string_1.ml sample_files/multiline_string_2.ml
d8d0b40bad1697ca143709d5ab46869f -

sample_files/nest_1.rs sample_files/nest_2.rs
d3a799fe2cd9d81aa251c96af5cd9711 -
bc148be13151c0eda4b4ebde0c44f2d7 -

sample_files/nested_slider_1.el sample_files/nested_slider_2.el
232c0736336031d762a1453f323e8064 -

sample_files/nested_slider_1.rs sample_files/nested_slider_2.rs
e9045007a25352eb81655458aebb452c -
77b17c0b44ddcc1fd108511984fd720f -

sample_files/nesting_1.el sample_files/nesting_2.el
fe12489ab4bb156c0aee478dd36f264a -
Expand Down Expand Up @@ -206,10 +206,10 @@ sample_files/preprocesor_1.h sample_files/preprocesor_2.h
a680158a7980d738ba831aab3a63165d -

sample_files/qml_1.qml sample_files/qml_2.qml
c23bda1d924adb57eb9e5c20744e99ea -
98624e3aaad26b552c6d74db37ddb039 -

sample_files/r_1.R sample_files/r_2.R
10f45a80a8554419bf30a2a0f574ab86 -
cd55e900a72253e1bba6212d0dd3261d -

sample_files/racket_1.rkt sample_files/racket_2.rkt
605aec93fa7b89d08e5d8ed56ad3c1be -
Expand Down Expand Up @@ -239,7 +239,7 @@ sample_files/slider_at_end_1.json sample_files/slider_at_end_2.json
cb370f1c0ccc5e155743330629f899f0 -

sample_files/slow_1.rs sample_files/slow_2.rs
2e76c128e008d5d255fdd29562208abc -
09fde8fdc933ebb95c7dec36fe9a53ff -

sample_files/small_1.js sample_files/small_2.js
6bd2d5c9e2b843cfe0748355f3203de9 -
Expand Down Expand Up @@ -269,7 +269,7 @@ sample_files/text_1.txt sample_files/text_2.txt
87914b8ef4a6b042054633ddb182ce02 -

sample_files/todomvc_1.gleam sample_files/todomvc_2.gleam
0a6f8335944adbd779c93aa52b7a30c5 -
723951e8d4b377a595a8392f91e1375f -

sample_files/toml_1.toml sample_files/toml_2.toml
df36cf07bb1b56fbdbcea7f651b4344d -
Expand All @@ -281,13 +281,13 @@ sample_files/typescript_1.ts sample_files/typescript_2.ts
fee7ee33d2037ad1941ba6bb5532a1db -

sample_files/typing_1.ml sample_files/typing_2.ml
36161bd77a8c86643bc90656ec41c92c -
80f23aba9aa1bfe0460855451ecde74c -

sample_files/utf16_1.py sample_files/utf16_2.py
39014a682ed2318f980c7ea4177cf659 -

sample_files/vhdl_1.vhd sample_files/vhdl_2.vhd
0f4de895a568092795c87f3d0a8a07e8 -
9ef53ef84f9a750b01db0ef6bde254cc -

sample_files/whitespace_1.tsx sample_files/whitespace_2.tsx
ac8b1a89ac26333f2d4e9433b2ca3958 -
Expand Down
64 changes: 61 additions & 3 deletions src/diff/sliders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@
use line_numbers::SingleLineSpan;

use crate::{
diff::changes::{insert_deep_novel, insert_deep_unchanged, ChangeKind::*, ChangeMap},
parse::guess_language,
parse::syntax::Syntax::{self, *},
diff::changes::{
insert_deep_novel, insert_deep_unchanged,
ChangeKind::{self, *},
ChangeMap,
},
parse::{
guess_language,
syntax::Syntax::{self, *},
},
};

pub(crate) fn fix_all_sliders<'a>(
Expand All @@ -47,6 +53,58 @@ pub(crate) fn fix_all_sliders<'a>(
fix_all_sliders_one_step(nodes, change_map);

fix_all_nested_sliders(language, nodes, change_map);

fix_invisible_delims(nodes, change_map);
}

fn fix_invisible_delims<'a>(nodes: &[&'a Syntax<'a>], change_map: &mut ChangeMap<'a>) {
for node in nodes {
match node {
List {
open_content,
children,
close_content,
..
} => {
if let Some(change_kind) = change_map.get(node) {
if open_content == ""
&& close_content == ""
&& matches!(change_kind, ChangeKind::Novel)
{
let mut all_children_unchanged = true;
for child in children {
let Some(child_change) = change_map.get(child) else {
continue;
};

if !matches!(child_change, ChangeKind::Unchanged(_)) {
all_children_unchanged = false;
break;
}
}

if all_children_unchanged {
for child in children {
let Some(child_change) = change_map.get(child) else {
continue;
};

let Unchanged(opposite_child) = child_change else {
continue;
};

change_map.insert(child, ChangeKind::Novel);
change_map.insert(opposite_child, ChangeKind::Novel);
}
}
} else {
fix_invisible_delims(children, change_map);
}
}
}
Atom { .. } => {}
}
}
}

/// Should nester slider correction prefer the inner or outer
Expand Down

0 comments on commit 2fd9aa4

Please sign in to comment.