Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 05b4e7c

Browse files
committed
Add way to hide suggestion snippet window from cli output
1 parent 57d7cfc commit 05b4e7c

File tree

3 files changed

+84
-36
lines changed

3 files changed

+84
-36
lines changed

src/librustc_errors/diagnostic.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::CodeSuggestion;
2+
use crate::SuggestionStyle;
23
use crate::SubstitutionPart;
34
use crate::Substitution;
45
use crate::Applicability;
@@ -243,7 +244,7 @@ impl Diagnostic {
243244
.collect(),
244245
}],
245246
msg: msg.to_owned(),
246-
show_code_when_inline: true,
247+
style: SuggestionStyle::ShowCode,
247248
applicability,
248249
});
249250
self
@@ -277,7 +278,7 @@ impl Diagnostic {
277278
}],
278279
}],
279280
msg: msg.to_owned(),
280-
show_code_when_inline: true,
281+
style: SuggestionStyle::ShowCode,
281282
applicability,
282283
});
283284
self
@@ -295,7 +296,7 @@ impl Diagnostic {
295296
}],
296297
}).collect(),
297298
msg: msg.to_owned(),
298-
show_code_when_inline: true,
299+
style: SuggestionStyle::ShowCode,
299300
applicability,
300301
});
301302
self
@@ -316,7 +317,7 @@ impl Diagnostic {
316317
}],
317318
}],
318319
msg: msg.to_owned(),
319-
show_code_when_inline: false,
320+
style: SuggestionStyle::HideCodeInline,
320321
applicability: applicability,
321322
});
322323
self

src/librustc_errors/emitter.rs

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use Destination::*;
22

33
use syntax_pos::{SourceFile, Span, MultiSpan};
44

5-
use crate::{Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, SourceMapperDyn, DiagnosticId};
5+
use crate::{
6+
Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic,
7+
SuggestionStyle, SourceMapperDyn, DiagnosticId,
8+
};
69
use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledString, Style};
710
use crate::styled_buffer::StyledBuffer;
811

