From 6fb7a9bbd16a8ff450e4051b0eb80e91afbdb6f2 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Sat, 27 Apr 2024 12:13:08 -0600 Subject: [PATCH] refactor: Move diagnostic printing to Shell --- src/cargo/core/shell.rs | 14 ++++++++++++++ src/cargo/util/lints.rs | 28 ++++++---------------------- src/cargo/util/toml/mod.rs | 10 ++-------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index ff4bb3d2d9ef..257bb042aa31 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -1,3 +1,4 @@ +use annotate_snippets::{Message, Renderer}; use std::fmt; use std::io::prelude::*; use std::io::IsTerminal; @@ -391,6 +392,19 @@ impl Shell { drop(writeln!(self.out(), "{}", encoded)); Ok(()) } + + /// Prints the passed in [Message] to stderr + pub fn emit_diagnostic(&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 { diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index fd462166aa1a..9a06be9d2670 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -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; @@ -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().emit_diagnostic(message)?; } Ok(()) } @@ -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().emit_diagnostic(message)?; } Ok(()) } @@ -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().emit_diagnostic(message)?; } } } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 96720937a922..2887a59ab904 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -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}; @@ -2309,13 +2309,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().emit_diagnostic(message) { return err.into(); } return AlreadyPrintedError::new(e.into()).into();