-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor internal suggestion API #45741
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,23 +38,23 @@ impl Emitter for EmitterWriter { | |
|
||
if let Some((sugg, rest)) = db.suggestions.split_first() { | ||
if rest.is_empty() && | ||
// don't display multipart suggestions as labels | ||
sugg.substitution_parts.len() == 1 && | ||
// don't display multi-suggestions as labels | ||
sugg.substitutions() == 1 && | ||
sugg.substitutions.len() == 1 && | ||
// don't display multipart suggestions as labels | ||
sugg.substitutions[0].parts.len() == 1 && | ||
// don't display long messages as labels | ||
sugg.msg.split_whitespace().count() < 10 && | ||
// don't display multiline suggestions as labels | ||
sugg.substitution_parts[0].substitutions[0].find('\n').is_none() { | ||
let substitution = &sugg.substitution_parts[0].substitutions[0]; | ||
!sugg.substitutions[0].parts[0].snippet.contains('\n') { | ||
let substitution = &sugg.substitutions[0].parts[0].snippet; | ||
let msg = if substitution.len() == 0 || !sugg.show_code_when_inline { | ||
// This substitution is only removal or we explicitly don't want to show the | ||
// code inline, don't show it | ||
format!("help: {}", sugg.msg) | ||
} else { | ||
format!("help: {}: `{}`", sugg.msg, substitution) | ||
}; | ||
primary_span.push_span_label(sugg.substitution_spans().next().unwrap(), msg); | ||
primary_span.push_span_label(sugg.substitutions[0].parts[0].span, msg); | ||
} else { | ||
// if there are multiple suggestions, print them all in full | ||
// to be consistent. We could try to figure out if we can | ||
|
@@ -1098,30 +1098,34 @@ impl EmitterWriter { | |
-> io::Result<()> { | ||
use std::borrow::Borrow; | ||
|
||
let primary_sub = &suggestion.substitution_parts[0]; | ||
if let Some(ref cm) = self.cm { | ||
let mut buffer = StyledBuffer::new(); | ||
|
||
let lines = cm.span_to_lines(primary_sub.span).unwrap(); | ||
|
||
assert!(!lines.lines.is_empty()); | ||
|
||
// Render the suggestion message | ||
buffer.append(0, &level.to_string(), 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)); | ||
&[(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(cm.borrow()); | ||
let span_start_pos = cm.lookup_char_pos(primary_sub.span.lo()); | ||
let line_start = span_start_pos.line; | ||
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1); | ||
|
||
let mut row_num = 2; | ||
for (&(ref complete, show_underline), ref sub) in suggestions | ||
.iter().zip(primary_sub.substitutions.iter()).take(MAX_SUGGESTIONS) | ||
{ | ||
for &(ref complete, ref parts) in suggestions.iter().take(MAX_SUGGESTIONS) { | ||
let show_underline = parts.len() == 1 | ||
&& complete.lines().count() == 1 | ||
&& parts[0].snippet.trim() != complete.trim(); | ||
|
||
let lines = cm.span_to_lines(parts[0].span).unwrap(); | ||
|
||
assert!(!lines.lines.is_empty()); | ||
|
||
let span_start_pos = cm.lookup_char_pos(parts[0].span.lo()); | ||
let line_start = span_start_pos.line; | ||
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1); | ||
let mut line_pos = 0; | ||
// Only show underline if there's a single suggestion and it is a single line | ||
let mut lines = complete.lines(); | ||
|
@@ -1136,21 +1140,21 @@ impl EmitterWriter { | |
buffer.append(row_num, line, Style::NoStyle); | ||
line_pos += 1; | ||
row_num += 1; | ||
// 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); | ||
let sub_len = sub.trim_right().len(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To other reviewers, this line's change is the one that causes the output change. |
||
let underline_start = span_start_pos.col.0; | ||
let underline_end = span_start_pos.col.0 + sub_len; | ||
for p in underline_start..underline_end { | ||
buffer.putc(row_num, | ||
max_line_num_len + 3 + p, | ||
'^', | ||
Style::UnderlinePrimary); | ||
} | ||
row_num += 1; | ||
} | ||
// 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); | ||
let sub_len = parts[0].snippet.len(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to include trailing whitespace in the suggestions? I feel it looks slightly cleaner when looking at it in the terminal as it is pointing at the code you need to write only, while it is still there for the RLS to suggest the properly formatted code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it was less correct, but you're right, there's no gain in knowing about the whitespace in the terminal output. I'll fix it. |
||
let underline_start = span_start_pos.col.0; | ||
let underline_end = span_start_pos.col.0 + sub_len; | ||
for p in underline_start..underline_end { | ||
buffer.putc(row_num, | ||
max_line_num_len + 3 + p, | ||
'^', | ||
Style::UnderlinePrimary); | ||
} | ||
row_num += 1; | ||
} | ||
|
||
// if we elided some lines, add an ellipsis | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realign to opening paren.