@@ -247,8 +247,17 @@ impl<S: Span> Report<'_, S> {
247
247
} else {
248
248
labels[ 0 ] . char_span . start
249
249
} ;
250
- let ( line_no, col_no) = src
251
- . get_offset_line ( location)
250
+ let line_and_col = match self . config . index_type {
251
+ IndexType :: Char => src. get_offset_line ( location) ,
252
+ IndexType :: Byte => src. get_byte_line ( location) . map ( |( line_obj, idx, col) | {
253
+ let line_text = src. get_line_text ( line_obj) . unwrap ( ) ;
254
+
255
+ let col = line_text[ ..col. min ( line_text. len ( ) ) ] . chars ( ) . count ( ) ;
256
+
257
+ ( line_obj, idx, col)
258
+ } ) ,
259
+ } ;
260
+ let ( line_no, col_no) = line_and_col
252
261
. map ( |( _, idx, col) | ( format ! ( "{}" , idx + 1 ) , format ! ( "{}" , col + 1 ) ) )
253
262
. unwrap_or_else ( || ( '?' . to_string ( ) , '?' . to_string ( ) ) ) ;
254
263
let line_ref = format ! ( ":{}:{}" , line_no, col_no) ;
@@ -1020,6 +1029,30 @@ mod tests {
1020
1029
"### ) ;
1021
1030
}
1022
1031
1032
+ #[ test]
1033
+ fn byte_column ( ) {
1034
+ let source = "äpplë == örängë;" ;
1035
+ let msg = Report :: < Range < usize > > :: build ( ReportKind :: Error , ( ) , 11 )
1036
+ . with_config ( no_color_and_ascii ( ) . with_index_type ( IndexType :: Byte ) )
1037
+ . with_message ( "can't compare äpplës with örängës" )
1038
+ . with_label ( Label :: new ( 0 ..7 ) . with_message ( "This is an äpplë" ) )
1039
+ . with_label ( Label :: new ( 11 ..20 ) . with_message ( "This is an örängë" ) )
1040
+ . finish ( )
1041
+ . write_to_string ( Source :: from ( source) ) ;
1042
+ // TODO: it would be nice if these lines didn't cross
1043
+ assert_snapshot ! ( msg, @r###"
1044
+ Error: can't compare äpplës with örängës
1045
+ ,-[<unknown>:1:10]
1046
+ |
1047
+ 1 | äpplë == örängë;
1048
+ | ^^|^^ ^^^|^^
1049
+ | `-------------- This is an äpplë
1050
+ | |
1051
+ | `---- This is an örängë
1052
+ ---'
1053
+ "### ) ;
1054
+ }
1055
+
1023
1056
#[ test]
1024
1057
fn label_at_end_of_long_line ( ) {
1025
1058
let source = format ! ( "{}orange" , "apple == " . repeat( 100 ) ) ;
0 commit comments