@@ -967,21 +967,7 @@ impl Renderer {
967
967
968
968
let line_offset = buffer. num_lines ( ) ;
969
969
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 (
985
971
buffer,
986
972
& source_string,
987
973
line_info. line_index ,
@@ -1136,11 +1122,16 @@ impl Renderer {
1136
1122
// | x_span
1137
1123
// <EMPTY LINE>
1138
1124
//
1125
+ let mut overlap = vec ! [ false ; annotations. len( ) ] ;
1139
1126
let mut annotations_position = vec ! [ ] ;
1140
1127
let mut line_len: usize = 0 ;
1141
1128
let mut p = 0 ;
1142
1129
for ( i, annotation) in annotations. iter ( ) . enumerate ( ) {
1143
1130
for ( j, next) in annotations. iter ( ) . enumerate ( ) {
1131
+ if overlaps ( next, annotation, 0 ) && j > 1 {
1132
+ overlap[ i] = true ;
1133
+ overlap[ j] = true ;
1134
+ }
1144
1135
if overlaps ( next, annotation, 0 ) // This label overlaps with another one and both
1145
1136
&& annotation. has_label ( ) // take space (they have text and are not
1146
1137
&& j > i // multiline lines).
@@ -1488,6 +1479,39 @@ impl Renderer {
1488
1479
) ;
1489
1480
}
1490
1481
}
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
+ }
1491
1515
annotations_position
1492
1516
. iter ( )
1493
1517
. filter_map ( |& ( _, annotation) | match annotation. annotation_type {
@@ -2036,12 +2060,12 @@ impl Renderer {
2036
2060
code_offset : usize ,
2037
2061
max_line_num_len : usize ,
2038
2062
margin : Margin ,
2039
- ) {
2063
+ ) -> usize {
2040
2064
// Tabs are assumed to have been replaced by spaces in calling code.
2041
2065
debug_assert ! ( !source_string. contains( '\t' ) ) ;
2042
2066
let line_len = str_width ( source_string) ;
2043
2067
// Create the source line we will highlight.
2044
- let left = margin. left ( line_len) ;
2068
+ let mut left = margin. left ( line_len) ;
2045
2069
let right = margin. right ( line_len) ;
2046
2070
// FIXME: The following code looks fishy. See #132860.
2047
2071
// On long lines, we strip the source line, accounting for unicode.
@@ -2074,10 +2098,15 @@ impl Renderer {
2074
2098
break ;
2075
2099
}
2076
2100
}
2101
+
2102
+ if width_taken > padding {
2103
+ left -= width_taken - padding;
2104
+ }
2105
+
2077
2106
buffer. puts (
2078
2107
line_offset,
2079
2108
code_offset,
2080
- & format ! ( "{ placeholder:>width_taken$}" ) ,
2109
+ placeholder,
2081
2110
ElementStyle :: LineNumber ,
2082
2111
) ;
2083
2112
( width_taken, bytes_taken)
@@ -2092,7 +2121,7 @@ impl Renderer {
2092
2121
ElementStyle :: Quotation ,
2093
2122
) ;
2094
2123
2095
- if margin . was_cut_right ( line_len) {
2124
+ if line_len > right {
2096
2125
// We have stripped some code/whitespace from the beginning, make it clear.
2097
2126
let mut char_taken = 0 ;
2098
2127
let mut width_taken_inner = 0 ;
@@ -2121,6 +2150,8 @@ impl Renderer {
2121
2150
) ;
2122
2151
2123
2152
self . draw_col_separator_no_space ( buffer, line_offset, width_offset - 2 ) ;
2153
+
2154
+ left
2124
2155
}
2125
2156
2126
2157
fn draw_range (
0 commit comments