From 73f779640e90ad9efe914e213485652113450eb2 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Sat, 27 Apr 2024 12:13:08 -0600 Subject: [PATCH] refactor: Move Renderer creation to GlobalContext --- src/cargo/util/context/mod.rs | 15 +++++++++++++++ src/cargo/util/lints.rs | 26 ++++---------------------- src/cargo/util/toml/mod.rs | 10 ++-------- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/cargo/util/context/mod.rs b/src/cargo/util/context/mod.rs index 914d57f2138d..9544ef9d9efb 100644 --- a/src/cargo/util/context/mod.rs +++ b/src/cargo/util/context/mod.rs @@ -50,6 +50,7 @@ //! desired type. use crate::util::cache_lock::{CacheLock, CacheLockMode, CacheLocker}; +use annotate_snippets::Renderer; use std::borrow::Cow; use std::cell::{RefCell, RefMut}; use std::collections::hash_map::Entry::{Occupied, Vacant}; @@ -251,6 +252,8 @@ pub struct GlobalContext { /// A cache of modifications to make to [`GlobalContext::global_cache_tracker`], /// saved to disk in a batch to improve performance. deferred_global_last_use: LazyCell>, + /// The renderer to use for outputting diagnostics. + renderer: Renderer, } impl GlobalContext { @@ -281,6 +284,13 @@ impl GlobalContext { _ => true, }; + let renderer = Renderer::styled().term_width( + shell + .err_width() + .diagnostic_terminal_width() + .unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH), + ); + GlobalContext { home_path: Filesystem::new(homedir), shell: RefCell::new(shell), @@ -326,6 +336,7 @@ impl GlobalContext { ws_roots: RefCell::new(HashMap::new()), global_cache_tracker: LazyCell::new(), deferred_global_last_use: LazyCell::new(), + renderer, } } @@ -412,6 +423,10 @@ impl GlobalContext { self.shell.borrow_mut() } + pub fn renderer(&self) -> &Renderer { + &self.renderer + } + /// Gets the path to the `rustdoc` executable. pub fn rustdoc(&self) -> CargoResult<&Path> { self.rustdoc diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index fd462166aa1a..bd91d7c9c192 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,7 @@ 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))?; + writeln!(gctx.shell().err(), "{}", gctx.renderer().render(message))?; } Ok(()) } @@ -367,13 +361,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))?; + writeln!(gctx.shell().err(), "{}", gctx.renderer().render(message))?; } Ok(()) } @@ -476,13 +464,7 @@ 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))?; + writeln!(gctx.shell().err(), "{}", gctx.renderer().render(message))?; } } } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 96720937a922..fe4fd93105bb 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) = writeln!(gctx.shell().err(), "{}", gctx.renderer().render(message)) { return err.into(); } return AlreadyPrintedError::new(e.into()).into();