From 1d1eedf7a5fd89d380b7e1e2508cdc51dc094fbe Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Mon, 3 Jan 2022 23:23:49 -0800 Subject: [PATCH] Don't store a position tuple in MatchedPos --- src/context.rs | 6 +- src/hunks.rs | 234 +++++++++++++++++++------------------------------ src/syntax.rs | 41 +++++---- 3 files changed, 114 insertions(+), 167 deletions(-) diff --git a/src/context.rs b/src/context.rs index fb4956c34..fb49b1879 100644 --- a/src/context.rs +++ b/src/context.rs @@ -24,11 +24,7 @@ pub fn opposite_positions(mps: &[MatchedPos]) -> HashMap { - for (self_span, opposite_span) in zip_repeat_shorter(&self_pos.0, &opposite_pos.0) { - let opposite_lines = res.entry(self_span.line).or_insert_with(HashSet::new); - opposite_lines.insert(opposite_span.line); - } - for (self_span, opposite_span) in zip_repeat_shorter(&self_pos.1, &opposite_pos.1) { + for (self_span, opposite_span) in zip_repeat_shorter(&self_pos, &opposite_pos) { let opposite_lines = res.entry(self_span.line).or_insert_with(HashSet::new); opposite_lines.insert(opposite_span.line); } diff --git a/src/hunks.rs b/src/hunks.rs index 78345a0c7..a28c715c6 100644 --- a/src/hunks.rs +++ b/src/hunks.rs @@ -754,22 +754,16 @@ mod tests { let matched_mp = MatchedPos { kind: MatchKind::Unchanged { highlight: TokenKind::Delimiter, - self_pos: ( - vec![SingleLineSpan { - line: 1.into(), - start_col: 1, - end_col: 2, - }], - vec![], - ), - opposite_pos: ( - vec![SingleLineSpan { - line: 2.into(), - start_col: 1, - end_col: 2, - }], - vec![], - ), + self_pos: vec![SingleLineSpan { + line: 1.into(), + start_col: 1, + end_col: 2, + }], + opposite_pos: vec![SingleLineSpan { + line: 2.into(), + start_col: 1, + end_col: 2, + }], }, pos: SingleLineSpan { line: 1.into(), @@ -806,22 +800,16 @@ mod tests { MatchedPos { kind: MatchKind::Unchanged { highlight: TokenKind::Atom(AtomKind::Normal), - self_pos: ( - vec![SingleLineSpan { - line: 0.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), - opposite_pos: ( - vec![SingleLineSpan { - line: 0.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), + self_pos: vec![SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 1, + }], + opposite_pos: vec![SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 1, + }], }, pos: SingleLineSpan { line: 0.into(), @@ -832,22 +820,16 @@ mod tests { MatchedPos { kind: MatchKind::Unchanged { highlight: TokenKind::Atom(AtomKind::Normal), - self_pos: ( - vec![SingleLineSpan { - line: 1.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), - opposite_pos: ( - vec![SingleLineSpan { - line: 2.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), + self_pos: vec![SingleLineSpan { + line: 1.into(), + start_col: 0, + end_col: 1, + }], + opposite_pos: vec![SingleLineSpan { + line: 2.into(), + start_col: 0, + end_col: 1, + }], }, pos: SingleLineSpan { line: 1.into(), @@ -861,22 +843,16 @@ mod tests { MatchedPos { kind: MatchKind::Unchanged { highlight: TokenKind::Atom(AtomKind::Normal), - self_pos: ( - vec![SingleLineSpan { - line: 0.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), - opposite_pos: ( - vec![SingleLineSpan { - line: 0.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), + self_pos: vec![SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 1, + }], + opposite_pos: vec![SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 1, + }], }, pos: SingleLineSpan { line: 0.into(), @@ -897,22 +873,16 @@ mod tests { MatchedPos { kind: MatchKind::Unchanged { highlight: TokenKind::Atom(AtomKind::Normal), - self_pos: ( - vec![SingleLineSpan { - line: 2.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), - opposite_pos: ( - vec![SingleLineSpan { - line: 1.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), + self_pos: vec![SingleLineSpan { + line: 2.into(), + start_col: 0, + end_col: 1, + }], + opposite_pos: vec![SingleLineSpan { + line: 1.into(), + start_col: 0, + end_col: 1, + }], }, pos: SingleLineSpan { line: 2.into(), @@ -956,22 +926,16 @@ mod tests { MatchedPos { kind: MatchKind::Unchanged { highlight: TokenKind::Atom(AtomKind::Normal), - self_pos: ( - vec![SingleLineSpan { - line: 0.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), - opposite_pos: ( - vec![SingleLineSpan { - line: 0.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), + self_pos: vec![SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 1, + }], + opposite_pos: vec![SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 1, + }], }, pos: SingleLineSpan { line: 0.into(), @@ -982,22 +946,16 @@ mod tests { MatchedPos { kind: MatchKind::Unchanged { highlight: TokenKind::Atom(AtomKind::Normal), - self_pos: ( - vec![SingleLineSpan { - line: 1.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), - opposite_pos: ( - vec![SingleLineSpan { - line: 2.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), + self_pos: vec![SingleLineSpan { + line: 1.into(), + start_col: 0, + end_col: 1, + }], + opposite_pos: vec![SingleLineSpan { + line: 2.into(), + start_col: 0, + end_col: 1, + }], }, pos: SingleLineSpan { line: 1.into(), @@ -1011,22 +969,16 @@ mod tests { MatchedPos { kind: MatchKind::Unchanged { highlight: TokenKind::Atom(AtomKind::Normal), - self_pos: ( - vec![SingleLineSpan { - line: 0.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), - opposite_pos: ( - vec![SingleLineSpan { - line: 0.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), + self_pos: vec![SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 1, + }], + opposite_pos: vec![SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 1, + }], }, pos: SingleLineSpan { line: 0.into(), @@ -1047,22 +999,16 @@ mod tests { MatchedPos { kind: MatchKind::Unchanged { highlight: TokenKind::Atom(AtomKind::Normal), - self_pos: ( - vec![SingleLineSpan { - line: 2.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), - opposite_pos: ( - vec![SingleLineSpan { - line: 1.into(), - start_col: 0, - end_col: 1, - }], - vec![], - ), + self_pos: vec![SingleLineSpan { + line: 2.into(), + start_col: 0, + end_col: 1, + }], + opposite_pos: vec![SingleLineSpan { + line: 1.into(), + start_col: 0, + end_col: 1, + }], }, pos: SingleLineSpan { line: 2.into(), diff --git a/src/syntax.rs b/src/syntax.rs index 0defa3154..2c45f0459 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -489,11 +489,8 @@ pub enum TokenKind { pub enum MatchKind { Unchanged { highlight: TokenKind, - self_pos: (Vec, Vec), - // If it's a matched atom, only use the first vec. If it'a - // matched delimiter pair, use both vecs. - // TODO: better data type. - opposite_pos: (Vec, Vec), + self_pos: Vec, + opposite_pos: Vec, }, Novel { highlight: TokenKind, @@ -508,7 +505,7 @@ pub enum MatchKind { impl MatchKind { pub fn first_opposite_span(&self) -> Option { match self { - MatchKind::Unchanged { opposite_pos, .. } => opposite_pos.0.first().copied(), + MatchKind::Unchanged { opposite_pos, .. } => opposite_pos.first().copied(), MatchKind::UnchangedCommentPart { opposite_pos, .. } => opposite_pos.first().copied(), MatchKind::Novel { .. } => None, MatchKind::ChangedCommentPart {} => None, @@ -604,7 +601,8 @@ impl MatchedPos { fn new( ck: ChangeKind, highlight: TokenKind, - pos: (&[SingleLineSpan], &[SingleLineSpan]), + pos: &[SingleLineSpan], + is_close: bool, ) -> Vec { let kind = match ck { ReplacedComment(this, opposite) => { @@ -621,8 +619,8 @@ impl MatchedPos { return split_comment_words( this_content, - // TODO: handle the whole pos.0 here. - pos.0[0], + // TODO: handle the whole pos here. + pos[0], opposite_content, opposite_pos[0], ); @@ -633,13 +631,19 @@ impl MatchedPos { open_position, close_position, .. - } => (open_position.clone(), close_position.clone()), - Atom { position, .. } => (position.clone(), position.clone()), + } => { + if is_close { + close_position.clone() + } else { + open_position.clone() + } + } + Atom { position, .. } => position.clone(), }; MatchKind::Unchanged { highlight, - self_pos: (pos.0.to_vec(), pos.1.to_vec()), + self_pos: pos.to_vec(), opposite_pos, } } @@ -649,7 +653,7 @@ impl MatchedPos { // Create a MatchedPos for every line that `pos` covers. // TODO: what about opposite pos? let mut res = vec![]; - for line_pos in pos.0 { + for line_pos in pos { // Don't create a MatchedPos for empty positions. This // occurs when we have lists with empty open/close // delimiter positions, such as the top-level list of syntax items. @@ -704,7 +708,8 @@ fn change_positions_<'a>( positions.extend(MatchedPos::new( change, TokenKind::Delimiter, - (open_position, close_position), + open_position, + false, )); change_positions_(nl_pos, opposite_nl_pos, children, positions); @@ -712,9 +717,8 @@ fn change_positions_<'a>( positions.extend(MatchedPos::new( change, TokenKind::Delimiter, - // TODO: use open position here too (currently - // breaks display). - (close_position, close_position), + close_position, + true, )); } Atom { @@ -730,7 +734,8 @@ fn change_positions_<'a>( positions.extend(MatchedPos::new( change, TokenKind::Atom(*kind), - (position, &[]), + position, + false, )); } }