Simplify EmitterWriter::emit_suggestion_default
Make function return early if source map is not present
This commit is contained in:
parent
21724eda05
commit
7a807c5b14
@ -1431,122 +1431,125 @@ impl EmitterWriter {
|
||||
level: &Level,
|
||||
max_line_num_len: usize,
|
||||
) -> io::Result<()> {
|
||||
if let Some(ref sm) = self.sm {
|
||||
let mut buffer = StyledBuffer::new();
|
||||
let sm = match self.sm {
|
||||
Some(ref sm) => sm,
|
||||
None => return Ok(())
|
||||
};
|
||||
|
||||
// Render the suggestion message
|
||||
let level_str = level.to_string();
|
||||
if !level_str.is_empty() {
|
||||
buffer.append(0, &level_str, Style::Level(level.clone()));
|
||||
buffer.append(0, ": ", Style::HeaderMsg);
|
||||
let mut buffer = StyledBuffer::new();
|
||||
|
||||
// Render the suggestion message
|
||||
let level_str = level.to_string();
|
||||
if !level_str.is_empty() {
|
||||
buffer.append(0, &level_str, Style::Level(level.clone()));
|
||||
buffer.append(0, ": ", Style::HeaderMsg);
|
||||
}
|
||||
self.msg_to_buffer(
|
||||
&mut buffer,
|
||||
&[(suggestion.msg.to_owned(), Style::NoStyle)],
|
||||
max_line_num_len,
|
||||
"suggestion",
|
||||
Some(Style::HeaderMsg),
|
||||
);
|
||||
|
||||
// Render the replacements for each suggestion
|
||||
let suggestions = suggestion.splice_lines(&**sm);
|
||||
|
||||
let mut row_num = 2;
|
||||
for &(ref complete, ref parts) in suggestions.iter().take(MAX_SUGGESTIONS) {
|
||||
// Only show underline if the suggestion spans a single line and doesn't cover the
|
||||
// entirety of the code output. If you have multiple replacements in the same line
|
||||
// of code, show the underline.
|
||||
let show_underline = !(parts.len() == 1
|
||||
&& parts[0].snippet.trim() == complete.trim())
|
||||
&& complete.lines().count() == 1;
|
||||
|
||||
let lines = sm.span_to_lines(parts[0].span).unwrap();
|
||||
|
||||
assert!(!lines.lines.is_empty());
|
||||
|
||||
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
|
||||
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
|
||||
let mut line_pos = 0;
|
||||
let mut lines = complete.lines();
|
||||
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
|
||||
// Print the span column to avoid confusion
|
||||
buffer.puts(row_num,
|
||||
0,
|
||||
&self.maybe_anonymized(line_start + line_pos),
|
||||
Style::LineNumber);
|
||||
// print the suggestion
|
||||
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
|
||||
buffer.append(row_num, line, Style::NoStyle);
|
||||
line_pos += 1;
|
||||
row_num += 1;
|
||||
}
|
||||
self.msg_to_buffer(
|
||||
&mut buffer,
|
||||
&[(suggestion.msg.to_owned(), Style::NoStyle)],
|
||||
max_line_num_len,
|
||||
"suggestion",
|
||||
Some(Style::HeaderMsg),
|
||||
);
|
||||
|
||||
// Render the replacements for each suggestion
|
||||
let suggestions = suggestion.splice_lines(&**sm);
|
||||
// This offset and the ones below need to be signed to account for replacement code
|
||||
// that is shorter than the original code.
|
||||
let mut offset: isize = 0;
|
||||
// Only show an underline in the suggestions if the suggestion is not the
|
||||
// entirety of the code being shown and the displayed code is not multiline.
|
||||
if show_underline {
|
||||
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
|
||||
for part in parts {
|
||||
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
|
||||
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;
|
||||
|
||||
let mut row_num = 2;
|
||||
for &(ref complete, ref parts) in suggestions.iter().take(MAX_SUGGESTIONS) {
|
||||
// Only show underline if the suggestion spans a single line and doesn't cover the
|
||||
// entirety of the code output. If you have multiple replacements in the same line
|
||||
// of code, show the underline.
|
||||
let show_underline = !(parts.len() == 1
|
||||
&& parts[0].snippet.trim() == complete.trim())
|
||||
&& complete.lines().count() == 1;
|
||||
// Do not underline the leading...
|
||||
let start = part.snippet.len()
|
||||
.saturating_sub(part.snippet.trim_start().len());
|
||||
// ...or trailing spaces. Account for substitutions containing unicode
|
||||
// characters.
|
||||
let sub_len = part.snippet.trim().chars()
|
||||
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
|
||||
.sum();
|
||||
|
||||
let lines = sm.span_to_lines(parts[0].span).unwrap();
|
||||
|
||||
assert!(!lines.lines.is_empty());
|
||||
|
||||
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
|
||||
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
|
||||
let mut line_pos = 0;
|
||||
let mut lines = complete.lines();
|
||||
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
|
||||
// Print the span column to avoid confusion
|
||||
buffer.puts(row_num,
|
||||
0,
|
||||
&self.maybe_anonymized(line_start + line_pos),
|
||||
Style::LineNumber);
|
||||
// print the suggestion
|
||||
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
|
||||
buffer.append(row_num, line, Style::NoStyle);
|
||||
line_pos += 1;
|
||||
row_num += 1;
|
||||
}
|
||||
|
||||
// This offset and the ones below need to be signed to account for replacement code
|
||||
// that is shorter than the original code.
|
||||
let mut offset: isize = 0;
|
||||
// Only show an underline in the suggestions if the suggestion is not the
|
||||
// entirety of the code being shown and the displayed code is not multiline.
|
||||
if show_underline {
|
||||
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
|
||||
for part in parts {
|
||||
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
|
||||
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;
|
||||
|
||||
// Do not underline the leading...
|
||||
let start = part.snippet.len()
|
||||
.saturating_sub(part.snippet.trim_start().len());
|
||||
// ...or trailing spaces. Account for substitutions containing unicode
|
||||
// characters.
|
||||
let sub_len = part.snippet.trim().chars()
|
||||
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
|
||||
.sum();
|
||||
|
||||
let underline_start = (span_start_pos + start) as isize + offset;
|
||||
let underline_end = (span_start_pos + start + sub_len) as isize + offset;
|
||||
for p in underline_start..underline_end {
|
||||
let underline_start = (span_start_pos + start) as isize + offset;
|
||||
let underline_end = (span_start_pos + start + sub_len) as isize + offset;
|
||||
for p in underline_start..underline_end {
|
||||
buffer.putc(row_num,
|
||||
max_line_num_len + 3 + p as usize,
|
||||
'^',
|
||||
Style::UnderlinePrimary);
|
||||
}
|
||||
// underline removals too
|
||||
if underline_start == underline_end {
|
||||
for p in underline_start-1..underline_start+1 {
|
||||
buffer.putc(row_num,
|
||||
max_line_num_len + 3 + p as usize,
|
||||
'^',
|
||||
Style::UnderlinePrimary);
|
||||
'-',
|
||||
Style::UnderlineSecondary);
|
||||
}
|
||||
// underline removals too
|
||||
if underline_start == underline_end {
|
||||
for p in underline_start-1..underline_start+1 {
|
||||
buffer.putc(row_num,
|
||||
max_line_num_len + 3 + p as usize,
|
||||
'-',
|
||||
Style::UnderlineSecondary);
|
||||
}
|
||||
}
|
||||
|
||||
// length of the code after substitution
|
||||
let full_sub_len = part.snippet.chars()
|
||||
.map(|ch| acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
|
||||
.sum() as isize;
|
||||
|
||||
// length of the code to be substituted
|
||||
let snippet_len = span_end_pos as isize - span_start_pos as isize;
|
||||
// For multiple substitutions, use the position *after* the previous
|
||||
// substitutions have happened.
|
||||
offset += full_sub_len - snippet_len;
|
||||
}
|
||||
row_num += 1;
|
||||
}
|
||||
|
||||
// if we elided some lines, add an ellipsis
|
||||
if lines.next().is_some() {
|
||||
buffer.puts(row_num, max_line_num_len - 1, "...", Style::LineNumber);
|
||||
} else if !show_underline {
|
||||
draw_col_separator_no_space(&mut buffer, row_num, max_line_num_len + 1);
|
||||
row_num += 1;
|
||||
// length of the code after substitution
|
||||
let full_sub_len = part.snippet.chars()
|
||||
.map(|ch| acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
|
||||
.sum() as isize;
|
||||
|
||||
// length of the code to be substituted
|
||||
let snippet_len = span_end_pos as isize - span_start_pos as isize;
|
||||
// For multiple substitutions, use the position *after* the previous
|
||||
// substitutions have happened.
|
||||
offset += full_sub_len - snippet_len;
|
||||
}
|
||||
row_num += 1;
|
||||
}
|
||||
if suggestions.len() > MAX_SUGGESTIONS {
|
||||
let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS);
|
||||
buffer.puts(row_num, 0, &msg, Style::NoStyle);
|
||||
|
||||
// if we elided some lines, add an ellipsis
|
||||
if lines.next().is_some() {
|
||||
buffer.puts(row_num, max_line_num_len - 1, "...", Style::LineNumber);
|
||||
} else if !show_underline {
|
||||
draw_col_separator_no_space(&mut buffer, row_num, max_line_num_len + 1);
|
||||
row_num += 1;
|
||||
}
|
||||
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
|
||||
}
|
||||
if suggestions.len() > MAX_SUGGESTIONS {
|
||||
let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS);
|
||||
buffer.puts(row_num, 0, &msg, Style::NoStyle);
|
||||
}
|
||||
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user