From b78ba2da4bbd1d2145cdffe935ec32e922f266ea Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Sat, 26 Aug 2023 20:36:07 -0700 Subject: [PATCH] Use type names from line_numbers directly --- src/line_parser.rs | 16 ++++++++-------- src/lines.rs | 23 ----------------------- src/parse/syntax.rs | 6 +++--- src/parse/tree_sitter_parser.rs | 16 +++++++--------- 4 files changed, 18 insertions(+), 43 deletions(-) diff --git a/src/line_parser.rs b/src/line_parser.rs index 31f803fd9..c3503a216 100644 --- a/src/line_parser.rs +++ b/src/line_parser.rs @@ -1,7 +1,7 @@ //! A fallback "parser" for plain text. use lazy_static::lazy_static; -use line_numbers::LinePositions as NewlinePositions; +use line_numbers::LinePositions; use regex::Regex; use crate::{ @@ -108,8 +108,8 @@ fn line_len_in_bytes(line: &str) -> usize { pub fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec { // TODO: If either side is "", don't split each line by words // pointlessly. This is common for file additions/removals. - let lhs_nlp = NewlinePositions::from(lhs_src); - let rhs_nlp = NewlinePositions::from(rhs_src); + let lhs_lp = LinePositions::from(lhs_src); + let rhs_lp = LinePositions::from(rhs_src); let mut lhs_offset = 0; let mut rhs_offset = 0; @@ -120,9 +120,9 @@ pub fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec { TextChangeKind::Unchanged => { for (lhs_line, rhs_line) in lhs_lines.iter().zip(rhs_lines) { let lhs_pos = - lhs_nlp.from_offsets(lhs_offset, lhs_offset + line_len_in_bytes(lhs_line)); + lhs_lp.from_offsets(lhs_offset, lhs_offset + line_len_in_bytes(lhs_line)); let rhs_pos = - rhs_nlp.from_offsets(rhs_offset, rhs_offset + line_len_in_bytes(rhs_line)); + rhs_lp.from_offsets(rhs_offset, rhs_offset + line_len_in_bytes(rhs_line)); res.push(MatchedPos { kind: MatchKind::UnchangedToken { @@ -149,7 +149,7 @@ pub fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec { myers_diff::DiffResult::Left(lhs_word) => { if *lhs_word != "\n" { let lhs_pos = - lhs_nlp.from_offsets(lhs_offset, lhs_offset + lhs_word.len()); + lhs_lp.from_offsets(lhs_offset, lhs_offset + lhs_word.len()); res.push(MatchedPos { kind: MatchKind::NovelWord { highlight: TokenKind::Atom(AtomKind::Normal), @@ -163,9 +163,9 @@ pub fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec { myers_diff::DiffResult::Both(lhs_word, rhs_word) => { if *lhs_word != "\n" { let lhs_pos = - lhs_nlp.from_offsets(lhs_offset, lhs_offset + lhs_word.len()); + lhs_lp.from_offsets(lhs_offset, lhs_offset + lhs_word.len()); let rhs_pos = - rhs_nlp.from_offsets(rhs_offset, rhs_offset + rhs_word.len()); + rhs_lp.from_offsets(rhs_offset, rhs_offset + rhs_word.len()); res.push(MatchedPos { kind: MatchKind::NovelLinePart { diff --git a/src/lines.rs b/src/lines.rs index 858d42623..4500cb299 100644 --- a/src/lines.rs +++ b/src/lines.rs @@ -52,8 +52,6 @@ pub fn is_all_whitespace(s: &str) -> bool { #[cfg(test)] mod tests { use super::*; - use line_numbers::LinePositions as NewlinePositions; - use line_numbers::SingleLineSpan; use pretty_assertions::assert_eq; #[test] @@ -80,27 +78,6 @@ mod tests { assert_eq!(line.max_line().0, 1); } - #[test] - fn from_offsets_relative_to() { - let newline_positions: NewlinePositions = "foo\nbar".into(); - - let pos = SingleLineSpan { - line: 1.into(), - start_col: 1, - end_col: 1, - }; - - let line_spans = newline_positions.from_offsets_relative_to(pos, 1, 2); - assert_eq!( - line_spans, - vec![SingleLineSpan { - line: 1.into(), - start_col: 2, - end_col: 3 - }] - ); - } - #[test] fn codepoint_len_non_ascii() { assert_eq!(codepoint_len("ƒoo"), 3); diff --git a/src/parse/syntax.rs b/src/parse/syntax.rs index 473c8c556..3e89af572 100644 --- a/src/parse/syntax.rs +++ b/src/parse/syntax.rs @@ -2,7 +2,7 @@ #![allow(clippy::mutable_key_type)] // Hash for Syntax doesn't use mutable fields. -use line_numbers::LinePositions as NewlinePositions; +use line_numbers::LinePositions; use line_numbers::SingleLineSpan; use std::{cell::Cell, env, fmt, hash::Hash, num::NonZeroU32}; use typed_arena::Arena; @@ -729,8 +729,8 @@ fn split_atom_words( .collect(); } - let content_newlines = NewlinePositions::from(content); - let opposite_content_newlines = NewlinePositions::from(opposite_content); + let content_newlines = LinePositions::from(content); + let opposite_content_newlines = LinePositions::from(opposite_content); let mut offset = 0; let mut opposite_offset = 0; diff --git a/src/parse/tree_sitter_parser.rs b/src/parse/tree_sitter_parser.rs index 6e5820f8c..c4177b740 100644 --- a/src/parse/tree_sitter_parser.rs +++ b/src/parse/tree_sitter_parser.rs @@ -5,13 +5,11 @@ use std::collections::HashSet; use crate::hash::DftHashMap; use crate::options::DiffOptions; use crate::parse::guess_language as guess; +use line_numbers::LinePositions; use tree_sitter as ts; use typed_arena::Arena; -use line_numbers::LinePositions as NewlinePositions; -use crate::{ - parse::syntax::{AtomKind, Syntax}, -}; +use crate::parse::syntax::{AtomKind, Syntax}; use super::syntax; use super::syntax::MatchedPos; @@ -1309,7 +1307,7 @@ pub fn to_syntax<'a>( // highlighting and for more precise Syntax nodes where applicable. let subtrees = parse_subtrees(src, config, tree); - let nl_pos = NewlinePositions::from(src); + let nl_pos = LinePositions::from(src); let mut cursor = tree.walk(); let mut error_count: usize = 0; @@ -1409,7 +1407,7 @@ pub struct HighlightedNodeIds { fn all_syntaxes_from_cursor<'a>( arena: &'a Arena>, src: &str, - nl_pos: &NewlinePositions, + nl_pos: &LinePositions, cursor: &mut ts::TreeCursor, error_count: &mut usize, config: &TreeSitterConfig, @@ -1445,7 +1443,7 @@ fn all_syntaxes_from_cursor<'a>( fn syntax_from_cursor<'a>( arena: &'a Arena>, src: &str, - nl_pos: &NewlinePositions, + nl_pos: &LinePositions, cursor: &mut ts::TreeCursor, error_count: &mut usize, config: &TreeSitterConfig, @@ -1502,7 +1500,7 @@ fn syntax_from_cursor<'a>( fn list_from_cursor<'a>( arena: &'a Arena>, src: &str, - nl_pos: &NewlinePositions, + nl_pos: &LinePositions, cursor: &mut ts::TreeCursor, error_count: &mut usize, config: &TreeSitterConfig, @@ -1639,7 +1637,7 @@ fn list_from_cursor<'a>( fn atom_from_cursor<'a>( arena: &'a Arena>, src: &str, - nl_pos: &NewlinePositions, + nl_pos: &LinePositions, cursor: &mut ts::TreeCursor, highlights: &HighlightedNodeIds, ignore_comments: bool,