use crate::CodeSuggestion;
+use crate::SuggestionStyle;
use crate::SubstitutionPart;
use crate::Substitution;
use crate::Applicability;
.collect(),
}],
msg: msg.to_owned(),
- show_code_when_inline: true,
+ style: SuggestionStyle::ShowCode,
applicability,
});
self
}],
}],
msg: msg.to_owned(),
- show_code_when_inline: true,
+ style: SuggestionStyle::ShowCode,
applicability,
});
self
}],
}).collect(),
msg: msg.to_owned(),
- show_code_when_inline: true,
+ style: SuggestionStyle::ShowCode,
applicability,
});
self
}],
}],
msg: msg.to_owned(),
- show_code_when_inline: false,
+ style: SuggestionStyle::HideCodeInline,
applicability: applicability,
});
self
use syntax_pos::{SourceFile, Span, MultiSpan};
-use crate::{Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, SourceMapperDyn, DiagnosticId};
+use crate::{
+ Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic,
+ SuggestionStyle, SourceMapperDyn, DiagnosticId,
+};
use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledString, Style};
use crate::styled_buffer::StyledBuffer;
// don't display multiline suggestions as labels
!sugg.substitutions[0].parts[0].snippet.contains('\n') {
let substitution = &sugg.substitutions[0].parts[0].snippet.trim();
- let msg = if substitution.len() == 0 || !sugg.show_code_when_inline {
+ let msg = if substitution.len() == 0 || sugg.style.hide_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)
}
}
- fn emit_message_default(&mut self,
- msp: &MultiSpan,
- msg: &[(String, Style)],
- code: &Option<DiagnosticId>,
- level: &Level,
- max_line_num_len: usize,
- is_secondary: bool)
- -> io::Result<()> {
+ fn emit_message_default(
+ &mut self,
+ msp: &MultiSpan,
+ msg: &[(String, Style)],
+ code: &Option<DiagnosticId>,
+ level: &Level,
+ max_line_num_len: usize,
+ is_secondary: bool,
+ ) -> io::Result<()> {
let mut buffer = StyledBuffer::new();
let header_style = if is_secondary {
Style::HeaderMsg
}
- fn emit_suggestion_default(&mut self,
- suggestion: &CodeSuggestion,
- level: &Level,
- max_line_num_len: usize)
- -> io::Result<()> {
+ fn emit_suggestion_default(
+ &mut self,
+ suggestion: &CodeSuggestion,
+ level: &Level,
+ max_line_num_len: usize,
+ ) -> io::Result<()> {
if let Some(ref sm) = self.sm {
let mut buffer = StyledBuffer::new();
buffer.append(0, &level_str, 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));
+ self.msg_to_buffer(
+ &mut buffer,
+ &[(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(&**sm);
if !self.short_message {
for child in children {
let span = child.render_span.as_ref().unwrap_or(&child.span);
- match self.emit_message_default(&span,
- &child.styled_message(),
- &None,
- &child.level,
- max_line_num_len,
- true) {
+ match self.emit_message_default(
+ &span,
+ &child.styled_message(),
+ &None,
+ &child.level,
+ max_line_num_len,
+ true,
+ ) {
Err(e) => panic!("failed to emit error: {}", e),
_ => ()
}
}
for sugg in suggestions {
- match self.emit_suggestion_default(sugg,
- &Level::Help,
- max_line_num_len) {
- Err(e) => panic!("failed to emit error: {}", e),
- _ => ()
+ if sugg.style == SuggestionStyle::HideCodeAlways {
+ match self.emit_message_default(
+ &MultiSpan::new(),
+ &[(sugg.msg.to_owned(), Style::HeaderMsg)],
+ &None,
+ &Level::Help,
+ max_line_num_len,
+ true,
+ ) {
+ Err(e) => panic!("failed to emit error: {}", e),
+ _ => ()
+ }
+ } else {
+ match self.emit_suggestion_default(
+ sugg,
+ &Level::Help,
+ max_line_num_len,
+ ) {
+ Err(e) => panic!("failed to emit error: {}", e),
+ _ => ()
+ }
}
}
}
Unspecified,
}
+#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, RustcEncodable, RustcDecodable)]
+pub enum SuggestionStyle {
+ /// Hide the suggested code when displaying this suggestion inline.
+ HideCodeInline,
+ /// Always hide the suggested code.
+ HideCodeAlways,
+ /// Always show the suggested code.
+ /// This will *not* show the code if the suggestion is inline *and* the suggested code is
+ /// empty.
+ ShowCode,
+}
+
+impl SuggestionStyle {
+ fn hide_inline(&self) -> bool {
+ match *self {
+ SuggestionStyle::HideCodeAlways | SuggestionStyle::HideCodeInline => true,
+ SuggestionStyle::ShowCode => false,
+ }
+ }
+}
+
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
pub struct CodeSuggestion {
/// Each substitute can have multiple variants due to multiple
/// ```
pub substitutions: Vec<Substitution>,
pub msg: String,
- pub show_code_when_inline: bool,
+ /// Visual representation of this suggestion.
+ pub style: SuggestionStyle,
/// Whether or not the suggestion is approximate
///
/// Sometimes we may show suggestions with placeholders,