@@ -483,25 +483,39 @@ fn highlight_lines(err: &mut EmitterWriter,
483
483
cm : & codemap:: CodeMap ,
484
484
sp : Span ,
485
485
lvl : Level ,
486
- lines : codemap:: FileLines ) -> io:: Result < ( ) > {
486
+ lines : codemap:: FileLines )
487
+ -> io:: Result < ( ) >
488
+ {
487
489
let fm = & * lines. file ;
488
490
489
- let mut elided = false ;
490
- let mut display_lines = & lines. lines [ ..] ;
491
- if display_lines. len ( ) > MAX_LINES {
492
- display_lines = & display_lines[ 0 ..MAX_LINES ] ;
493
- elided = true ;
494
- }
491
+ let line_strings: Option < Vec < & str > > =
492
+ lines. lines . iter ( )
493
+ . map ( |info| fm. get_line ( info. line_index ) )
494
+ . collect ( ) ;
495
+
496
+ let line_strings = match line_strings {
497
+ None => { return Ok ( ( ) ) ; }
498
+ Some ( line_strings) => line_strings
499
+ } ;
500
+
501
+ // Display only the first MAX_LINES lines.
502
+ let all_lines = lines. lines . len ( ) ;
503
+ let display_lines = cmp:: min ( all_lines, MAX_LINES ) ;
504
+ let display_line_infos = & lines. lines [ ..display_lines] ;
505
+ let display_line_strings = & line_strings[ ..display_lines] ;
506
+
495
507
// Print the offending lines
496
- for & line_number in display_lines {
497
- if let Some ( line ) = fm . get_line ( line_number ) {
498
- try! ( write ! ( & mut err . dst , "{}:{} {} \n " , fm. name,
499
- line_number + 1 , line ) ) ;
500
- }
508
+ for ( line_info , line ) in display_line_infos . iter ( ) . zip ( display_line_strings . iter ( ) ) {
509
+ try! ( write ! ( & mut err . dst , "{}:{} {} \n " ,
510
+ fm. name,
511
+ line_info . line_index + 1 ,
512
+ line ) ) ;
501
513
}
502
- if elided {
503
- let last_line = display_lines[ display_lines. len ( ) - 1 ] ;
504
- let s = format ! ( "{}:{} " , fm. name, last_line + 1 ) ;
514
+
515
+ // If we elided something, put an ellipsis.
516
+ if display_lines < all_lines {
517
+ let last_line_index = display_line_infos. last ( ) . unwrap ( ) . line_index ;
518
+ let s = format ! ( "{}:{} " , fm. name, last_line_index + 1 ) ;
505
519
try!( write ! ( & mut err. dst, "{0:1$}...\n " , "" , s. len( ) ) ) ;
506
520
}
507
521
@@ -510,7 +524,7 @@ fn highlight_lines(err: &mut EmitterWriter,
510
524
if lines. lines . len ( ) == 1 {
511
525
let lo = cm. lookup_char_pos ( sp. lo ) ;
512
526
let mut digits = 0 ;
513
- let mut num = ( lines. lines [ 0 ] + 1 ) / 10 ;
527
+ let mut num = ( lines. lines [ 0 ] . line_index + 1 ) / 10 ;
514
528
515
529
// how many digits must be indent past?
516
530
while num > 0 { num /= 10 ; digits += 1 ; }
@@ -522,7 +536,7 @@ fn highlight_lines(err: &mut EmitterWriter,
522
536
for _ in 0 ..skip {
523
537
s. push ( ' ' ) ;
524
538
}
525
- if let Some ( orig) = fm. get_line ( lines. lines [ 0 ] ) {
539
+ if let Some ( orig) = fm. get_line ( lines. lines [ 0 ] . line_index ) {
526
540
let mut col = skip;
527
541
let mut lastc = ' ' ;
528
542
let mut iter = orig. chars ( ) . enumerate ( ) ;
@@ -597,32 +611,32 @@ fn end_highlight_lines(w: &mut EmitterWriter,
597
611
598
612
let lines = & lines. lines [ ..] ;
599
613
if lines. len ( ) > MAX_LINES {
600
- if let Some ( line) = fm. get_line ( lines[ 0 ] ) {
614
+ if let Some ( line) = fm. get_line ( lines[ 0 ] . line_index ) {
601
615
try!( write ! ( & mut w. dst, "{}:{} {}\n " , fm. name,
602
- lines[ 0 ] + 1 , line) ) ;
616
+ lines[ 0 ] . line_index + 1 , line) ) ;
603
617
}
604
618
try!( write ! ( & mut w. dst, "...\n " ) ) ;
605
- let last_line_number = lines[ lines. len ( ) - 1 ] ;
606
- if let Some ( last_line) = fm. get_line ( last_line_number ) {
619
+ let last_line_index = lines[ lines. len ( ) - 1 ] . line_index ;
620
+ if let Some ( last_line) = fm. get_line ( last_line_index ) {
607
621
try!( write ! ( & mut w. dst, "{}:{} {}\n " , fm. name,
608
- last_line_number + 1 , last_line) ) ;
622
+ last_line_index + 1 , last_line) ) ;
609
623
}
610
624
} else {
611
- for & line_number in lines {
612
- if let Some ( line) = fm. get_line ( line_number ) {
625
+ for line_info in lines {
626
+ if let Some ( line) = fm. get_line ( line_info . line_index ) {
613
627
try!( write ! ( & mut w. dst, "{}:{} {}\n " , fm. name,
614
- line_number + 1 , line) ) ;
628
+ line_info . line_index + 1 , line) ) ;
615
629
}
616
630
}
617
631
}
618
- let last_line_start = format ! ( "{}:{} " , fm. name, lines[ lines. len( ) -1 ] + 1 ) ;
632
+ let last_line_start = format ! ( "{}:{} " , fm. name, lines[ lines. len( ) -1 ] . line_index + 1 ) ;
619
633
let hi = cm. lookup_char_pos ( sp. hi ) ;
620
634
let skip = last_line_start. width ( false ) ;
621
635
let mut s = String :: new ( ) ;
622
636
for _ in 0 ..skip {
623
637
s. push ( ' ' ) ;
624
638
}
625
- if let Some ( orig) = fm. get_line ( lines[ 0 ] ) {
639
+ if let Some ( orig) = fm. get_line ( lines[ 0 ] . line_index ) {
626
640
let iter = orig. chars ( ) . enumerate ( ) ;
627
641
for ( pos, ch) in iter {
628
642
// Span seems to use half-opened interval, so subtract 1
0 commit comments