Use type names from line_numbers directly

pull/504/merge
Wilfred Hughes 2023-08-26 20:36:07 +07:00
parent 41c9165c79
commit b78ba2da4b
4 changed files with 18 additions and 43 deletions

@ -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<MatchedPos> {
// 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<MatchedPos> {
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<MatchedPos> {
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<MatchedPos> {
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 {

@ -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);

@ -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;

@ -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<Syntax<'a>>,
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<Syntax<'a>>,
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<Syntax<'a>>,
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<Syntax<'a>>,
src: &str,
nl_pos: &NewlinePositions,
nl_pos: &LinePositions,
cursor: &mut ts::TreeCursor,
highlights: &HighlightedNodeIds,
ignore_comments: bool,