32
32
//!
33
33
//! The above snippet has been built out of the following structure:
34
34
use crate :: snippet;
35
- use itertools:: FoldWhile :: { Continue , Done } ;
36
- use itertools:: Itertools ;
37
35
use std:: fmt:: { Display , Write } ;
38
36
use std:: ops:: Range ;
39
37
use std:: { cmp, fmt} ;
@@ -830,20 +828,7 @@ fn format_header<'a>(
830
828
} = item
831
829
{
832
830
if main_range >= range. 0 && main_range <= range. 1 {
833
- let char_column = text
834
- . chars ( )
835
- . map ( |c| unicode_width:: UnicodeWidthChar :: width ( c) . unwrap_or ( 0 ) )
836
- . chain ( std:: iter:: once ( 1 ) ) // treat the end of line as single-width
837
- . enumerate ( )
838
- . fold_while ( ( 0 , 0 ) , |( count, acc) , ( i, width) | {
839
- if acc <= main_range - range. 0 {
840
- Continue ( ( i, acc + width) )
841
- } else {
842
- Done ( ( count, acc) )
843
- }
844
- } )
845
- . into_inner ( )
846
- . 0 ;
831
+ let char_column = text[ 0 ..( main_range - range. 0 ) ] . chars ( ) . count ( ) ;
847
832
col = char_column + 1 ;
848
833
line_offset = lineno. unwrap_or ( 1 ) ;
849
834
break ;
@@ -984,18 +969,11 @@ fn format_body(
984
969
let mut body = vec ! [ ] ;
985
970
let mut current_line = snippet. line_start ;
986
971
let mut current_index = 0 ;
987
- let mut line_info = vec ! [ ] ;
988
972
989
- struct LineInfo {
990
- line_start_index : usize ,
991
- line_end_index : usize ,
992
- }
993
-
994
- for ( line, end_line) in CursorLines :: new ( snippet. source ) {
995
- let line_length: usize = line
996
- . chars ( )
997
- . map ( |c| unicode_width:: UnicodeWidthChar :: width ( c) . unwrap_or ( 0 ) )
998
- . sum ( ) ;
973
+ let mut annotation_line_count = 0 ;
974
+ let mut annotations = snippet. annotations ;
975
+ for ( idx, ( line, end_line) ) in CursorLines :: new ( snippet. source ) . enumerate ( ) {
976
+ let line_length: usize = line. len ( ) ;
999
977
let line_range = ( current_index, current_index + line_length) ;
1000
978
body. push ( DisplayLine :: Source {
1001
979
lineno : Some ( current_line) ,
@@ -1005,24 +983,11 @@ fn format_body(
1005
983
range : line_range,
1006
984
} ,
1007
985
} ) ;
1008
- line_info. push ( LineInfo {
1009
- line_start_index : line_range. 0 ,
1010
- line_end_index : line_range. 1 ,
1011
- } ) ;
986
+ let line_start_index = line_range. 0 ;
987
+ let line_end_index = line_range. 1 ;
1012
988
current_line += 1 ;
1013
989
current_index += line_length + end_line as usize ;
1014
- }
1015
990
1016
- let mut annotation_line_count = 0 ;
1017
- let mut annotations = snippet. annotations ;
1018
- for (
1019
- idx,
1020
- LineInfo {
1021
- line_start_index,
1022
- line_end_index,
1023
- } ,
1024
- ) in line_info. into_iter ( ) . enumerate ( )
1025
- {
1026
991
let margin_left = margin
1027
992
. map ( |m| m. left ( line_end_index - line_start_index) )
1028
993
. unwrap_or_default ( ) ;
@@ -1040,8 +1005,20 @@ fn format_body(
1040
1005
if start >= line_start_index && end <= line_end_index
1041
1006
|| start == line_end_index && end - start <= 1 =>
1042
1007
{
1043
- let annotation_start_col = start - line_start_index - margin_left;
1044
- let annotation_end_col = end - line_start_index - margin_left;
1008
+ let annotation_start_col = line[ 0 ..( start - line_start_index) ]
1009
+ . chars ( )
1010
+ . map ( |c| unicode_width:: UnicodeWidthChar :: width ( c) . unwrap_or ( 0 ) )
1011
+ . sum :: < usize > ( )
1012
+ - margin_left;
1013
+ // This allows for annotations to be placed one past the
1014
+ // last character
1015
+ let safe_end = ( end - line_start_index) . saturating_sub ( line_length) ;
1016
+ let annotation_end_col = line[ 0 ..( end - line_start_index) - safe_end]
1017
+ . chars ( )
1018
+ . map ( |c| unicode_width:: UnicodeWidthChar :: width ( c) . unwrap_or ( 0 ) )
1019
+ . sum :: < usize > ( )
1020
+ + safe_end
1021
+ - margin_left;
1045
1022
let range = ( annotation_start_col, annotation_end_col) ;
1046
1023
body. insert (
1047
1024
body_idx + 1 ,
@@ -1080,7 +1057,10 @@ fn format_body(
1080
1057
} ) ;
1081
1058
}
1082
1059
} else {
1083
- let annotation_start_col = start - line_start_index;
1060
+ let annotation_start_col = line[ 0 ..( start - line_start_index) ]
1061
+ . chars ( )
1062
+ . map ( |c| unicode_width:: UnicodeWidthChar :: width ( c) . unwrap_or ( 0 ) )
1063
+ . sum :: < usize > ( ) ;
1084
1064
let range = ( annotation_start_col, annotation_start_col + 1 ) ;
1085
1065
body. insert (
1086
1066
body_idx + 1 ,
@@ -1132,7 +1112,11 @@ fn format_body(
1132
1112
} ) ;
1133
1113
}
1134
1114
1135
- let end_mark = ( end - line_start_index) . saturating_sub ( 1 ) ;
1115
+ let end_mark = line[ 0 ..( end - line_start_index) ]
1116
+ . chars ( )
1117
+ . map ( |c| unicode_width:: UnicodeWidthChar :: width ( c) . unwrap_or ( 0 ) )
1118
+ . sum :: < usize > ( )
1119
+ . saturating_sub ( 1 ) ;
1136
1120
let range = ( end_mark - margin_left, ( end_mark + 1 ) - margin_left) ;
1137
1121
body. insert (
1138
1122
body_idx + 1 ,
0 commit comments