Add way to hide suggestion snippet window from cli output

This commit is contained in:
Esteban Küber 2019-02-08 02:45:53 -08:00
parent 57d7cfc3cf
commit 05b4e7c8a9
3 changed files with 84 additions and 36 deletions

View File

@ -1,4 +1,5 @@
use crate::CodeSuggestion;
use crate::SuggestionStyle;
use crate::SubstitutionPart;
use crate::Substitution;
use crate::Applicability;
@ -243,7 +244,7 @@ pub fn multipart_suggestion(
.collect(),
}],
msg: msg.to_owned(),
show_code_when_inline: true,
style: SuggestionStyle::ShowCode,
applicability,
});
self
@ -277,7 +278,7 @@ pub fn span_suggestion(&mut self, sp: Span, msg: &str,
}],
}],
msg: msg.to_owned(),
show_code_when_inline: true,
style: SuggestionStyle::ShowCode,
applicability,
});
self
@ -295,7 +296,7 @@ pub fn span_suggestions(&mut self, sp: Span, msg: &str,
}],
}).collect(),
msg: msg.to_owned(),
show_code_when_inline: true,
style: SuggestionStyle::ShowCode,
applicability,
});
self
@ -316,7 +317,7 @@ pub fn span_suggestion_short(
}],
}],
msg: msg.to_owned(),
show_code_when_inline: false,
style: SuggestionStyle::HideCodeInline,
applicability: applicability,
});
self

View File

@ -2,7 +2,10 @@
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;
@ -45,7 +48,7 @@ fn emit(&mut self, db: &DiagnosticBuilder<'_>) {
// 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)
@ -942,14 +945,15 @@ fn style_or_override(style: Style, override_style: Option<Style>) -> Style {
}
}
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
@ -1184,11 +1188,12 @@ fn emit_message_default(&mut self,
}
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();
@ -1198,11 +1203,13 @@ fn emit_suggestion_default(&mut self,
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);
@ -1340,22 +1347,40 @@ fn emit_messages_default(&mut self,
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),
_ => ()
}
}
}
}

View File

@ -68,6 +68,27 @@ pub enum Applicability {
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
@ -93,7 +114,8 @@ pub struct CodeSuggestion {
/// ```
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,