Use StringPart
more.
This commit is contained in:
parent
0621cd46f2
commit
be64802854
3 changed files with 38 additions and 43 deletions
|
@ -165,10 +165,10 @@ impl DiagnosticStyledString {
|
|||
DiagnosticStyledString(vec![])
|
||||
}
|
||||
pub fn push_normal<S: Into<String>>(&mut self, t: S) {
|
||||
self.0.push(StringPart::normal(t.into()));
|
||||
self.0.push(StringPart::normal(t));
|
||||
}
|
||||
pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
|
||||
self.0.push(StringPart::highlighted(t.into()));
|
||||
self.0.push(StringPart::highlighted(t));
|
||||
}
|
||||
pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
|
||||
if highlight {
|
||||
|
@ -178,11 +178,11 @@ impl DiagnosticStyledString {
|
|||
}
|
||||
}
|
||||
pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
|
||||
DiagnosticStyledString(vec![StringPart::normal(t.into())])
|
||||
DiagnosticStyledString(vec![StringPart::normal(t)])
|
||||
}
|
||||
|
||||
pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
|
||||
DiagnosticStyledString(vec![StringPart::highlighted(t.into())])
|
||||
DiagnosticStyledString(vec![StringPart::highlighted(t)])
|
||||
}
|
||||
|
||||
pub fn content(&self) -> String {
|
||||
|
@ -197,12 +197,12 @@ pub struct StringPart {
|
|||
}
|
||||
|
||||
impl StringPart {
|
||||
fn normal(content: String) -> StringPart {
|
||||
StringPart { content, style: Style::NoStyle }
|
||||
pub fn normal<S: Into<String>>(content: S) -> StringPart {
|
||||
StringPart { content: content.into(), style: Style::NoStyle }
|
||||
}
|
||||
|
||||
fn highlighted(content: String) -> StringPart {
|
||||
StringPart { content, style: Style::Highlight }
|
||||
pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
|
||||
StringPart { content: content.into(), style: Style::Highlight }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -391,13 +391,16 @@ impl Diagnostic {
|
|||
} else {
|
||||
(0, found_label.len() - expected_label.len())
|
||||
};
|
||||
let mut msg: Vec<_> =
|
||||
vec![(format!("{}{} `", " ".repeat(expected_padding), expected_label), Style::NoStyle)];
|
||||
msg.extend(expected.0.into_iter().map(|p| (p.content, p.style)));
|
||||
msg.push((format!("`{expected_extra}\n"), Style::NoStyle));
|
||||
msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle));
|
||||
msg.extend(found.0.into_iter().map(|p| (p.content, p.style)));
|
||||
msg.push((format!("`{found_extra}"), Style::NoStyle));
|
||||
let mut msg = vec![StringPart::normal(format!(
|
||||
"{}{} `",
|
||||
" ".repeat(expected_padding),
|
||||
expected_label
|
||||
))];
|
||||
msg.extend(expected.0.into_iter());
|
||||
msg.push(StringPart::normal(format!("`{expected_extra}\n")));
|
||||
msg.push(StringPart::normal(format!("{}{} `", " ".repeat(found_padding), found_label)));
|
||||
msg.extend(found.0.into_iter());
|
||||
msg.push(StringPart::normal(format!("`{found_extra}")));
|
||||
|
||||
// For now, just attach these as notes.
|
||||
self.highlighted_note(msg);
|
||||
|
@ -406,9 +409,9 @@ impl Diagnostic {
|
|||
|
||||
pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
|
||||
self.highlighted_note(vec![
|
||||
(format!("`{name}` from trait: `"), Style::NoStyle),
|
||||
(signature, Style::Highlight),
|
||||
("`".to_string(), Style::NoStyle),
|
||||
StringPart::normal(format!("`{name}` from trait: `")),
|
||||
StringPart::highlighted(signature),
|
||||
StringPart::normal("`"),
|
||||
]);
|
||||
self
|
||||
}
|
||||
|
@ -420,10 +423,7 @@ impl Diagnostic {
|
|||
self
|
||||
}
|
||||
|
||||
fn highlighted_note<M: Into<SubdiagnosticMessage>>(
|
||||
&mut self,
|
||||
msg: Vec<(M, Style)>,
|
||||
) -> &mut Self {
|
||||
fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
|
||||
self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
|
||||
self
|
||||
}
|
||||
|
@ -492,7 +492,7 @@ impl Diagnostic {
|
|||
}
|
||||
|
||||
/// Add a help message attached to this diagnostic with a customizable highlighted message.
|
||||
pub fn highlighted_help(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
|
||||
pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
|
||||
self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
|
||||
self
|
||||
}
|
||||
|
@ -941,15 +941,10 @@ impl Diagnostic {
|
|||
|
||||
/// Convenience function for internal use, clients should use one of the
|
||||
/// public methods above.
|
||||
fn sub_with_highlights<M: Into<SubdiagnosticMessage>>(
|
||||
&mut self,
|
||||
level: Level,
|
||||
messages: Vec<(M, Style)>,
|
||||
span: MultiSpan,
|
||||
) {
|
||||
fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
|
||||
let messages = messages
|
||||
.into_iter()
|
||||
.map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.0), m.1))
|
||||
.map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.content), m.style))
|
||||
.collect();
|
||||
let sub = SubDiagnostic { level, messages, span };
|
||||
self.children.push(sub);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue