From 2d98d0fcc23d6101ec2bbb5b74e77ca5fa0f5ab9 Mon Sep 17 00:00:00 2001 From: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> Date: Sat, 21 Jan 2023 14:44:17 +0100 Subject: [PATCH] Extract manual `PWD` extraction with method `current_work_dir` (#7812) --- crates/nu-cli/src/completions/directory_completions.rs | 9 +-------- crates/nu-cli/src/completions/dotnu_completions.rs | 9 +-------- crates/nu-cli/src/completions/file_completions.rs | 9 +-------- crates/nu-protocol/src/engine/engine_state.rs | 6 ++++++ 4 files changed, 9 insertions(+), 24 deletions(-) diff --git a/crates/nu-cli/src/completions/directory_completions.rs b/crates/nu-cli/src/completions/directory_completions.rs index 27a6f80d1c..6ec0888d28 100644 --- a/crates/nu-cli/src/completions/directory_completions.rs +++ b/crates/nu-cli/src/completions/directory_completions.rs @@ -33,14 +33,7 @@ impl Completer for DirectoryCompletion { _: usize, options: &CompletionOptions, ) -> Vec { - let cwd = if let Some(d) = self.engine_state.get_env_var("PWD") { - match d.as_string() { - Ok(s) => s, - Err(_) => "".to_string(), - } - } else { - "".to_string() - }; + let cwd = self.engine_state.current_work_dir(); let partial = String::from_utf8_lossy(&prefix).to_string(); // Filter only the folders diff --git a/crates/nu-cli/src/completions/dotnu_completions.rs b/crates/nu-cli/src/completions/dotnu_completions.rs index ac07ba7d2e..243d07dd9d 100644 --- a/crates/nu-cli/src/completions/dotnu_completions.rs +++ b/crates/nu-cli/src/completions/dotnu_completions.rs @@ -70,14 +70,7 @@ impl Completer for DotNuCompletion { partial = base_dir_partial; } else { // Fetch the current folder - let current_folder = if let Some(d) = self.engine_state.get_env_var("PWD") { - match d.as_string() { - Ok(s) => s, - Err(_) => "".to_string(), - } - } else { - "".to_string() - }; + let current_folder = self.engine_state.current_work_dir(); is_current_folder = true; // Add the current folder and the lib dirs into the diff --git a/crates/nu-cli/src/completions/file_completions.rs b/crates/nu-cli/src/completions/file_completions.rs index 6ee3cf55b2..4a303bde10 100644 --- a/crates/nu-cli/src/completions/file_completions.rs +++ b/crates/nu-cli/src/completions/file_completions.rs @@ -30,14 +30,7 @@ impl Completer for FileCompletion { _: usize, options: &CompletionOptions, ) -> Vec { - let cwd = if let Some(d) = self.engine_state.get_env_var("PWD") { - match d.as_string() { - Ok(s) => s, - Err(_) => "".to_string(), - } - } else { - "".to_string() - }; + let cwd = self.engine_state.current_work_dir(); let prefix = String::from_utf8_lossy(&prefix).to_string(); let output: Vec<_> = file_path_completion(span, &prefix, &cwd, options) .into_iter() diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 59c60f44f3..ac31a66382 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -965,6 +965,12 @@ impl EngineState { self.get_module_comments(module_id) .map(|comment_spans| self.build_usage(comment_spans)) } + + pub fn current_work_dir(&self) -> String { + self.get_env_var("PWD") + .map(|d| d.as_string().unwrap_or_default()) + .unwrap_or_default() + } } /// A temporary extension to the global state. This handles bridging between the global state and the