@@ -644,6 +644,8 @@ impl EmitterWriter {
644644 code_offset : usize ,
645645 margin : Margin ,
646646 ) {
647+ // Tabs are assumed to have been replaced by spaces in calling code.
648+ assert ! ( !source_string. contains( '\t' ) ) ;
647649 let line_len = source_string. len ( ) ;
648650 // Create the source line we will highlight.
649651 let left = margin. left ( line_len) ;
@@ -707,7 +709,7 @@ impl EmitterWriter {
707709 }
708710
709711 let source_string = match file. get_line ( line. line_index - 1 ) {
710- Some ( s) => s ,
712+ Some ( s) => replace_tabs ( & * s ) ,
711713 None => return Vec :: new ( ) ,
712714 } ;
713715
@@ -1376,8 +1378,17 @@ impl EmitterWriter {
13761378 let file = annotated_file. file . clone ( ) ;
13771379 let line = & annotated_file. lines [ line_idx] ;
13781380 if let Some ( source_string) = file. get_line ( line. line_index - 1 ) {
1379- let leading_whitespace =
1380- source_string. chars ( ) . take_while ( |c| c. is_whitespace ( ) ) . count ( ) ;
1381+ let leading_whitespace = source_string
1382+ . chars ( )
1383+ . take_while ( |c| c. is_whitespace ( ) )
1384+ . map ( |c| {
1385+ match c {
1386+ // Tabs are displayed as 4 spaces
1387+ '\t' => 4 ,
1388+ _ => 1 ,
1389+ }
1390+ } )
1391+ . sum ( ) ;
13811392 if source_string. chars ( ) . any ( |c| !c. is_whitespace ( ) ) {
13821393 whitespace_margin = min ( whitespace_margin, leading_whitespace) ;
13831394 }
@@ -1502,7 +1513,7 @@ impl EmitterWriter {
15021513
15031514 self . draw_line (
15041515 & mut buffer,
1505- & unannotated_line,
1516+ & replace_tabs ( & unannotated_line) ,
15061517 annotated_file. lines [ line_idx + 1 ] . line_index - 1 ,
15071518 last_buffer_line_num,
15081519 width_offset,
@@ -1598,7 +1609,7 @@ impl EmitterWriter {
15981609 ) ;
15991610 // print the suggestion
16001611 draw_col_separator ( & mut buffer, row_num, max_line_num_len + 1 ) ;
1601- buffer. append ( row_num, line, Style :: NoStyle ) ;
1612+ buffer. append ( row_num, & replace_tabs ( line) , Style :: NoStyle ) ;
16021613 row_num += 1 ;
16031614 }
16041615
@@ -1930,6 +1941,10 @@ impl FileWithAnnotatedLines {
19301941 }
19311942}
19321943
1944+ fn replace_tabs ( str : & str ) -> String {
1945+ str. replace ( '\t' , " " )
1946+ }
1947+
19331948fn draw_col_separator ( buffer : & mut StyledBuffer , line : usize , col : usize ) {
19341949 buffer. puts ( line, col, "| " , Style :: LineNumber ) ;
19351950}
0 commit comments