Skip to content

Commit

Permalink
refactor: Move diagnostic printing to Shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Apr 30, 2024
1 parent ba6fb40 commit 5415b04
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
14 changes: 14 additions & 0 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt;
use std::io::prelude::*;
use std::io::IsTerminal;

use annotate_snippets::{Message, Renderer};
use anstream::AutoStream;
use anstyle::Style;

Expand Down Expand Up @@ -391,6 +392,19 @@ impl Shell {
drop(writeln!(self.out(), "{}", encoded));
Ok(())
}

/// Prints the passed in [Message] to stderr
pub fn print_message(&mut self, message: Message<'_>) -> std::io::Result<()> {
let term_width = self
.err_width()
.diagnostic_terminal_width()
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH);
writeln!(
self.err(),
"{}",
Renderer::styled().term_width(term_width).render(message)
)
}
}

impl Default for Shell {
Expand Down
28 changes: 6 additions & 22 deletions src/cargo/util/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::core::FeatureValue::Dep;
use crate::core::{Edition, FeatureValue, Package};
use crate::util::interning::InternedString;
use crate::{CargoResult, GlobalContext};
use annotate_snippets::{Level, Renderer, Snippet};
use annotate_snippets::{Level, Snippet};
use cargo_util_schemas::manifest::{TomlLintLevel, TomlToolLints};
use pathdiff::diff_paths;
use std::collections::HashSet;
Expand Down Expand Up @@ -270,13 +270,8 @@ pub fn check_im_a_teapot(
.fold(true),
)
.footer(Level::Note.title(&emitted_reason));
let renderer = Renderer::styled().term_width(
gctx.shell()
.err_width()
.diagnostic_terminal_width()
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH),
);
writeln!(gctx.shell().err(), "{}", renderer.render(message))?;

gctx.shell().print_message(message)?;
}
Ok(())
}
Expand Down Expand Up @@ -367,13 +362,7 @@ pub fn check_implicit_features(
));
message = message.footer(Level::Note.title(emitted_source.as_ref().unwrap()));
}
let renderer = Renderer::styled().term_width(
gctx.shell()
.err_width()
.diagnostic_terminal_width()
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH),
);
writeln!(gctx.shell().err(), "{}", renderer.render(message))?;
gctx.shell().print_message(message)?;
}
Ok(())
}
Expand Down Expand Up @@ -476,13 +465,8 @@ pub fn unused_dependencies(
"remove the dependency or activate it in a feature with `dep:{name}`"
);
message = message.footer(Level::Help.title(&help));
let renderer = Renderer::styled().term_width(
gctx.shell()
.err_width()
.diagnostic_terminal_width()
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH),
);
writeln!(gctx.shell().err(), "{}", renderer.render(message))?;

gctx.shell().print_message(message)?;
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use annotate_snippets::{Level, Renderer, Snippet};
use annotate_snippets::{Level, Snippet};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -2343,13 +2343,7 @@ fn emit_diagnostic(
.fold(true)
.annotation(Level::Error.span(span)),
);
let renderer = Renderer::styled().term_width(
gctx.shell()
.err_width()
.diagnostic_terminal_width()
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH),
);
if let Err(err) = writeln!(gctx.shell().err(), "{}", renderer.render(message)) {
if let Err(err) = gctx.shell().print_message(message) {
return err.into();
}
return AlreadyPrintedError::new(e.into()).into();
Expand Down

0 comments on commit 5415b04

Please sign in to comment.