@@ -143,11 +143,13 @@ impl Margin {
143
143
self . computed_right = self . computed_left + self . column_width ;
144
144
} else if self . label_right - self . span_left <= self . column_width {
145
145
// 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;
147
148
self . computed_right = self . computed_left + self . column_width ;
148
149
} else if self . span_right - self . span_left <= self . column_width {
149
150
// 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;
151
153
self . computed_right = self . computed_left + self . column_width ;
152
154
} else { // Mostly give up but still don't show the full line.
153
155
self . computed_left = self . span_left ;
@@ -360,13 +362,11 @@ impl EmitterWriter {
360
362
) {
361
363
let line_len = source_string. len ( ) ;
362
364
// 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 ) ;
370
370
if margin. was_cut_left ( ) {
371
371
// We have stripped some code/whitespace from the beginning, make it clear.
372
372
buffer. puts ( line_offset, code_offset, "..." , Style :: LineNumber ) ;
@@ -419,6 +419,8 @@ impl EmitterWriter {
419
419
420
420
let line_offset = buffer. num_lines ( ) ;
421
421
422
+ let left = margin. left ( source_string. len ( ) ) ; // Left trim
423
+
422
424
self . draw_line (
423
425
buffer,
424
426
& source_string,
@@ -680,15 +682,15 @@ impl EmitterWriter {
680
682
'_' ,
681
683
line_offset + pos,
682
684
width_offset + depth,
683
- code_offset + annotation. start_col - margin . computed_left ,
685
+ code_offset + annotation. start_col - left ,
684
686
style,
685
687
) ;
686
688
}
687
689
_ if self . teach => {
688
690
buffer. set_style_range (
689
691
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 ,
692
694
style,
693
695
annotation. is_primary ,
694
696
) ;
@@ -763,15 +765,20 @@ impl EmitterWriter {
763
765
Style :: LabelSecondary
764
766
} ;
765
767
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
+ } )
767
773
} 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
+ } )
769
779
} ;
770
780
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) ;
775
782
}
776
783
}
777
784
@@ -806,10 +813,16 @@ impl EmitterWriter {
806
813
( '-' , Style :: UnderlineSecondary )
807
814
} ;
808
815
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
+ ) ;
813
826
}
814
827
}
815
828
annotations_position. iter ( ) . filter_map ( |& ( _, annotation) | {
0 commit comments