Trim suggestion parts to the subset that is purely additive
This commit is contained in:
parent
f6406dfd4e
commit
6d71251cf9
148 changed files with 321 additions and 304 deletions
|
@ -1976,9 +1976,11 @@ impl HumanEmitter {
|
|||
Some(Style::HeaderMsg),
|
||||
);
|
||||
|
||||
let other_suggestions = suggestions.len().saturating_sub(MAX_SUGGESTIONS);
|
||||
|
||||
let mut row_num = 2;
|
||||
for (i, (complete, parts, highlights, _)) in
|
||||
suggestions.iter().enumerate().take(MAX_SUGGESTIONS)
|
||||
suggestions.into_iter().enumerate().take(MAX_SUGGESTIONS)
|
||||
{
|
||||
debug!(?complete, ?parts, ?highlights);
|
||||
|
||||
|
@ -2168,7 +2170,7 @@ impl HumanEmitter {
|
|||
self.draw_code_line(
|
||||
&mut buffer,
|
||||
&mut row_num,
|
||||
highlight_parts,
|
||||
&highlight_parts,
|
||||
line_pos + line_start,
|
||||
line,
|
||||
show_code_change,
|
||||
|
@ -2214,7 +2216,12 @@ impl HumanEmitter {
|
|||
if let DisplaySuggestion::Diff | DisplaySuggestion::Underline | DisplaySuggestion::Add =
|
||||
show_code_change
|
||||
{
|
||||
for part in parts {
|
||||
for mut part in parts {
|
||||
// If this is a replacement of, e.g. `"a"` into `"ab"`, adjust the
|
||||
// suggestion and snippet to look as if we just suggested to add
|
||||
// `"b"`, which is typically much easier for the user to understand.
|
||||
part.trim_trivial_replacements(sm);
|
||||
|
||||
let snippet = if let Ok(snippet) = sm.span_to_snippet(part.span) {
|
||||
snippet
|
||||
} else {
|
||||
|
@ -2377,9 +2384,12 @@ impl HumanEmitter {
|
|||
row_num = row + 1;
|
||||
}
|
||||
}
|
||||
if suggestions.len() > MAX_SUGGESTIONS {
|
||||
let others = suggestions.len() - MAX_SUGGESTIONS;
|
||||
let msg = format!("and {} other candidate{}", others, pluralize!(others));
|
||||
if other_suggestions > 0 {
|
||||
let msg = format!(
|
||||
"and {} other candidate{}",
|
||||
other_suggestions,
|
||||
pluralize!(other_suggestions)
|
||||
);
|
||||
buffer.puts(row_num, max_line_num_len + 3, &msg, Style::NoStyle);
|
||||
}
|
||||
|
||||
|
|
|
@ -246,6 +246,24 @@ impl SubstitutionPart {
|
|||
sm.span_to_snippet(self.span)
|
||||
.map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())
|
||||
}
|
||||
|
||||
/// Try to turn a replacement into an addition when the span that is being
|
||||
/// overwritten matches either the prefix or suffix of the replacement.
|
||||
fn trim_trivial_replacements(&mut self, sm: &SourceMap) {
|
||||
if self.snippet.is_empty() {
|
||||
return;
|
||||
}
|
||||
let Ok(snippet) = sm.span_to_snippet(self.span) else {
|
||||
return;
|
||||
};
|
||||
if self.snippet.starts_with(&snippet) {
|
||||
self.span = self.span.shrink_to_hi();
|
||||
self.snippet = self.snippet[snippet.len()..].to_string();
|
||||
} else if self.snippet.ends_with(&snippet) {
|
||||
self.span = self.span.shrink_to_lo();
|
||||
self.snippet = self.snippet[..self.snippet.len() - snippet.len()].to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CodeSuggestion {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue