@@ -1882,9 +1882,8 @@ impl EmitterWriter {
1882
1882
& mut buffer,
1883
1883
& mut row_num,
1884
1884
& Vec :: new ( ) ,
1885
- p,
1885
+ p + line_start ,
1886
1886
l,
1887
- line_start,
1888
1887
show_code_change,
1889
1888
max_line_num_len,
1890
1889
& file_lines,
@@ -1907,9 +1906,8 @@ impl EmitterWriter {
1907
1906
& mut buffer,
1908
1907
& mut row_num,
1909
1908
& Vec :: new ( ) ,
1910
- p,
1909
+ p + line_start ,
1911
1910
l,
1912
- line_start,
1913
1911
show_code_change,
1914
1912
max_line_num_len,
1915
1913
& file_lines,
@@ -1925,9 +1923,8 @@ impl EmitterWriter {
1925
1923
& mut buffer,
1926
1924
& mut row_num,
1927
1925
& Vec :: new ( ) ,
1928
- p,
1926
+ p + line_start ,
1929
1927
l,
1930
- line_start,
1931
1928
show_code_change,
1932
1929
max_line_num_len,
1933
1930
& file_lines,
@@ -1941,9 +1938,8 @@ impl EmitterWriter {
1941
1938
& mut buffer,
1942
1939
& mut row_num,
1943
1940
highlight_parts,
1944
- line_pos,
1941
+ line_pos + line_start ,
1945
1942
line,
1946
- line_start,
1947
1943
show_code_change,
1948
1944
max_line_num_len,
1949
1945
& file_lines,
@@ -2167,40 +2163,63 @@ impl EmitterWriter {
2167
2163
buffer : & mut StyledBuffer ,
2168
2164
row_num : & mut usize ,
2169
2165
highlight_parts : & Vec < SubstitutionHighlight > ,
2170
- line_pos : usize ,
2171
- line : & str ,
2172
- line_start : usize ,
2166
+ line_num : usize ,
2167
+ line_to_add : & str ,
2173
2168
show_code_change : DisplaySuggestion ,
2174
2169
max_line_num_len : usize ,
2175
2170
file_lines : & FileLines ,
2176
2171
is_multiline : bool ,
2177
2172
) {
2178
- // Print the span column to avoid confusion
2179
- buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_start + line_pos) , Style :: LineNumber ) ;
2180
2173
if let DisplaySuggestion :: Diff = show_code_change {
2181
- // Add the line number for both addition and removal to drive the point home.
2182
- //
2183
- // N - fn foo<A: T>(bar: A) {
2184
- // N + fn foo(bar: impl T) {
2185
- buffer. puts (
2186
- * row_num - 1 ,
2187
- 0 ,
2188
- & self . maybe_anonymized ( line_start + line_pos) ,
2189
- Style :: LineNumber ,
2190
- ) ;
2191
- buffer. puts ( * row_num - 1 , max_line_num_len + 1 , "- " , Style :: Removal ) ;
2192
- buffer. puts (
2193
- * row_num - 1 ,
2194
- max_line_num_len + 3 ,
2195
- & normalize_whitespace (
2196
- & file_lines. file . get_line ( file_lines. lines [ line_pos] . line_index ) . unwrap ( ) ,
2197
- ) ,
2198
- Style :: NoStyle ,
2199
- ) ;
2200
- buffer. puts ( * row_num, max_line_num_len + 1 , "+ " , Style :: Addition ) ;
2174
+ // We need to print more than one line if the span we need to remove is multiline.
2175
+ // For more info: https://github.com/rust-lang/rust/issues/92741
2176
+ let lines_to_remove = file_lines. lines . iter ( ) . take ( file_lines. lines . len ( ) - 1 ) ;
2177
+ for ( index, line_to_remove) in lines_to_remove. enumerate ( ) {
2178
+ buffer. puts (
2179
+ * row_num - 1 ,
2180
+ 0 ,
2181
+ & self . maybe_anonymized ( line_num + index) ,
2182
+ Style :: LineNumber ,
2183
+ ) ;
2184
+ buffer. puts ( * row_num - 1 , max_line_num_len + 1 , "- " , Style :: Removal ) ;
2185
+ let line = normalize_whitespace (
2186
+ & file_lines. file . get_line ( line_to_remove. line_index ) . unwrap ( ) ,
2187
+ ) ;
2188
+ buffer. puts ( * row_num - 1 , max_line_num_len + 3 , & line, Style :: NoStyle ) ;
2189
+ * row_num += 1 ;
2190
+ }
2191
+ // If the last line is exactly equal to the line we need to add, we can skip both of them.
2192
+ // This allows us to avoid output like the following:
2193
+ // 2 - &
2194
+ // 2 + if true { true } else { false }
2195
+ // 3 - if true { true } else { false }
2196
+ // If those lines aren't equal, we print their diff
2197
+ let last_line_index = file_lines. lines [ file_lines. lines . len ( ) - 1 ] . line_index ;
2198
+ let last_line = & file_lines. file . get_line ( last_line_index) . unwrap ( ) ;
2199
+ if last_line != line_to_add {
2200
+ buffer. puts (
2201
+ * row_num - 1 ,
2202
+ 0 ,
2203
+ & self . maybe_anonymized ( line_num + file_lines. lines . len ( ) - 1 ) ,
2204
+ Style :: LineNumber ,
2205
+ ) ;
2206
+ buffer. puts ( * row_num - 1 , max_line_num_len + 1 , "- " , Style :: Removal ) ;
2207
+ buffer. puts (
2208
+ * row_num - 1 ,
2209
+ max_line_num_len + 3 ,
2210
+ & normalize_whitespace ( last_line) ,
2211
+ Style :: NoStyle ,
2212
+ ) ;
2213
+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
2214
+ buffer. puts ( * row_num, max_line_num_len + 1 , "+ " , Style :: Addition ) ;
2215
+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
2216
+ } else {
2217
+ * row_num -= 2 ;
2218
+ }
2201
2219
} else if is_multiline {
2220
+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
2202
2221
match & highlight_parts[ ..] {
2203
- [ SubstitutionHighlight { start : 0 , end } ] if * end == line . len ( ) => {
2222
+ [ SubstitutionHighlight { start : 0 , end } ] if * end == line_to_add . len ( ) => {
2204
2223
buffer. puts ( * row_num, max_line_num_len + 1 , "+ " , Style :: Addition ) ;
2205
2224
}
2206
2225
[ ] => {
@@ -2210,17 +2229,17 @@ impl EmitterWriter {
2210
2229
buffer. puts ( * row_num, max_line_num_len + 1 , "~ " , Style :: Addition ) ;
2211
2230
}
2212
2231
}
2232
+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
2213
2233
} else {
2234
+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
2214
2235
draw_col_separator ( buffer, * row_num, max_line_num_len + 1 ) ;
2236
+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
2215
2237
}
2216
2238
2217
- // print the suggestion
2218
- buffer. append ( * row_num, & normalize_whitespace ( line) , Style :: NoStyle ) ;
2219
-
2220
2239
// Colorize addition/replacements with green.
2221
2240
for & SubstitutionHighlight { start, end } in highlight_parts {
2222
2241
// Account for tabs when highlighting (#87972).
2223
- let tabs: usize = line
2242
+ let tabs: usize = line_to_add
2224
2243
. chars ( )
2225
2244
. take ( start)
2226
2245
. map ( |ch| match ch {
0 commit comments