@@ -2,7 +2,10 @@ use Destination::*;
2
2
3
3
use syntax_pos::{SourceFile, Span, MultiSpan};
4
4
5
- use crate::{Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, SourceMapperDyn, DiagnosticId};
5
+ use crate::{
6
+ Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic,
7
+ SuggestionStyle, SourceMapperDyn, DiagnosticId,
8
+ };
6
9
use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledString, Style};
7
10
use crate::styled_buffer::StyledBuffer;
8
11
@@ -43,9 +46,14 @@ impl Emitter for EmitterWriter {
43
46
// don't display long messages as labels
44
47
sugg.msg.split_whitespace().count() < 10 &&
45
48
// don't display multiline suggestions as labels
46
- !sugg.substitutions[0].parts[0].snippet.contains('\n') {
49
+ !sugg.substitutions[0].parts[0].snippet.contains('\n') &&
50
+ // when this style is set we want the suggestion to be a message, not inline
51
+ sugg.style != SuggestionStyle::HideCodeAlways &&
52
+ // trivial suggestion for tooling's sake, never shown
53
+ sugg.style != SuggestionStyle::CompletelyHidden
54
+ {
47
55
let substitution = &sugg.substitutions[0].parts[0].snippet.trim();
48
- let msg = if substitution.len() == 0 || ! sugg.show_code_when_inline {
56
+ let msg = if substitution.len() == 0 || sugg.style.hide_inline() {
49
57
// This substitution is only removal or we explicitly don't want to show the
50
58
// code inline, don't show it
51
59
format!("help: {}", sugg.msg)
@@ -942,14 +950,15 @@ impl EmitterWriter {
942
950
}
943
951
}
944
952
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<()> {
953
+ fn emit_message_default(
954
+ &mut self,
955
+ msp: &MultiSpan,
956
+ msg: &[(String, Style)],
957
+ code: &Option<DiagnosticId>,
958
+ level: &Level,
959
+ max_line_num_len: usize,
960
+ is_secondary: bool,
961
+ ) -> io::Result<()> {
953
962
let mut buffer = StyledBuffer::new();
954
963
let header_style = if is_secondary {
955
964
Style::HeaderMsg
@@ -1184,11 +1193,12 @@ impl EmitterWriter {
1184
1193
1185
1194
}
1186
1195
1187
- fn emit_suggestion_default(&mut self,
1188
- suggestion: &CodeSuggestion,
1189
- level: &Level,
1190
- max_line_num_len: usize)
1191
- -> io::Result<()> {
1196
+ fn emit_suggestion_default(
1197
+ &mut self,
1198
+ suggestion: &CodeSuggestion,
1199
+ level: &Level,
1200
+ max_line_num_len: usize,
1201
+ ) -> io::Result<()> {
1192
1202
if let Some(ref sm) = self.sm {
1193
1203
let mut buffer = StyledBuffer::new();
1194
1204
@@ -1198,11 +1208,13 @@ impl EmitterWriter {
1198
1208
buffer.append(0, &level_str, Style::Level(level.clone()));
1199
1209
buffer.append(0, ": ", Style::HeaderMsg);
1200
1210
}
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));
1211
+ self.msg_to_buffer(
1212
+ &mut buffer,
1213
+ &[(suggestion.msg.to_owned(), Style::NoStyle)],
1214
+ max_line_num_len,
1215
+ "suggestion",
1216
+ Some(Style::HeaderMsg),
1217
+ );
1206
1218
1207
1219
// Render the replacements for each suggestion
1208
1220
let suggestions = suggestion.splice_lines(&**sm);
@@ -1340,22 +1352,42 @@ impl EmitterWriter {
1340
1352
if !self.short_message {
1341
1353
for child in children {
1342
1354
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) {
1355
+ match self.emit_message_default(
1356
+ &span,
1357
+ &child.styled_message(),
1358
+ &None,
1359
+ &child.level,
1360
+ max_line_num_len,
1361
+ true,
1362
+ ) {
1349
1363
Err(e) => panic!("failed to emit error: {}", e),
1350
1364
_ => ()
1351
1365
}
1352
1366
}
1353
1367
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
- _ => ()
1368
+ if sugg.style == SuggestionStyle::CompletelyHidden {
1369
+ // do not display this suggestion, it is meant only for tools
1370
+ } else if sugg.style == SuggestionStyle::HideCodeAlways {
1371
+ match self.emit_message_default(
1372
+ &MultiSpan::new(),
1373
+ &[(sugg.msg.to_owned(), Style::HeaderMsg)],
1374
+ &None,
1375
+ &Level::Help,
1376
+ max_line_num_len,
1377
+ true,
1378
+ ) {
1379
+ Err(e) => panic!("failed to emit error: {}", e),
1380
+ _ => ()
1381
+ }
1382
+ } else {
1383
+ match self.emit_suggestion_default(
1384
+ sugg,
1385
+ &Level::Help,
1386
+ max_line_num_len,
1387
+ ) {
1388
+ Err(e) => panic!("failed to emit error: {}", e),
1389
+ _ => ()
1390
+ }
1359
1391
}
1360
1392
}
1361
1393
}
0 commit comments