@@ -967,21 +967,7 @@ impl Renderer {
967967
968968 let line_offset = buffer. num_lines ( ) ;
969969
970- // Left trim
971- let left = margin. left ( str_width ( & source_string) ) ;
972-
973- // FIXME: This looks fishy. See #132860.
974- // Account for unicode characters of width !=0 that were removed.
975- let mut taken = 0 ;
976- source_string. chars ( ) . for_each ( |ch| {
977- let next = char_width ( ch) ;
978- if taken + next <= left {
979- taken += next;
980- }
981- } ) ;
982-
983- let left = taken;
984- self . draw_line (
970+ let left = self . draw_line (
985971 buffer,
986972 & source_string,
987973 line_info. line_index ,
@@ -1136,11 +1122,16 @@ impl Renderer {
11361122 // | x_span
11371123 // <EMPTY LINE>
11381124 //
1125+ let mut overlap = vec ! [ false ; annotations. len( ) ] ;
11391126 let mut annotations_position = vec ! [ ] ;
11401127 let mut line_len: usize = 0 ;
11411128 let mut p = 0 ;
11421129 for ( i, annotation) in annotations. iter ( ) . enumerate ( ) {
11431130 for ( j, next) in annotations. iter ( ) . enumerate ( ) {
1131+ if overlaps ( next, annotation, 0 ) && j > 1 {
1132+ overlap[ i] = true ;
1133+ overlap[ j] = true ;
1134+ }
11441135 if overlaps ( next, annotation, 0 ) // This label overlaps with another one and both
11451136 && annotation. has_label ( ) // take space (they have text and are not
11461137 && j > i // multiline lines).
@@ -1488,6 +1479,39 @@ impl Renderer {
14881479 ) ;
14891480 }
14901481 }
1482+
1483+ // We look for individual *long* spans, and we trim the *middle*, so that we render
1484+ // LL | ...= [0, 0, 0, ..., 0, 0];
1485+ // | ^^^^^^^^^^...^^^^^^^ expected `&[u8]`, found `[{integer}; 1680]`
1486+ for ( i, ( _pos, annotation) ) in annotations_position. iter ( ) . enumerate ( ) {
1487+ // Skip cases where multiple spans overlap eachother.
1488+ if overlap[ i] {
1489+ continue ;
1490+ } ;
1491+ let LineAnnotationType :: Singleline = annotation. annotation_type else {
1492+ continue ;
1493+ } ;
1494+ let width = annotation. end . display - annotation. start . display ;
1495+ if width > margin. term_width * 2 && width > 10 {
1496+ // If the terminal is *too* small, we keep at least a tiny bit of the span for
1497+ // display.
1498+ let pad = max ( margin. term_width / 3 , 5 ) ;
1499+ // Code line
1500+ buffer. replace (
1501+ line_offset,
1502+ annotation. start . display + pad,
1503+ annotation. end . display - pad,
1504+ self . margin ( ) ,
1505+ ) ;
1506+ // Underline line
1507+ buffer. replace (
1508+ line_offset + 1 ,
1509+ annotation. start . display + pad,
1510+ annotation. end . display - pad,
1511+ self . margin ( ) ,
1512+ ) ;
1513+ }
1514+ }
14911515 annotations_position
14921516 . iter ( )
14931517 . filter_map ( |& ( _, annotation) | match annotation. annotation_type {
@@ -2036,12 +2060,12 @@ impl Renderer {
20362060 code_offset : usize ,
20372061 max_line_num_len : usize ,
20382062 margin : Margin ,
2039- ) {
2063+ ) -> usize {
20402064 // Tabs are assumed to have been replaced by spaces in calling code.
20412065 debug_assert ! ( !source_string. contains( '\t' ) ) ;
20422066 let line_len = str_width ( source_string) ;
20432067 // Create the source line we will highlight.
2044- let left = margin. left ( line_len) ;
2068+ let mut left = margin. left ( line_len) ;
20452069 let right = margin. right ( line_len) ;
20462070 // FIXME: The following code looks fishy. See #132860.
20472071 // On long lines, we strip the source line, accounting for unicode.
@@ -2074,10 +2098,15 @@ impl Renderer {
20742098 break ;
20752099 }
20762100 }
2101+
2102+ if width_taken > padding {
2103+ left -= width_taken - padding;
2104+ }
2105+
20772106 buffer. puts (
20782107 line_offset,
20792108 code_offset,
2080- & format ! ( "{ placeholder:>width_taken$}" ) ,
2109+ placeholder,
20812110 ElementStyle :: LineNumber ,
20822111 ) ;
20832112 ( width_taken, bytes_taken)
@@ -2092,7 +2121,7 @@ impl Renderer {
20922121 ElementStyle :: Quotation ,
20932122 ) ;
20942123
2095- if margin . was_cut_right ( line_len) {
2124+ if line_len > right {
20962125 // We have stripped some code/whitespace from the beginning, make it clear.
20972126 let mut char_taken = 0 ;
20982127 let mut width_taken_inner = 0 ;
@@ -2121,6 +2150,8 @@ impl Renderer {
21212150 ) ;
21222151
21232152 self . draw_col_separator_no_space ( buffer, line_offset, width_offset - 2 ) ;
2153+
2154+ left
21242155 }
21252156
21262157 fn draw_range (
0 commit comments