Skip to content

Commit

Permalink
refactor: Move Renderer creation to GlobalContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Apr 27, 2024
1 parent 9d8b1a3 commit 73f7796
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
15 changes: 15 additions & 0 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<RefCell<DeferredGlobalLastUse>>,
/// The renderer to use for outputting diagnostics.
renderer: Renderer,
}

impl GlobalContext {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -326,6 +336,7 @@ impl GlobalContext {
ws_roots: RefCell::new(HashMap::new()),
global_cache_tracker: LazyCell::new(),
deferred_global_last_use: LazyCell::new(),
renderer,
}
}

Expand Down Expand Up @@ -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
Expand Down
26 changes: 4 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,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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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))?;
}
}
}
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 @@ -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();
Expand Down

0 comments on commit 73f7796

Please sign in to comment.