summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2022-04-24 11:46:30 -0700
committerWilfred Hughes <me@wilfred.me.uk>2022-04-24 11:46:30 -0700
commit6a900f4a88c5c41fd27b5afc2a937af37dce0337 (patch)
tree79101790002c3689eaf663c6d942138911cd4214
parent950cfbebfb51011a17ae023c4ec60f851367c3d6 (diff)
Remove .change accessor
-rw-r--r--src/dijkstra.rs9
-rw-r--r--src/main.rs4
-rw-r--r--src/sliders.rs74
-rw-r--r--src/syntax.rs31
-rw-r--r--src/unchanged.rs52
5 files changed, 92 insertions, 78 deletions
diff --git a/src/dijkstra.rs b/src/dijkstra.rs
index e232f39f52..822b9d9a4e 100644
--- a/src/dijkstra.rs
+++ b/src/dijkstra.rs
@@ -565,8 +565,9 @@ mod tests {
let mut change_map = new_change_map();
mark_syntax(Some(lhs), Some(rhs), &mut change_map);
- assert_eq!(lhs.change(), Some(ChangeKind::Unchanged(rhs)));
- assert_eq!(rhs.change(), Some(ChangeKind::Unchanged(lhs)));
+
+ assert_eq!(change_map.get(lhs), Some(&ChangeKind::Unchanged(rhs)));
+ assert_eq!(change_map.get(rhs), Some(&ChangeKind::Unchanged(lhs)));
}
#[test]
@@ -578,7 +579,7 @@ mod tests {
let mut change_map = new_change_map();
mark_syntax(Some(lhs), Some(rhs), &mut change_map);
- assert_eq!(lhs.change(), Some(ChangeKind::Novel));
- assert_eq!(rhs.change(), Some(ChangeKind::Novel));
+ assert_eq!(change_map.get(lhs), Some(&ChangeKind::Novel));
+ assert_eq!(change_map.get(rhs), Some(&ChangeKind::Novel));
}
}
diff --git a/src/main.rs b/src/main.rs
index c5568277de..d7e07b39f8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -344,8 +344,8 @@ fn diff_file_content(
fix_all_sliders(language, &rhs_section_nodes, &mut change_map);
}
- let lhs_positions = syntax::change_positions(&lhs);
- let rhs_positions = syntax::change_positions(&rhs);
+ let lhs_positions = syntax::change_positions(&lhs, &change_map);
+ let rhs_positions = syntax::change_positions(&rhs, &change_map);
(Some(ts_lang.name.into()), lhs_positions, rhs_positions)
}
}
diff --git a/src/sliders.rs b/src/sliders.rs
index 6683695753..e9128b7954 100644
--- a/src/sliders.rs
+++ b/src/sliders.rs
@@ -30,7 +30,7 @@
//! (B in this example).
use crate::{
- changes::{ChangeKind::*, ChangeMap, insert_deep_unchanged, insert_deep_novel},
+ changes::{insert_deep_novel, insert_deep_unchanged, ChangeKind::*, ChangeMap},
guess_language,
positions::SingleLineSpan,
syntax::Syntax,
@@ -104,8 +104,8 @@ fn fix_all_nested_sliders<'a>(nodes: &[&'a Syntax<'a>], change_map: &mut ChangeM
fn fix_nested_slider<'a>(node: &'a Syntax<'a>, change_map: &mut ChangeMap<'a>) {
if let List { children, .. } = node {
- match node
- .change()
+ match change_map
+ .get(node)
.expect("Changes should be set before slider correction")
{
Unchanged(_) => {
@@ -117,7 +117,7 @@ fn fix_nested_slider<'a>(node: &'a Syntax<'a>, change_map: &mut ChangeMap<'a>) {
Novel => {
let mut found_unchanged = vec![];
for child in children {
- unchanged_descendants(child, &mut found_unchanged);
+ unchanged_descendants(child, &mut found_unchanged, change_map);
}
if let [List { .. }] = found_unchanged[..] {
@@ -128,19 +128,23 @@ fn fix_nested_slider<'a>(node: &'a Syntax<'a>, change_map: &mut ChangeMap<'a>) {
}
}
-fn unchanged_descendants<'a>(node: &'a Syntax<'a>, found: &mut Vec<&'a Syntax<'a>>) {
+fn unchanged_descendants<'a>(
+ node: &'a Syntax<'a>,
+ found: &mut Vec<&'a Syntax<'a>>,
+ change_map: &ChangeMap<'a>,
+) {
if found.len() > 1 {
return;
}
- match node.change().unwrap() {
+ match change_map.get(node).unwrap() {
Unchanged(_) => {
found.push(node);
}
Novel | ReplacedComment(_, _) => {
if let List { children, .. } = node {
for child in children {
- unchanged_descendants(child, found);
+ unchanged_descendants(child, found, change_map);
}
}
}
@@ -152,7 +156,7 @@ fn push_unchanged_to_ancestor<'a>(
inner: &'a Syntax<'a>,
change_map: &mut ChangeMap<'a>,
) {
- let inner_change = inner.change().expect("Node changes should be set");
+ let inner_change = *change_map.get(inner).expect("Node changes should be set");
let delimiters_match = match (root, inner) {
(
@@ -177,20 +181,23 @@ fn push_unchanged_to_ancestor<'a>(
}
fn fix_sliders<'a>(nodes: &[&'a Syntax<'a>], change_map: &mut ChangeMap<'a>) {
- for (region_start, region_end) in novel_regions_after_unchanged(nodes) {
+ for (region_start, region_end) in novel_regions_after_unchanged(nodes, change_map) {
slide_to_prev_node(nodes, change_map, region_start, region_end);
}
- for (region_start, region_end) in novel_regions_before_unchanged(nodes) {
+ for (region_start, region_end) in novel_regions_before_unchanged(nodes, change_map) {
slide_to_next_node(nodes, change_map, region_start, region_end);
}
}
-fn novel_regions_after_unchanged<'a>(nodes: &[&'a Syntax<'a>]) -> Vec<(usize, usize)> {
+fn novel_regions_after_unchanged<'a>(
+ nodes: &[&'a Syntax<'a>],
+ change_map: &ChangeMap<'a>,
+) -> Vec<(usize, usize)> {
let mut regions: Vec<Vec<usize>> = vec![];
let mut region: Option<Vec<usize>> = None;
for (i, node) in nodes.iter().enumerate() {
- let change = node.change().expect("Node changes should be set");
+ let change = change_map.get(node).expect("Node changes should be set");
match change {
Unchanged(_) => {
@@ -230,12 +237,15 @@ fn novel_regions_after_unchanged<'a>(nodes: &[&'a Syntax<'a>]) -> Vec<(usize, us
.collect()
}
-fn novel_regions_before_unchanged<'a>(nodes: &[&'a Syntax<'a>]) -> Vec<(usize, usize)> {
+fn novel_regions_before_unchanged<'a>(
+ nodes: &[&'a Syntax<'a>],
+ change_map: &ChangeMap<'a>,
+) -> Vec<(usize, usize)> {
let mut regions: Vec<Vec<usize>> = vec![];
let mut region: Option<Vec<usize>> = None;
for (i, node) in nodes.iter().enumerate() {
- let change = node.change().expect("Node changes should be set");
+ let change = change_map.get(node).expect("Node changes should be set");
match change {
Unchanged(_) => {
@@ -336,11 +346,11 @@ fn slide_to_prev_node<'a>(
}
}
- let opposite = match before_start_node
- .change()
+ let opposite = match change_map
+ .get(before_start_node)
.expect("Node changes should be set")
{
- Unchanged(n) => n,
+ Unchanged(n) => *n,
_ => unreachable!(),
};
@@ -386,11 +396,11 @@ fn slide_to_next_node<'a>(
}
}
- let opposite = match after_last_node
- .change()
+ let opposite = match change_map
+ .get(after_last_node)
.expect("Node changes should be set")
{
- Unchanged(n) => n,
+ Unchanged(n) => *n,
_ => unreachable!(),
};
@@ -520,10 +530,10 @@ mod tests {
change_map.insert(lhs[2], Novel);
fix_all_sliders(guess_language::Language::EmacsLisp, &lhs, &mut change_map);
- assert_eq!(lhs[0].change(), Some(Novel));
- assert_eq!(lhs[1].change(), Some(Novel));
- assert_eq!(lhs[2].change(), Some(Unchanged(rhs[0])));
- assert_eq!(rhs[0].change(), Some(Unchanged(lhs[2])));
+ assert_eq!(change_map.get(lhs[0]), Some(&Novel));
+ assert_eq!(change_map.get(lhs[1]), Some(&Novel));
+ assert_eq!(change_map.get(lhs[2]), Some(&Unchanged(rhs[0])));
+ assert_eq!(change_map.get(rhs[0]), Some(&Unchanged(lhs[2])));
}
/// Test that we slide at the end if the unchanged node is
@@ -569,10 +579,10 @@ mod tests {
change_map.insert(lhs[2], Unchanged(rhs[0]));
fix_all_sliders(guess_language::Language::EmacsLisp, &lhs, &mut change_map);
- assert_eq!(rhs[0].change(), Some(Unchanged(lhs[0])));
- assert_eq!(lhs[0].change(), Some(Unchanged(rhs[0])));
- assert_eq!(lhs[1].change(), Some(Novel));
- assert_eq!(lhs[2].change(), Some(Novel));
+ assert_eq!(change_map.get(rhs[0]), Some(&Unchanged(lhs[0])));
+ assert_eq!(change_map.get(lhs[0]), Some(&Unchanged(rhs[0])));
+ assert_eq!(change_map.get(lhs[1]), Some(&Novel));
+ assert_eq!(change_map.get(lhs[2]), Some(&Novel));
}
#[test]
fn test_slider_two_steps() {
@@ -591,9 +601,9 @@ mod tests {
change_map.insert(rhs[4], Novel);
fix_all_sliders(guess_language::Language::EmacsLisp, &rhs, &mut change_map);
- assert_eq!(rhs[0].change(), Some(Novel));
- assert_eq!(rhs[1].change(), Some(Novel));
- assert_eq!(rhs[2].change(), Some(Novel));
- assert_eq!(rhs[3].change(), Some(Unchanged(rhs[0])));
+ assert_eq!(change_map.get(rhs[0]), Some(&Novel));
+ assert_eq!(change_map.get(rhs[1]), Some(&Novel));
+ assert_eq!(change_map.get(rhs[2]), Some(&Novel));
+ assert_eq!(change_map.get(rhs[3]), Some(&Unchanged(rhs[0])));
}
}
diff --git a/src/syntax.rs b/src/syntax.rs
index 4d20bd1fad..96a120af20 100644
--- a/src/syntax.rs
+++ b/src/syntax.rs
@@ -13,7 +13,7 @@ use typed_arena::Arena;
use crate::{
changes::ChangeKind,
- changes::ChangeKind::*,
+ changes::{ChangeKind::*, ChangeMap},
lines::{LineNumber, NewlinePositions},
myers_diff,
positions::SingleLineSpan,
@@ -320,10 +320,6 @@ impl<'a> Syntax<'a> {
};
position.last().map(|lp| lp.line)
}
-
- pub fn change(&'a self) -> Option<ChangeKind<'a>> {
- todo!()
- }
}
/// Initialise all the fields in `SyntaxInfo`.
@@ -768,16 +764,23 @@ impl MatchedPos {
}
/// Walk `nodes` and return a vec of all the changed positions.
-pub fn change_positions<'a>(nodes: &[&'a Syntax<'a>]) -> Vec<MatchedPos> {
+pub fn change_positions<'a>(
+ nodes: &[&'a Syntax<'a>],
+ change_map: &ChangeMap<'a>,
+) -> Vec<MatchedPos> {
let mut positions = Vec::new();
- change_positions_(nodes, &mut positions);
+ change_positions_(nodes, change_map, &mut positions);
positions
}
-fn change_positions_<'a>(nodes: &[&'a Syntax<'a>], positions: &mut Vec<MatchedPos>) {
+fn change_positions_<'a>(
+ nodes: &[&'a Syntax<'a>],
+ change_map: &ChangeMap<'a>,
+ positions: &mut Vec<MatchedPos>,
+) {
for node in nodes {
- let change = node
- .change()
+ let change = change_map
+ .get(node)
.unwrap_or_else(|| panic!("Should have changes set in all nodes: {:#?}", node));
match node {
@@ -788,16 +791,16 @@ fn change_positions_<'a>(nodes: &[&'a Syntax<'a>], positions: &mut Vec<MatchedPo
..
} => {
positions.extend(MatchedPos::new(
- change,
+ *change,
TokenKind::Delimiter,
open_position,
false,
));
- change_positions_(children, positions);
+ change_positions_(children, change_map, positions);
positions.extend(MatchedPos::new(
- change,
+ *change,
TokenKind::Delimiter,
close_position,
true,
@@ -805,7 +808,7 @@ fn change_positions_<'a>(nodes: &[&'a Syntax<'a>], positions: &mut Vec<MatchedPo
}
Atom { position, kind, .. } => {
positions.extend(MatchedPos::new(
- change,
+ *change,
TokenKind::Atom(*kind),
position,
false,
diff --git a/src/unchanged.rs b/src/unchanged.rs
index 45805ad401..085a191c08 100644
--- a/src/unchanged.rs
+++ b/src/unchanged.rs
@@ -450,12 +450,12 @@ mod tests {
shrink_unchanged_at_ends(&lhs_nodes, &rhs_nodes, &mut change_map);
assert_eq!(
- lhs_nodes[0].change(),
- Some(ChangeKind::Unchanged(rhs_nodes[0]))
+ change_map.get(lhs_nodes[0]),
+ Some(&ChangeKind::Unchanged(rhs_nodes[0]))
);
assert_eq!(
- rhs_nodes[0].change(),
- Some(ChangeKind::Unchanged(lhs_nodes[0]))
+ change_map.get(rhs_nodes[0]),
+ Some(&ChangeKind::Unchanged(lhs_nodes[0]))
);
assert_eq!(lhs_after_skip.len(), 2);
@@ -476,12 +476,12 @@ mod tests {
shrink_unchanged_at_ends(&lhs_nodes, &rhs_nodes, &mut change_map);
assert_eq!(
- lhs_nodes[2].change(),
- Some(ChangeKind::Unchanged(rhs_nodes[1]))
+ change_map.get(lhs_nodes[2]),
+ Some(&ChangeKind::Unchanged(rhs_nodes[1]))
);
assert_eq!(
- rhs_nodes[1].change(),
- Some(ChangeKind::Unchanged(lhs_nodes[2]))
+ change_map.get(rhs_nodes[1]),
+ Some(&ChangeKind::Unchanged(lhs_nodes[2]))
);
assert_eq!(lhs_after_skip.len(), 2);
@@ -509,8 +509,8 @@ mod tests {
assert!(matches!(rhs_after_skip[0], Syntax::List { .. }));
// The inner items haven't had their change set yet.
- assert_eq!(lhs_after_skip[0].change(), None);
- assert_eq!(rhs_after_skip[0].change(), None);
+ assert_eq!(change_map.get(lhs_after_skip[0]), None);
+ assert_eq!(change_map.get(rhs_after_skip[0]), None);
}
#[test]
@@ -530,12 +530,12 @@ mod tests {
let (lhs_after_skip, rhs_after_skip) = &res[0];
assert_eq!(
- lhs_nodes[0].change(),
- Some(ChangeKind::Unchanged(rhs_nodes[0]))
+ change_map.get(lhs_nodes[0]),
+ Some(&ChangeKind::Unchanged(rhs_nodes[0]))
);
assert_eq!(
- rhs_nodes[0].change(),
- Some(ChangeKind::Unchanged(lhs_nodes[0]))
+ change_map.get(rhs_nodes[0]),
+ Some(&ChangeKind::Unchanged(lhs_nodes[0]))
);
assert_eq!(lhs_after_skip.len(), 2);
@@ -558,12 +558,12 @@ mod tests {
let (lhs_after_skip, rhs_after_skip) = &res[0];
assert_eq!(
- lhs_nodes[2].change(),
- Some(ChangeKind::Unchanged(rhs_nodes[1]))
+ change_map.get(lhs_nodes[2]),
+ Some(&ChangeKind::Unchanged(rhs_nodes[1]))
);
assert_eq!(
- rhs_nodes[1].change(),
- Some(ChangeKind::Unchanged(lhs_nodes[2]))
+ change_map.get(rhs_nodes[1]),
+ Some(&ChangeKind::Unchanged(lhs_nodes[2]))
);
assert_eq!(lhs_after_skip.len(), 2);
@@ -593,8 +593,8 @@ mod tests {
assert!(matches!(rhs_after_skip[0], Syntax::List { .. }));
// The outer list delimiters don't have their change set yet.
- assert_eq!(lhs_nodes[0].change(), None);
- assert_eq!(rhs_nodes[0].change(), None);
+ assert_eq!(change_map.get(lhs_nodes[0]), None);
+ assert_eq!(change_map.get(rhs_nodes[0]), None);
}
#[test]
@@ -625,12 +625,12 @@ mod tests {
);
assert_eq!(
- lhs_nodes[1].change(),
- Some(ChangeKind::Unchanged(rhs_nodes[1]))
+ change_map.get(lhs_nodes[1]),
+ Some(&ChangeKind::Unchanged(rhs_nodes[1]))
);
assert_eq!(
- rhs_nodes[1].change(),
- Some(ChangeKind::Unchanged(lhs_nodes[1]))
+ change_map.get(rhs_nodes[1]),
+ Some(&ChangeKind::Unchanged(lhs_nodes[1]))
);
}
@@ -684,8 +684,8 @@ mod tests {
assert_eq!(res.len(), 2);
assert_eq!(
- lhs_nodes[0].change(),
- Some(ChangeKind::Unchanged(rhs_nodes[0]))
+ change_map.get(lhs_nodes[0]),
+ Some(&ChangeKind::Unchanged(rhs_nodes[0]))
);
}