Skip to content

Commit

Permalink
WIP store probably_punctuation on unchanged edges
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Jun 30, 2023
1 parent 3730580 commit abf8836
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/diff/dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ mod tests {
assert_eq!(
actions,
vec![UnchangedNode {
probably_punctuation: false,
depth_difference: 0
}]
);
Expand Down Expand Up @@ -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
},
],
Expand Down Expand Up @@ -459,6 +462,7 @@ mod tests {
actions,
vec![
UnchangedNode {
probably_punctuation: false,
depth_difference: 0
},
NovelAtomLHS {
Expand Down
20 changes: 18 additions & 2 deletions src/diff/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ impl<'s, 'b> Vertex<'s, 'b> {
pub enum Edge {
UnchangedNode {
depth_difference: u32,
probably_punctuation: bool,
},
EnterUnchangedDelimiter {
depth_difference: u32,
Expand Down Expand Up @@ -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),

Expand Down Expand Up @@ -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>(
Expand Down Expand Up @@ -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(),
Expand All @@ -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),
Expand Down

0 comments on commit abf8836

Please sign in to comment.