diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2017-07-16 11:43:24 -0700 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2017-07-17 09:27:51 -0700 |
| commit | faf90351b79de04de0176b1e0de07cc5f1730eaa (patch) | |
| tree | 56e8a580fc683a21d2b6e623af9758e0b36b250c /src/librustc_errors | |
| parent | 7239d7717188f6dc2b380a40335a0c8571be6502 (diff) | |
| download | rust-faf90351b79de04de0176b1e0de07cc5f1730eaa.tar.gz rust-faf90351b79de04de0176b1e0de07cc5f1730eaa.zip | |
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.
Diffstat (limited to 'src/librustc_errors')
| -rw-r--r-- | src/librustc_errors/diagnostic.rs | 18 | ||||
| -rw-r--r-- | src/librustc_errors/diagnostic_builder.rs | 5 | ||||
| -rw-r--r-- | src/librustc_errors/emitter.rs | 5 | ||||
| -rw-r--r-- | src/librustc_errors/lib.rs | 1 |
4 files changed, 27 insertions, 2 deletions
diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index d7c21127474..2ffa5fc796e 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -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 } diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index 0081339a363..6f6470089d7 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -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, diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 2aea6d125f2..94b3a4396d7 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -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) diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index c4beb5ebc42..e873137444d 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -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)] |
