Skip to content

Commit 626afa1

Browse files
authored
Rollup merge of rust-lang#91250 - rukai:remove_trailing_whitespace, r=wesleywiser
Refactor EmitterWriter::emit_suggestion_default Makes progress towards rust-lang#89979 Split into 2 commits: * the first commit is purely a refactor and I verified that `./x.py test src/test/ui --stage 1` and `./x.py test src/test/rustdoc-ui --stage 1` continue to pass on this commit. * ~~the second commit removes the empty trailing line from diff style suggestions.~~ - I discovered an issue with this so its just the refactor now. r? diagnostics
2 parents 6e7cf2e + df3e7a2 commit 626afa1

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

compiler/rustc_errors/src/emitter.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ pub trait Emitter {
214214

215215
/// Formats the substitutions of the primary_span
216216
///
217-
/// The are a lot of conditions to this method, but in short:
217+
/// There are a lot of conditions to this method, but in short:
218218
///
219219
/// * If the current `Diagnostic` has only one visible `CodeSuggestion`,
220220
/// we format the `help` suggestion depending on the content of the
@@ -736,7 +736,9 @@ impl EmitterWriter {
736736

737737
let line_offset = buffer.num_lines();
738738

739-
let left = margin.left(source_string.len()); // Left trim
739+
// Left trim
740+
let left = margin.left(source_string.len());
741+
740742
// Account for unicode characters of width !=0 that were removed.
741743
let left = source_string
742744
.chars()
@@ -1623,18 +1625,27 @@ impl EmitterWriter {
16231625
suggestions.iter().take(MAX_SUGGESTIONS)
16241626
{
16251627
notice_capitalization |= only_capitalization;
1626-
// Only show underline if the suggestion spans a single line and doesn't cover the
1627-
// entirety of the code output. If you have multiple replacements in the same line
1628-
// of code, show the underline.
1629-
let show_underline = !(parts.len() == 1 && parts[0].snippet.trim() == complete.trim())
1630-
&& complete.lines().count() == 1;
16311628

16321629
let has_deletion = parts.iter().any(|p| p.is_deletion());
16331630
let is_multiline = complete.lines().count() > 1;
16341631

1635-
let show_diff = has_deletion && !is_multiline;
1632+
enum DisplaySuggestion {
1633+
Underline,
1634+
Diff,
1635+
None,
1636+
}
1637+
1638+
let show_code_change = if has_deletion && !is_multiline {
1639+
DisplaySuggestion::Diff
1640+
} else if (parts.len() != 1 || parts[0].snippet.trim() != complete.trim())
1641+
&& !is_multiline
1642+
{
1643+
DisplaySuggestion::Underline
1644+
} else {
1645+
DisplaySuggestion::None
1646+
};
16361647

1637-
if show_diff {
1648+
if let DisplaySuggestion::Diff = show_code_change {
16381649
row_num += 1;
16391650
}
16401651

@@ -1657,7 +1668,7 @@ impl EmitterWriter {
16571668
&self.maybe_anonymized(line_start + line_pos),
16581669
Style::LineNumber,
16591670
);
1660-
if show_diff {
1671+
if let DisplaySuggestion::Diff = show_code_change {
16611672
// Add the line number for both addition and removal to drive the point home.
16621673
//
16631674
// N - fn foo<A: T>(bar: A) {
@@ -1727,7 +1738,7 @@ impl EmitterWriter {
17271738
let mut offsets: Vec<(usize, isize)> = Vec::new();
17281739
// Only show an underline in the suggestions if the suggestion is not the
17291740
// entirety of the code being shown and the displayed code is not multiline.
1730-
if show_underline {
1741+
if let DisplaySuggestion::Diff | DisplaySuggestion::Underline = show_code_change {
17311742
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
17321743
for part in parts {
17331744
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
@@ -1755,7 +1766,7 @@ impl EmitterWriter {
17551766
assert!(underline_start >= 0 && underline_end >= 0);
17561767
let padding: usize = max_line_num_len + 3;
17571768
for p in underline_start..underline_end {
1758-
if !show_diff {
1769+
if let DisplaySuggestion::Underline = show_code_change {
17591770
// If this is a replacement, underline with `^`, if this is an addition
17601771
// underline with `+`.
17611772
buffer.putc(
@@ -1766,7 +1777,7 @@ impl EmitterWriter {
17661777
);
17671778
}
17681779
}
1769-
if show_diff {
1780+
if let DisplaySuggestion::Diff = show_code_change {
17701781
// Colorize removal with red in diff format.
17711782
buffer.set_style_range(
17721783
row_num - 2,
@@ -1797,7 +1808,7 @@ impl EmitterWriter {
17971808
// if we elided some lines, add an ellipsis
17981809
if lines.next().is_some() {
17991810
buffer.puts(row_num, max_line_num_len - 1, "...", Style::LineNumber);
1800-
} else if !show_underline {
1811+
} else if let DisplaySuggestion::None = show_code_change {
18011812
draw_col_separator_no_space(&mut buffer, row_num, max_line_num_len + 1);
18021813
row_num += 1;
18031814
}

0 commit comments

Comments
 (0)