@@ -13,7 +13,7 @@ use super::DecorStyle;
1313use super :: Renderer ;
1414use crate :: level:: { Level , LevelInner } ;
1515use crate :: renderer:: source_map:: {
16- AnnotatedLineInfo , LineInfo , Loc , SourceMap , SplicedLines , SubstitutionHighlight ,
16+ AnnotatedLineInfo , LineInfo , Loc , SourceMap , SplicedLines , SubstitutionHighlight , TrimmedPatch ,
1717} ;
1818use crate :: renderer:: styled_buffer:: StyledBuffer ;
1919use crate :: snippet:: Id ;
@@ -145,14 +145,20 @@ pub(crate) fn render(renderer: &Renderer, groups: Report<'_>) -> String {
145145 }
146146 }
147147 }
148- PreProcessedElement :: Suggestion ( ( suggestion, source_map, spliced_lines) ) => {
148+ PreProcessedElement :: Suggestion ( (
149+ suggestion,
150+ source_map,
151+ spliced_lines,
152+ display_suggestion,
153+ ) ) => {
149154 let matches_previous_suggestion =
150155 last_suggestion_path == Some ( suggestion. path . as_ref ( ) ) ;
151156 emit_suggestion_default (
152157 renderer,
153158 & mut buffer,
154159 suggestion,
155160 spliced_lines,
161+ display_suggestion,
156162 max_line_num_len,
157163 & source_map,
158164 primary_path. or ( og_primary_path) ,
@@ -1427,6 +1433,7 @@ fn emit_suggestion_default(
14271433 buffer : & mut StyledBuffer ,
14281434 suggestion : & Snippet < ' _ , Patch < ' _ > > ,
14291435 spliced_lines : SplicedLines < ' _ > ,
1436+ show_code_change : DisplaySuggestion ,
14301437 max_line_num_len : usize ,
14311438 sm : & SourceMap < ' _ > ,
14321439 primary_path : Option < & Cow < ' _ , str > > ,
@@ -1437,9 +1444,6 @@ fn emit_suggestion_default(
14371444 let buffer_offset = buffer. num_lines ( ) ;
14381445 let mut row_num = buffer_offset + usize:: from ( !matches_previous_suggestion) ;
14391446 let ( complete, parts, highlights) = spliced_lines;
1440- let has_deletion = parts
1441- . iter ( )
1442- . any ( |p| p. is_deletion ( sm) || p. is_destructive_replacement ( sm) ) ;
14431447 let is_multiline = complete. lines ( ) . count ( ) > 1 ;
14441448
14451449 if matches_previous_suggestion {
@@ -1471,21 +1475,6 @@ fn emit_suggestion_default(
14711475 }
14721476 }
14731477 }
1474- let show_code_change = if has_deletion && !is_multiline {
1475- DisplaySuggestion :: Diff
1476- } else if parts. len ( ) == 1
1477- && parts. first ( ) . map_or ( false , |p| {
1478- p. replacement . ends_with ( '\n' ) && p. replacement . trim ( ) == complete. trim ( )
1479- } )
1480- {
1481- // We are adding a line(s) of code before code that was already there.
1482- DisplaySuggestion :: Add
1483- } else if ( parts. len ( ) != 1 || parts[ 0 ] . replacement . trim ( ) != complete. trim ( ) ) && !is_multiline
1484- {
1485- DisplaySuggestion :: Underline
1486- } else {
1487- DisplaySuggestion :: None
1488- } ;
14891478
14901479 if let DisplaySuggestion :: Diff = show_code_change {
14911480 row_num += 1 ;
@@ -2480,6 +2469,31 @@ pub(crate) enum DisplaySuggestion {
24802469 Add ,
24812470}
24822471
2472+ impl DisplaySuggestion {
2473+ fn new ( complete : & str , patches : & [ TrimmedPatch < ' _ > ] , sm : & SourceMap < ' _ > ) -> Self {
2474+ let has_deletion = patches
2475+ . iter ( )
2476+ . any ( |p| p. is_deletion ( sm) || p. is_destructive_replacement ( sm) ) ;
2477+ let is_multiline = complete. lines ( ) . count ( ) > 1 ;
2478+ if has_deletion && !is_multiline {
2479+ DisplaySuggestion :: Diff
2480+ } else if patches. len ( ) == 1
2481+ && patches. first ( ) . map_or ( false , |p| {
2482+ p. replacement . ends_with ( '\n' ) && p. replacement . trim ( ) == complete. trim ( )
2483+ } )
2484+ {
2485+ // We are adding a line(s) of code before code that was already there.
2486+ DisplaySuggestion :: Add
2487+ } else if ( patches. len ( ) != 1 || patches[ 0 ] . replacement . trim ( ) != complete. trim ( ) )
2488+ && !is_multiline
2489+ {
2490+ DisplaySuggestion :: Underline
2491+ } else {
2492+ DisplaySuggestion :: None
2493+ }
2494+ }
2495+ }
2496+
24832497// We replace some characters so the CLI output is always consistent and underlines aligned.
24842498// Keep the following list in sync with `rustc_span::char_width`.
24852499const OUTPUT_REPLACEMENTS : & [ ( char , & str ) ] = & [
@@ -2619,7 +2633,14 @@ enum PreProcessedElement<'a> {
26192633 Vec < AnnotatedLineInfo < ' a > > ,
26202634 ) ,
26212635 ) ,
2622- Suggestion ( ( & ' a Snippet < ' a , Patch < ' a > > , SourceMap < ' a > , SplicedLines < ' a > ) ) ,
2636+ Suggestion (
2637+ (
2638+ & ' a Snippet < ' a , Patch < ' a > > ,
2639+ SourceMap < ' a > ,
2640+ SplicedLines < ' a > ,
2641+ DisplaySuggestion ,
2642+ ) ,
2643+ ) ,
26232644 Origin ( & ' a Origin < ' a > ) ,
26242645 Padding ( Padding ) ,
26252646}
@@ -2672,9 +2693,11 @@ fn pre_process<'a>(
26722693 }
26732694 Element :: Suggestion ( suggestion) => {
26742695 let sm = SourceMap :: new ( & suggestion. source , suggestion. line_start ) ;
2675- if let Some ( spliced_lines ) =
2696+ if let Some ( ( complete , patches , highlights ) ) =
26762697 sm. splice_lines ( suggestion. markers . clone ( ) , suggestion. fold )
26772698 {
2699+ let display_suggestion = DisplaySuggestion :: new ( & complete, & patches, & sm) ;
2700+
26782701 if suggestion. fold {
26792702 let end = suggestion
26802703 . markers
@@ -2698,7 +2721,8 @@ fn pre_process<'a>(
26982721 elements. push ( PreProcessedElement :: Suggestion ( (
26992722 suggestion,
27002723 sm,
2701- spliced_lines,
2724+ ( complete, patches, highlights) ,
2725+ display_suggestion,
27022726 ) ) ) ;
27032727 }
27042728 }
0 commit comments