Add flag to hide code on inline suggestions

Now there's a way to add suggestions that hide the suggested code when
presented inline, to avoid weird wording when short code snippets are
added at the end.
This commit is contained in:
Esteban Küber 2017-07-16 11:43:24 -07:00
parent 7239d77171
commit faf90351b7
7 changed files with 32 additions and 7 deletions

View File

@ -209,6 +209,22 @@ impl Diagnostic {
self
}
/// Prints out a message with a suggested edit of the code. If the suggestion is presented
/// inline it will only show the text message and not the text.
///
/// See `diagnostic::CodeSuggestion` for more information.
pub fn span_suggestion_short(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
self.suggestions.push(CodeSuggestion {
substitution_parts: vec![Substitution {
span: sp,
substitutions: vec![suggestion],
}],
msg: msg.to_owned(),
show_code_when_inline: false,
});
self
}
/// Prints out a message with a suggested edit of the code.
///
/// See `diagnostic::CodeSuggestion` for more information.
@ -219,6 +235,7 @@ impl Diagnostic {
substitutions: vec![suggestion],
}],
msg: msg.to_owned(),
show_code_when_inline: true,
});
self
}
@ -230,6 +247,7 @@ impl Diagnostic {
substitutions: suggestions,
}],
msg: msg.to_owned(),
show_code_when_inline: true,
});
self
}

View File

@ -146,6 +146,11 @@ impl<'a> DiagnosticBuilder<'a> {
sp: S,
msg: &str)
-> &mut Self);
forward!(pub fn span_suggestion_short(&mut self,
sp: Span,
msg: &str,
suggestion: String)
-> &mut Self);
forward!(pub fn span_suggestion(&mut self,
sp: Span,
msg: &str,

View File

@ -47,8 +47,9 @@ impl Emitter for EmitterWriter {
// 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];
let msg = if substitution.len() == 0 {
// This substitution is only removal, don't show it
let msg = if substitution.len() == 0 || !sugg.show_code_when_inline {
// This substitution is only removal or we explicitely don't want to show the
// code inline, don't show it
format!("help: {}", sugg.msg)
} else {
format!("help: {} `{}`", sugg.msg, substitution)

View File

@ -84,6 +84,7 @@ pub struct CodeSuggestion {
/// ```
pub substitution_parts: Vec<Substitution>,
pub msg: String,
pub show_code_when_inline: bool,
}
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]

View File

@ -2807,9 +2807,9 @@ impl<'a> Parser<'a> {
let cur_pos = cm.lookup_char_pos(self.span.lo);
let op_pos = cm.lookup_char_pos(cur_op_span.hi);
if cur_pos.line != op_pos.line {
err.span_suggestion(cur_op_span,
"did you mean to end the statement here instead?",
";".to_string());
err.span_suggestion_short(cur_op_span,
"did you mean to use `;` here?",
";".to_string());
}
return Err(err);
}

View File

@ -100,5 +100,5 @@ error: expected type, found `4`
--> $DIR/issue-22644.rs:38:28
|
38 | println!("{}", a: &mut 4);
| ^
| ^ expecting a type here because of type ascription

View File

@ -2,7 +2,7 @@ error: expected type, found `0`
--> $DIR/type-ascription-instead-of-statement-end.rs:15:5
|
14 | println!("test"):
| - help: did you mean to end the statement here instead? `;`
| - help: did you mean to use `;` here?
15 | 0;
| ^ expecting a type here because of type ascription