@@ -35,7 +35,7 @@ use crate::snippet;
35
35
use std:: cmp:: { max, min, Reverse } ;
36
36
use std:: collections:: HashMap ;
37
37
use std:: fmt:: Display ;
38
- use std:: ops:: Range ;
38
+ use std:: ops:: { Bound , Range } ;
39
39
use std:: { cmp, fmt} ;
40
40
41
41
use crate :: renderer:: styled_buffer:: StyledBuffer ;
@@ -1085,7 +1085,7 @@ fn format_snippet(
1085
1085
term_width : usize ,
1086
1086
anonymized_line_numbers : bool ,
1087
1087
) -> 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 ( ) ) ;
1089
1089
let origin = snippet. origin ;
1090
1090
let need_empty_header = origin. is_some ( ) || is_first;
1091
1091
let mut body = format_body (
@@ -1175,7 +1175,7 @@ fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_>
1175
1175
let ann_start = snippet
1176
1176
. annotations
1177
1177
. iter ( )
1178
- . map ( |ann| ann. range . start )
1178
+ . map ( |ann| ann. inclusive_start ( ) )
1179
1179
. min ( )
1180
1180
. unwrap_or ( 0 ) ;
1181
1181
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<'_>
1187
1187
snippet. source = & snippet. source [ new_start..] ;
1188
1188
1189
1189
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) ;
1193
1201
}
1194
1202
}
1195
1203
1196
1204
let ann_end = snippet
1197
1205
. annotations
1198
1206
. iter ( )
1199
- . map ( |ann| ann. range . end )
1207
+ . map ( |ann| ann. exclusive_end ( snippet . source . len ( ) ) )
1200
1208
. max ( )
1201
1209
. unwrap_or ( snippet. source . len ( ) ) ;
1202
1210
if let Some ( end_offset) = snippet. source [ ann_end..] . find ( '\n' ) {
@@ -1286,7 +1294,7 @@ fn format_body(
1286
1294
let source_len = snippet. source . len ( ) ;
1287
1295
if let Some ( bigger) = snippet. annotations . iter ( ) . find_map ( |x| {
1288
1296
// 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 ) {
1290
1298
Some ( & x. range )
1291
1299
} else {
1292
1300
None
@@ -1313,7 +1321,7 @@ fn format_body(
1313
1321
let mut annotations = snippet. annotations ;
1314
1322
let ranges = annotations
1315
1323
. iter ( )
1316
- . map ( |a| a . range . clone ( ) )
1324
+ . map ( |a| ( a . inclusive_start ( ) , a . exclusive_end ( source_len ) ) )
1317
1325
. collect :: < Vec < _ > > ( ) ;
1318
1326
// We want to merge multiline annotations that have the same range into one
1319
1327
// multiline annotation to save space. This is done by making any duplicate
@@ -1354,16 +1362,16 @@ fn format_body(
1354
1362
// Skip if the annotation's index matches the range index
1355
1363
if ann_idx != r_idx
1356
1364
// We only want to merge multiline annotations
1357
- && snippet. source [ ann. range . clone ( ) ] . lines ( ) . count ( ) > 1
1365
+ && snippet. source [ ann. range ] . lines ( ) . count ( ) > 1
1358
1366
// 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
1361
1369
{
1362
- ann. range . start = ann. range . end . saturating_sub ( 1 ) ;
1370
+ ann. range . 0 = Bound :: Included ( ann. exclusive_end ( source_len ) . saturating_sub ( 1 ) ) ;
1363
1371
}
1364
1372
} ) ;
1365
1373
} ) ;
1366
- annotations. sort_by_key ( |a| a. range . start ) ;
1374
+ annotations. sort_by_key ( |a| a. inclusive_start ( ) ) ;
1367
1375
let mut annotations = annotations. into_iter ( ) . enumerate ( ) . collect :: < Vec < _ > > ( ) ;
1368
1376
1369
1377
for ( idx, ( line, end_line) ) in CursorLines :: new ( snippet. source ) . enumerate ( ) {
@@ -1411,7 +1419,7 @@ fn format_body(
1411
1419
_ => DisplayAnnotationType :: from ( annotation. level ) ,
1412
1420
} ;
1413
1421
let label_right = annotation. label . map_or ( 0 , |label| label. len ( ) + 1 ) ;
1414
- match annotation. range {
1422
+ match annotation. make_range ( source_len ) {
1415
1423
// This handles if the annotation is on the next line. We add
1416
1424
// the `end_line_size` to account for annotating the line end.
1417
1425
Range { start, .. } if start > line_end_index + end_line_size => true ,
0 commit comments