diff --git a/crates/flowistry/src/range.rs b/crates/flowistry/src/range.rs index 4538c518c..dd6332e29 100644 --- a/crates/flowistry/src/range.rs +++ b/crates/flowistry/src/range.rs @@ -111,6 +111,23 @@ impl Range { }) } + pub fn from_byte_range( + byte_start: usize, + byte_end: usize, + src: &str, + filename: String, + ) -> Self { + let char_start = src[.. byte_start].graphemes(true).count(); + let char_end = char_start + src[byte_start .. byte_end].graphemes(true).count(); + Range { + byte_start, + byte_end, + char_start, + char_end, + filename, + } + } + pub fn from_span(span: Span, source_map: &SourceMap) -> Result { let file = source_map.lookup_source_file(span.lo()); let filename = match &file.name { @@ -126,19 +143,7 @@ impl Range { let byte_start = source_map.lookup_byte_offset(span.lo()).pos.0 as usize; let byte_end = source_map.lookup_byte_offset(span.hi()).pos.0 as usize; - let prefix = &src[0 .. byte_start]; - let contents = &src[byte_start .. byte_end]; - - let char_start = prefix.graphemes(true).count(); - let char_end = char_start + contents.graphemes(true).count(); - - Ok(Range { - char_start, - char_end, - byte_start, - byte_end, - filename, - }) + Ok(Self::from_byte_range(byte_start, byte_end, src, filename)) } pub fn source_file<'a>( diff --git a/crates/flowistry/src/test_utils.rs b/crates/flowistry/src/test_utils.rs index d1d7a2501..fce843808 100644 --- a/crates/flowistry/src/test_utils.rs +++ b/crates/flowistry/src/test_utils.rs @@ -278,16 +278,7 @@ fn parse_range_map( k, vs.into_iter() .map(|(byte_start, byte_end)| { - let char_start = src[.. byte_start].graphemes(true).count(); - let char_end = - char_start + src[byte_start .. byte_end].graphemes(true).count(); - Range { - byte_start, - byte_end, - char_start, - char_end, - filename: "dummy.rs".to_string(), - } + Range::from_byte_range(byte_start, byte_end, &clean, "dummy.rs".into()) }) .collect::>(), ) @@ -346,12 +337,11 @@ pub fn test_command_output( match expected { Some(expected_path) => { - let expected_file = fs::read(expected_path); + let expected_file = fs::read_to_string(expected_path); match expected_file { Ok(file) => { - let output = String::from_utf8(file).unwrap(); let (_output_clean, output_ranges) = - parse_range_map(&output, vec![("`[", "]`")]).unwrap(); + parse_range_map(&file, vec![("`[", "]`")]).unwrap(); let expected = match output_ranges.get("`[") { Some(ranges) => ranges.clone().into_iter().collect::>(),