@@ -1832,6 +1832,12 @@ impl EmitterWriter {
1832
1832
}
1833
1833
let show_code_change = if has_deletion && !is_multiline {
1834
1834
DisplaySuggestion :: Diff
1835
+ } else if let [ part] = & parts[ ..]
1836
+ && part. snippet . ends_with ( '\n' )
1837
+ && part. snippet . trim ( ) == complete. trim ( )
1838
+ {
1839
+ // We are adding a line(s) of code before code that was already there.
1840
+ DisplaySuggestion :: Add
1835
1841
} else if ( parts. len ( ) != 1 || parts[ 0 ] . snippet . trim ( ) != complete. trim ( ) )
1836
1842
&& !is_multiline
1837
1843
{
@@ -1879,7 +1885,9 @@ impl EmitterWriter {
1879
1885
row_num += line_end - line_start;
1880
1886
}
1881
1887
let mut unhighlighted_lines = Vec :: new ( ) ;
1888
+ let mut last_pos = 0 ;
1882
1889
for ( line_pos, ( line, highlight_parts) ) in lines. by_ref ( ) . zip ( highlights) . enumerate ( ) {
1890
+ last_pos = line_pos;
1883
1891
debug ! ( %line_pos, %line, ?highlight_parts) ;
1884
1892
1885
1893
// Remember lines that are not highlighted to hide them if needed
@@ -1963,13 +1971,39 @@ impl EmitterWriter {
1963
1971
is_multiline,
1964
1972
)
1965
1973
}
1974
+ if let DisplaySuggestion :: Add = show_code_change {
1975
+ // The suggestion adds an entire line of code, ending on a newline, so we'll also
1976
+ // print the *following* line, to provide context of what we're advicing people to
1977
+ // do. Otherwise you would only see contextless code that can be confused for
1978
+ // already existing code, despite the colors and UI elements.
1979
+ let file_lines = sm
1980
+ . span_to_lines ( span. primary_span ( ) . unwrap ( ) . shrink_to_hi ( ) )
1981
+ . expect ( "span_to_lines failed when emitting suggestion" ) ;
1982
+ let line_num = sm. lookup_char_pos ( parts[ 0 ] . span . lo ( ) ) . line ;
1983
+ if let Some ( line) = file_lines. file . get_line ( line_num - 1 ) {
1984
+ let line = normalize_whitespace ( & line) ;
1985
+ self . draw_code_line (
1986
+ & mut buffer,
1987
+ & mut row_num,
1988
+ & [ ] ,
1989
+ line_num + last_pos + 1 ,
1990
+ & line,
1991
+ DisplaySuggestion :: None ,
1992
+ max_line_num_len,
1993
+ & file_lines,
1994
+ is_multiline,
1995
+ )
1996
+ }
1997
+ }
1966
1998
1967
1999
// This offset and the ones below need to be signed to account for replacement code
1968
2000
// that is shorter than the original code.
1969
2001
let mut offsets: Vec < ( usize , isize ) > = Vec :: new ( ) ;
1970
2002
// Only show an underline in the suggestions if the suggestion is not the
1971
2003
// entirety of the code being shown and the displayed code is not multiline.
1972
- if let DisplaySuggestion :: Diff | DisplaySuggestion :: Underline = show_code_change {
2004
+ if let DisplaySuggestion :: Diff | DisplaySuggestion :: Underline | DisplaySuggestion :: Add =
2005
+ show_code_change
2006
+ {
1973
2007
draw_col_separator_no_space ( & mut buffer, row_num, max_line_num_len + 1 ) ;
1974
2008
for part in parts {
1975
2009
let span_start_pos = sm. lookup_char_pos ( part. span . lo ( ) ) . col_display ;
@@ -2247,6 +2281,10 @@ impl EmitterWriter {
2247
2281
}
2248
2282
}
2249
2283
buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
2284
+ } else if let DisplaySuggestion :: Add = show_code_change {
2285
+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
2286
+ buffer. puts ( * row_num, max_line_num_len + 1 , "+ " , Style :: Addition ) ;
2287
+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
2250
2288
} else {
2251
2289
buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
2252
2290
draw_col_separator ( buffer, * row_num, max_line_num_len + 1 ) ;
@@ -2281,6 +2319,7 @@ enum DisplaySuggestion {
2281
2319
Underline ,
2282
2320
Diff ,
2283
2321
None ,
2322
+ Add ,
2284
2323
}
2285
2324
2286
2325
impl FileWithAnnotatedLines {
0 commit comments