@@ -45,7 +48,7 @@ impl Emitter for EmitterWriter {
4548
// don't display multiline suggestions as labels
4649
!sugg.substitutions[0].parts[0].snippet.contains('\n') {
4750
let substitution = &sugg.substitutions[0].parts[0].snippet.trim();
48-
let msg = if substitution.len() == 0 || !sugg.show_code_when_inline {
51+
let msg = if substitution.len() == 0 || sugg.style.hide_inline() {
4952
// This substitution is only removal or we explicitly don't want to show the
5053
// code inline, don't show it
5154
format!("help: {}", sugg.msg)
@@ -942,14 +945,15 @@ impl EmitterWriter {
942945
}
943946
}
944947

945-
fn emit_message_default(&mut self,
946-
msp: &MultiSpan,
947-
msg: &[(String, Style)],
948-
code: &Option<DiagnosticId>,
949-
level: &Level,
950-
max_line_num_len: usize,
951-
is_secondary: bool)
952-
-> io::Result<()> {
948+
fn emit_message_default(
949+
&mut self,
950+
msp: &MultiSpan,
951+
msg: &[(String, Style)],
952+
code: &Option<DiagnosticId>,
953+
level: &Level,
954+
max_line_num_len: usize,
955+
is_secondary: bool,
956+
) -> io::Result<()> {
953957
let mut buffer = StyledBuffer::new();
954958
let header_style = if is_secondary {
955959
Style::HeaderMsg
@@ -1184,11 +1188,12 @@ impl EmitterWriter {
11841188

11851189
}
11861190

1187-
fn emit_suggestion_default(&mut self,
1188-
suggestion: &CodeSuggestion,
1189-
level: &Level,
1190-
max_line_num_len: usize)
1191-
-> io::Result<()> {
1191+
fn emit_suggestion_default(
1192+
&mut self,
1193+
suggestion: &CodeSuggestion,
1194+
level: &Level,
1195+
max_line_num_len: usize,
1196+
) -> io::Result<()> {
11921197
if let Some(ref sm) = self.sm {
11931198
let mut buffer = StyledBuffer::new();
11941199

@@ -1198,11 +1203,13 @@ impl EmitterWriter {
11981203
buffer.append(0, &level_str, Style::Level(level.clone()));
11991204
buffer.append(0, ": ", Style::HeaderMsg);
12001205
}
1201-
self.msg_to_buffer(&mut buffer,
1202-
&[(suggestion.msg.to_owned(), Style::NoStyle)],
1203-
max_line_num_len,
1204-
"suggestion",
1205-
Some(Style::HeaderMsg));
1206+
self.msg_to_buffer(
1207+
&mut buffer,
1208+
&[(suggestion.msg.to_owned(), Style::NoStyle)],
1209+
max_line_num_len,
1210+
"suggestion",
1211+
Some(Style::HeaderMsg),
1212+
);
12061213

12071214
// Render the replacements for each suggestion
12081215
let suggestions = suggestion.splice_lines(&**sm);
@@ -1340,22 +1347,40 @@ impl EmitterWriter {
13401347
if !self.short_message {
13411348
for child in children {
13421349
let span = child.render_span.as_ref().unwrap_or(&child.span);
1343-
match self.emit_message_default(&span,
1344-
&child.styled_message(),
1345-
&None,
1346-
&child.level,
1347-
max_line_num_len,
1348-
true) {
1350+
match self.emit_message_default(
1351+
&span,
1352+
&child.styled_message(),
1353+
&None,
1354+
&child.level,
1355+
max_line_num_len,
1356+
true,
1357+
) {
13491358
Err(e) => panic!("failed to emit error: {}", e),
13501359
_ => ()
13511360
}
13521361
}
13531362
for sugg in suggestions {
1354-
match self.emit_suggestion_default(sugg,
1355-
&Level::Help,
1356-
max_line_num_len) {
1357-
Err(e) => panic!("failed to emit error: {}", e),
1358-
_ => ()
1363+
if sugg.style == SuggestionStyle::HideCodeAlways {
1364+
match self.emit_message_default(
1365+
&MultiSpan::new(),
1366+
&[(sugg.msg.to_owned(), Style::HeaderMsg)],
1367+
&None,
1368+
&Level::Help,
1369+
max_line_num_len,
1370+
true,
1371+
) {
1372+
Err(e) => panic!("failed to emit error: {}", e),
1373+
_ => ()
1374+
}
1375+
} else {
1376+
match self.emit_suggestion_default(
1377+
sugg,
1378+
&Level::Help,
1379+
max_line_num_len,
1380+
) {
1381+
Err(e) => panic!("failed to emit error: {}", e),
1382+
_ => ()
1383+
}
13591384
}
13601385
}
13611386
}

src/librustc_errors/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ pub enum Applicability {
6868
Unspecified,
6969
}
7070

71+
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, RustcEncodable, RustcDecodable)]
72+
pub enum SuggestionStyle {
73+
/// Hide the suggested code when displaying this suggestion inline.
74+
HideCodeInline,
75+
/// Always hide the suggested code.
76+
HideCodeAlways,
77+
/// Always show the suggested code.
78+
/// This will *not* show the code if the suggestion is inline *and* the suggested code is
79+
/// empty.
80+
ShowCode,
81+
}
82+
83+
impl SuggestionStyle {
84+
fn hide_inline(&self) -> bool {
85+
match *self {
86+
SuggestionStyle::HideCodeAlways | SuggestionStyle::HideCodeInline => true,
87+
SuggestionStyle::ShowCode => false,
88+
}
89+
}
90+
}
91+
7192
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
7293
pub struct CodeSuggestion {
7394
/// Each substitute can have multiple variants due to multiple
@@ -93,7 +114,8 @@ pub struct CodeSuggestion {
93114
/// ```
94115
pub substitutions: Vec<Substitution>,
95116
pub msg: String,
96-
pub show_code_when_inline: bool,
117+
/// Visual representation of this suggestion.
118+
pub style: SuggestionStyle,
97119
/// Whether or not the suggestion is approximate
98120
///
99121
/// Sometimes we may show suggestions with placeholders,

0 commit comments

Comments
 (0)