From 66d08e4f729e9935f346e43ffe84a446004841db Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Fri, 30 Jun 2023 08:48:12 -0700 Subject: [PATCH] WIP store probably_punctuation on unchanged edges --- sample_files/comma_and_comment_after.js | 1 + sample_files/comma_and_comment_before.js | 1 + src/diff/dijkstra.rs | 4 ++++ src/diff/graph.rs | 20 ++++++++++++++++++-- 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 sample_files/comma_and_comment_after.js create mode 100644 sample_files/comma_and_comment_before.js diff --git a/sample_files/comma_and_comment_after.js b/sample_files/comma_and_comment_after.js new file mode 100644 index 0000000000..8987b718a0 --- /dev/null +++ b/sample_files/comma_and_comment_after.js @@ -0,0 +1 @@ +funName(1 /* kinda like bar */ , /* foo */) diff --git a/sample_files/comma_and_comment_before.js b/sample_files/comma_and_comment_before.js new file mode 100644 index 0000000000..859e9fe38a --- /dev/null +++ b/sample_files/comma_and_comment_before.js @@ -0,0 +1 @@ +funName(1 /* foo */ , /* bar */) diff --git a/src/diff/dijkstra.rs b/src/diff/dijkstra.rs index 96e78372e6..9f84b1fa6f 100644 --- a/src/diff/dijkstra.rs +++ b/src/diff/dijkstra.rs @@ -282,6 +282,7 @@ mod tests { assert_eq!( actions, vec![UnchangedNode { + probably_punctuation: false, depth_difference: 0 }] ); @@ -423,9 +424,11 @@ mod tests { EnterNovelDelimiterRHS { contiguous: false }, EnterNovelDelimiterLHS { contiguous: false }, UnchangedNode { + probably_punctuation: false, depth_difference: 0 }, UnchangedNode { + probably_punctuation: false, depth_difference: 0 }, ], @@ -459,6 +462,7 @@ mod tests { actions, vec![ UnchangedNode { + probably_punctuation: false, depth_difference: 0 }, NovelAtomLHS { diff --git a/src/diff/graph.rs b/src/diff/graph.rs index 89ce09b190..6f321dd49b 100644 --- a/src/diff/graph.rs +++ b/src/diff/graph.rs @@ -276,6 +276,7 @@ impl<'s, 'b> Vertex<'s, 'b> { pub enum Edge { UnchangedNode { depth_difference: u32, + probably_punctuation: bool, }, EnterUnchangedDelimiter { depth_difference: u32, @@ -307,7 +308,10 @@ impl Edge { pub fn cost(self) -> u32 { match self { // Matching nodes is always best. - UnchangedNode { depth_difference } => min(40, depth_difference + 1), + UnchangedNode { + depth_difference, + probably_punctuation, + } => min(40, depth_difference + 1) + if probably_punctuation { 2000 } else { 0 }, // Matching an outer delimiter is good. EnterUnchangedDelimiter { depth_difference } => 100 + min(40, depth_difference), @@ -406,6 +410,13 @@ fn looks_like_punctuation(content: &str) -> bool { content == "," || content == ";" || content == "." } +fn looks_like_punctuation_atom(node: &Syntax) -> bool { + match node { + Syntax::Atom { content, .. } => content == "," || content == ";" || content == ".", + _ => false, + } +} + /// Pop as many parents of `lhs_node` and `rhs_node` as /// possible. Return the new syntax nodes and parents. fn pop_all_parents<'s>( @@ -493,6 +504,8 @@ pub fn get_set_neighbours<'s, 'b>( - rhs_syntax.num_ancestors() as i32) .unsigned_abs(); + let probably_punctuation = looks_like_punctuation_atom(lhs_syntax); + // Both nodes are equal, the happy case. let (lhs_syntax, rhs_syntax, lhs_parent_id, rhs_parent_id, parents) = pop_all_parents( lhs_syntax.next_sibling(), @@ -503,7 +516,10 @@ pub fn get_set_neighbours<'s, 'b>( ); res.push(( - UnchangedNode { depth_difference }, + UnchangedNode { + depth_difference, + probably_punctuation, + }, allocate_if_new( Vertex { neighbours: RefCell::new(None),