Skip to content

Commit

Permalink
Re-export which from helix-stdx::env
Browse files Browse the repository at this point in the history
We use `which::which` in many crates, so `which` was a separate
dependency across all of them. We can centralize `which` into the
stdx crate so it's easy for all crates to depend on it.

I also moved the rest of `helix-view/src/env.rs` into helix-stdx's
`env` module since it only contained a thin wrapper around `which`
and `std::env`.
  • Loading branch information
the-mikedavis authored and archseer committed Jan 24, 2024
1 parent 6bfe1dd commit 6d724a8
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 39 deletions.
23 changes: 10 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion helix-dap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ homepage.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
helix-stdx = { path = "../helix-stdx" }
helix-core = { path = "../helix-core" }

anyhow = "1.0"
Expand All @@ -21,7 +22,6 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "net", "sync"] }
which = "5.0.0"

[dev-dependencies]
fern = "0.6"
2 changes: 1 addition & 1 deletion helix-dap/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Client {
id: usize,
) -> Result<(Self, UnboundedReceiver<Payload>)> {
// Resolve path to the binary
let cmd = which::which(cmd).map_err(|err| anyhow::anyhow!(err))?;
let cmd = helix_stdx::env::which(cmd).map_err(|err| anyhow::anyhow!(err))?;

let process = Command::new(cmd)
.args(args)
Expand Down
1 change: 0 additions & 1 deletion helix-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ etcetera = "0.8"
tree-sitter.workspace = true
once_cell = "1.19"
log = "0.4"
which = "5.0.0"

# TODO: these two should be on !wasm32 only

Expand Down
2 changes: 1 addition & 1 deletion helix-loader/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn get_language(name: &str) -> Result<Language> {
}

fn ensure_git_is_available() -> Result<()> {
match which::which("git") {
match helix_stdx::env::which("git") {
Ok(_cmd) => Ok(()),
Err(err) => Err(anyhow::anyhow!("'git' could not be found ({err})")),
}
Expand Down
1 change: 0 additions & 1 deletion helix-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ serde_json = "1.0"
thiserror = "1.0"
tokio = { version = "1.35", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] }
tokio-stream = "0.1.14"
which = "5.0.0"
parking_lot = "0.12.1"
2 changes: 1 addition & 1 deletion helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Client {
doc_path: Option<&std::path::PathBuf>,
) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> {
// Resolve path to the binary
let cmd = which::which(cmd).map_err(|err| anyhow::anyhow!(err))?;
let cmd = helix_stdx::env::which(cmd).map_err(|err| anyhow::anyhow!(err))?;

let process = Command::new(cmd)
.envs(server_environment)
Expand Down
1 change: 1 addition & 0 deletions helix-stdx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ homepage.workspace = true
dunce = "1.0"
etcetera = "0.8"
ropey = { version = "1.6.1", default-features = false }
which = "6.0"

[dev-dependencies]
tempfile = "3.9"
10 changes: 10 additions & 0 deletions helix-stdx/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub use which::which;

use std::{
path::{Path, PathBuf},
sync::RwLock,
Expand Down Expand Up @@ -30,6 +32,14 @@ pub fn set_current_working_dir(path: impl AsRef<Path>) -> std::io::Result<()> {
Ok(())
}

pub fn env_var_is_set(env_var_name: &str) -> bool {
std::env::var_os(env_var_name).is_some()
}

pub fn binary_exists(binary_name: &str) -> bool {
which::which(binary_name).is_ok()
}

#[cfg(test)]
mod tests {
use super::{current_working_dir, set_current_working_dir};
Expand Down
2 changes: 0 additions & 2 deletions helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ helix-loader = { path = "../helix-loader" }
anyhow = "1"
once_cell = "1.19"

which = "5.0.0"

tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] }
tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] }
crossterm = { version = "0.27", features = ["event-stream"] }
Expand Down
6 changes: 3 additions & 3 deletions helix-term/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub fn languages_all() -> std::io::Result<()> {
.sort_unstable_by_key(|l| l.language_id.clone());

let check_binary = |cmd: Option<&str>| match cmd {
Some(cmd) => match which::which(cmd) {
Some(cmd) => match helix_stdx::env::which(cmd) {
Ok(_) => column(&format!("✓ {}", cmd), Color::Green),
Err(_) => column(&format!("✘ {}", cmd), Color::Red),
},
Expand Down Expand Up @@ -322,7 +322,7 @@ fn probe_protocols<'a, I: Iterator<Item = &'a str> + 'a>(
writeln!(stdout)?;

for cmd in server_cmds {
let (path, icon) = match which::which(cmd) {
let (path, icon) = match helix_stdx::env::which(cmd) {
Ok(path) => (path.display().to_string().green(), "✓".green()),
Err(_) => (format!("'{}' not found in $PATH", cmd).red(), "✘".red()),
};
Expand All @@ -344,7 +344,7 @@ fn probe_protocol(protocol_name: &str, server_cmd: Option<String>) -> std::io::R
writeln!(stdout, "Configured {}: {}", protocol_name, cmd_name)?;

if let Some(cmd) = server_cmd {
let path = match which::which(&cmd) {
let path = match helix_stdx::env::which(&cmd) {
Ok(path) => path.display().to_string().green(),
Err(_) => format!("'{}' not found in $PATH", cmd).red(),
};
Expand Down
1 change: 0 additions & 1 deletion helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ serde_json = "1.0"
toml = "0.7"
log = "~0.4"

which = "5.0.0"
parking_lot = "0.12.1"


Expand Down
4 changes: 2 additions & 2 deletions helix-view/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {

#[cfg(target_os = "macos")]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
use crate::env::{binary_exists, env_var_is_set};
use helix_stdx::env::{binary_exists, env_var_is_set};

if env_var_is_set("TMUX") && binary_exists("tmux") {
command_provider! {
Expand All @@ -98,7 +98,7 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {

#[cfg(not(any(windows, target_os = "wasm32", target_os = "macos")))]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
use crate::env::{binary_exists, env_var_is_set};
use helix_stdx::env::{binary_exists, env_var_is_set};
use provider::command::is_exit_success;
// TODO: support for user-defined provider, probably when we have plugin support by setting a
// variable?
Expand Down
7 changes: 6 additions & 1 deletion helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,12 @@ impl Document {
if let Some((fmt_cmd, fmt_args)) = self
.language_config()
.and_then(|c| c.formatter.as_ref())
.and_then(|formatter| Some((which::which(&formatter.command).ok()?, &formatter.args)))
.and_then(|formatter| {
Some((
helix_stdx::env::which(&formatter.command).ok()?,
&formatter.args,
))
})
{
use std::process::Stdio;
let text = self.text().clone();
Expand Down
4 changes: 2 additions & 2 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ pub struct TerminalConfig {

#[cfg(windows)]
pub fn get_terminal_provider() -> Option<TerminalConfig> {
use crate::env::binary_exists;
use helix_stdx::env::binary_exists;

if binary_exists("wt") {
return Some(TerminalConfig {
Expand All @@ -352,7 +352,7 @@ pub fn get_terminal_provider() -> Option<TerminalConfig> {

#[cfg(not(any(windows, target_os = "wasm32")))]
pub fn get_terminal_provider() -> Option<TerminalConfig> {
use crate::env::{binary_exists, env_var_is_set};
use helix_stdx::env::{binary_exists, env_var_is_set};

if env_var_is_set("TMUX") && binary_exists("tmux") {
return Some(TerminalConfig {
Expand Down
8 changes: 0 additions & 8 deletions helix-view/src/env.rs

This file was deleted.

1 change: 0 additions & 1 deletion helix-view/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub mod base64;
pub mod clipboard;
pub mod document;
pub mod editor;
pub mod env;
pub mod events;
pub mod graphics;
pub mod gutter;
Expand Down

0 comments on commit 6d724a8

Please sign in to comment.