@@ -143,11 +143,13 @@ impl Margin {
143143 self . computed_right = self . computed_left + self . column_width ;
144144 } else if self . label_right - self . span_left <= self . column_width {
145145 // Attempt to fit the code window considering only the spans and labels.
146- self . computed_left = self . span_left ;
146+ let padding_left = ( self . column_width - ( self . label_right - self . span_left ) ) / 2 ;
147+ self . computed_left = self . span_left - padding_left;
147148 self . computed_right = self . computed_left + self . column_width ;
148149 } else if self . span_right - self . span_left <= self . column_width {
149150 // Attempt to fit the code window considering the spans and labels plus padding.
150- self . computed_left = self . span_left ;
151+ let padding_left = ( self . column_width - ( self . span_right - self . span_left ) ) / 2 ;
152+ self . computed_left = self . span_left - padding_left;
151153 self . computed_right = self . computed_left + self . column_width ;
152154 } else { // Mostly give up but still don't show the full line.
153155 self . computed_left = self . span_left ;
@@ -360,13 +362,11 @@ impl EmitterWriter {
360362 ) {
361363 let line_len = source_string. len ( ) ;
362364 // Create the source line we will highlight.
363- buffer. puts (
364- line_offset,
365- code_offset,
366- // On long lines, we strip the source line
367- & source_string[ margin. left ( line_len) ..margin. right ( line_len) ] ,
368- Style :: Quotation ,
369- ) ;
365+ let left = margin. left ( line_len) ;
366+ let right = margin. right ( line_len) ;
367+ // On long lines, we strip the source line, accounting for unicode.
368+ let code: String = source_string. chars ( ) . skip ( left) . take ( right - left) . collect ( ) ;
369+ buffer. puts ( line_offset, code_offset, & code, Style :: Quotation ) ;
370370 if margin. was_cut_left ( ) {
371371 // We have stripped some code/whitespace from the beginning, make it clear.
372372 buffer. puts ( line_offset, code_offset, "..." , Style :: LineNumber ) ;
@@ -419,6 +419,8 @@ impl EmitterWriter {
419419
420420 let line_offset = buffer. num_lines ( ) ;
421421
422+ let left = margin. left ( source_string. len ( ) ) ; // Left trim
423+
422424 self . draw_line (
423425 buffer,
424426 & source_string,
@@ -680,15 +682,15 @@ impl EmitterWriter {
680682 '_' ,
681683 line_offset + pos,
682684 width_offset + depth,
683- code_offset + annotation. start_col - margin . computed_left ,
685+ code_offset + annotation. start_col - left ,
684686 style,
685687 ) ;
686688 }
687689 _ if self . teach => {
688690 buffer. set_style_range (
689691 line_offset,
690- code_offset + annotation. start_col - margin . computed_left ,
691- code_offset + annotation. end_col - margin . computed_left ,
692+ code_offset + annotation. start_col - left ,
693+ code_offset + annotation. end_col - left ,
692694 style,
693695 annotation. is_primary ,
694696 ) ;
@@ -763,15 +765,20 @@ impl EmitterWriter {
763765 Style :: LabelSecondary
764766 } ;
765767 let ( pos, col) = if pos == 0 {
766- ( pos + 1 , annotation. end_col + 1 - margin. computed_left )
768+ ( pos + 1 , if annotation. end_col + 1 > left {
769+ annotation. end_col + 1 - left
770+ } else {
771+ 0
772+ } )
767773 } else {
768- ( pos + 2 , annotation. start_col - margin. computed_left )
774+ ( pos + 2 , if annotation. start_col > left {
775+ annotation. start_col - left
776+ } else {
777+ 0
778+ } )
769779 } ;
770780 if let Some ( ref label) = annotation. label {
771- buffer. puts ( line_offset + pos,
772- code_offset + col,
773- & label,
774- style) ;
781+ buffer. puts ( line_offset + pos, code_offset + col, & label, style) ;
775782 }
776783 }
777784
@@ -806,10 +813,16 @@ impl EmitterWriter {
806813 ( '-' , Style :: UnderlineSecondary )
807814 } ;
808815 for p in annotation. start_col ..annotation. end_col {
809- buffer. putc ( line_offset + 1 ,
810- code_offset + p - margin. computed_left ,
811- underline,
812- style) ;
816+ buffer. putc (
817+ line_offset + 1 ,
818+ if code_offset + p > left {
819+ code_offset + p - left
820+ } else {
821+ 0
822+ } ,
823+ underline,
824+ style,
825+ ) ;
813826 }
814827 }
815828 annotations_position. iter ( ) . filter_map ( |& ( _, annotation) | {
0 commit comments