@@ -35,7 +35,7 @@ use crate::snippet;
3535use std:: cmp:: { max, min, Reverse } ;
3636use std:: collections:: HashMap ;
3737use std:: fmt:: Display ;
38- use std:: ops:: Range ;
38+ use std:: ops:: { Bound , Range } ;
3939use std:: { cmp, fmt} ;
4040
4141use crate :: renderer:: styled_buffer:: StyledBuffer ;
@@ -1085,7 +1085,7 @@ fn format_snippet(
10851085 term_width : usize ,
10861086 anonymized_line_numbers : bool ,
10871087) -> DisplaySet < ' _ > {
1088- let main_range = snippet. annotations . first ( ) . map ( |x| x. range . start ) ;
1088+ let main_range = snippet. annotations . first ( ) . map ( |x| x. inclusive_start ( ) ) ;
10891089 let origin = snippet. origin ;
10901090 let need_empty_header = origin. is_some ( ) || is_first;
10911091 let mut body = format_body (
@@ -1175,7 +1175,7 @@ fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_>
11751175 let ann_start = snippet
11761176 . annotations
11771177 . iter ( )
1178- . map ( |ann| ann. range . start )
1178+ . map ( |ann| ann. inclusive_start ( ) )
11791179 . min ( )
11801180 . unwrap_or ( 0 ) ;
11811181 if let Some ( before_new_start) = snippet. source [ 0 ..ann_start] . rfind ( '\n' ) {
@@ -1187,16 +1187,24 @@ fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_>
11871187 snippet. source = & snippet. source [ new_start..] ;
11881188
11891189 for ann in & mut snippet. annotations {
1190- let range_start = ann. range . start - new_start;
1191- let range_end = ann. range . end - new_start;
1192- ann. range = range_start..range_end;
1190+ let range_start = match ann. range . 0 {
1191+ Bound :: Unbounded => Bound :: Unbounded ,
1192+ Bound :: Excluded ( e) => Bound :: Excluded ( e - new_start) ,
1193+ Bound :: Included ( e) => Bound :: Included ( e - new_start) ,
1194+ } ;
1195+ let range_end = match ann. range . 1 {
1196+ Bound :: Unbounded => Bound :: Unbounded ,
1197+ Bound :: Excluded ( e) => Bound :: Excluded ( e - new_start) ,
1198+ Bound :: Included ( e) => Bound :: Included ( e - new_start) ,
1199+ } ;
1200+ ann. range = ( range_start, range_end) ;
11931201 }
11941202 }
11951203
11961204 let ann_end = snippet
11971205 . annotations
11981206 . iter ( )
1199- . map ( |ann| ann. range . end )
1207+ . map ( |ann| ann. exclusive_end ( snippet . source . len ( ) ) )
12001208 . max ( )
12011209 . unwrap_or ( snippet. source . len ( ) ) ;
12021210 if let Some ( end_offset) = snippet. source [ ann_end..] . find ( '\n' ) {
@@ -1286,7 +1294,7 @@ fn format_body(
12861294 let source_len = snippet. source . len ( ) ;
12871295 if let Some ( bigger) = snippet. annotations . iter ( ) . find_map ( |x| {
12881296 // Allow highlighting one past the last character in the source.
1289- if source_len + 1 < x. range . end {
1297+ if source_len + 1 < x. exclusive_end ( source_len ) {
12901298 Some ( & x. range )
12911299 } else {
12921300 None
@@ -1313,7 +1321,7 @@ fn format_body(
13131321 let mut annotations = snippet. annotations ;
13141322 let ranges = annotations
13151323 . iter ( )
1316- . map ( |a| a . range . clone ( ) )
1324+ . map ( |a| ( a . inclusive_start ( ) , a . exclusive_end ( source_len ) ) )
13171325 . collect :: < Vec < _ > > ( ) ;
13181326 // We want to merge multiline annotations that have the same range into one
13191327 // multiline annotation to save space. This is done by making any duplicate
@@ -1354,16 +1362,16 @@ fn format_body(
13541362 // Skip if the annotation's index matches the range index
13551363 if ann_idx != r_idx
13561364 // We only want to merge multiline annotations
1357- && snippet. source [ ann. range . clone ( ) ] . lines ( ) . count ( ) > 1
1365+ && snippet. source [ ann. range ] . lines ( ) . count ( ) > 1
13581366 // We only want to merge annotations that have the same range
1359- && ann. range . start == range. start
1360- && ann. range . end == range. end
1367+ && ann. inclusive_start ( ) == range. 0
1368+ && ann. exclusive_end ( source_len ) == range. 1
13611369 {
1362- ann. range . start = ann. range . end . saturating_sub ( 1 ) ;
1370+ ann. range . 0 = Bound :: Included ( ann. exclusive_end ( source_len ) . saturating_sub ( 1 ) ) ;
13631371 }
13641372 } ) ;
13651373 } ) ;
1366- annotations. sort_by_key ( |a| a. range . start ) ;
1374+ annotations. sort_by_key ( |a| a. inclusive_start ( ) ) ;
13671375 let mut annotations = annotations. into_iter ( ) . enumerate ( ) . collect :: < Vec < _ > > ( ) ;
13681376
13691377 for ( idx, ( line, end_line) ) in CursorLines :: new ( snippet. source ) . enumerate ( ) {
@@ -1411,7 +1419,7 @@ fn format_body(
14111419 _ => DisplayAnnotationType :: from ( annotation. level ) ,
14121420 } ;
14131421 let label_right = annotation. label . map_or ( 0 , |label| label. len ( ) + 1 ) ;
1414- match annotation. range {
1422+ match annotation. make_range ( source_len ) {
14151423 // This handles if the annotation is on the next line. We add
14161424 // the `end_line_size` to account for annotating the line end.
14171425 Range { start, .. } if start > line_end_index + end_line_size => true ,
0 commit comments