3232//!
3333//! The above snippet has been built out of the following structure:
3434use crate :: snippet;
35- use itertools:: FoldWhile :: { Continue , Done } ;
36- use itertools:: Itertools ;
3735use std:: fmt:: { Display , Write } ;
3836use std:: ops:: Range ;
3937use std:: { cmp, fmt} ;
@@ -830,20 +828,7 @@ fn format_header<'a>(
830828 } = item
831829 {
832830 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 ( ) ;
847832 col = char_column + 1 ;
848833 line_offset = lineno. unwrap_or ( 1 ) ;
849834 break ;
@@ -984,18 +969,11 @@ fn format_body(
984969 let mut body = vec ! [ ] ;
985970 let mut current_line = snippet. line_start ;
986971 let mut current_index = 0 ;
987- let mut line_info = vec ! [ ] ;
988972
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 ( ) ;
999977 let line_range = ( current_index, current_index + line_length) ;
1000978 body. push ( DisplayLine :: Source {
1001979 lineno : Some ( current_line) ,
@@ -1005,24 +983,11 @@ fn format_body(
1005983 range : line_range,
1006984 } ,
1007985 } ) ;
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 ;
1012988 current_line += 1 ;
1013989 current_index += line_length + end_line as usize ;
1014- }
1015990
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- {
1026991 let margin_left = margin
1027992 . map ( |m| m. left ( line_end_index - line_start_index) )
1028993 . unwrap_or_default ( ) ;
@@ -1040,8 +1005,20 @@ fn format_body(
10401005 if start >= line_start_index && end <= line_end_index
10411006 || start == line_end_index && end - start <= 1 =>
10421007 {
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;
10451022 let range = ( annotation_start_col, annotation_end_col) ;
10461023 body. insert (
10471024 body_idx + 1 ,
@@ -1080,7 +1057,10 @@ fn format_body(
10801057 } ) ;
10811058 }
10821059 } 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 > ( ) ;
10841064 let range = ( annotation_start_col, annotation_start_col + 1 ) ;
10851065 body. insert (
10861066 body_idx + 1 ,
@@ -1132,7 +1112,11 @@ fn format_body(
11321112 } ) ;
11331113 }
11341114
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 ) ;
11361120 let range = ( end_mark - margin_left, ( end_mark + 1 ) - margin_left) ;
11371121 body. insert (
11381122 body_idx + 1 ,
0 commit comments