From 14601688a887cd3f4cf46765ea81be09af9fd7ca Mon Sep 17 00:00:00 2001 From: reuben olinsky Date: Tue, 23 Jul 2024 07:59:14 -0700 Subject: [PATCH] fix: relative path completion fixes --- brush-core/src/completion.rs | 2 +- brush-core/src/shell.rs | 2 +- brush-interactive/src/interactive_shell.rs | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/brush-core/src/completion.rs b/brush-core/src/completion.rs index 831bf20d..6653d039 100644 --- a/brush-core/src/completion.rs +++ b/brush-core/src/completion.rs @@ -853,7 +853,7 @@ impl Config { fn get_file_completions(shell: &Shell, context: &Context, must_be_dir: bool) -> Vec { let glob = std::format!("{}*", context.token_to_complete); - let path_filter = |path: &Path| !must_be_dir || path.is_dir(); + let path_filter = |path: &Path| !must_be_dir || shell.get_absolute_path(path).is_dir(); // TODO: Pass through quoting. patterns::Pattern::from(glob) diff --git a/brush-core/src/shell.rs b/brush-core/src/shell.rs index fa7c04a7..33517a76 100644 --- a/brush-core/src/shell.rs +++ b/brush-core/src/shell.rs @@ -886,7 +886,7 @@ impl Shell { /// # Arguments /// /// * `path` - The path to get the absolute form of. - pub(crate) fn get_absolute_path(&self, path: &Path) -> PathBuf { + pub fn get_absolute_path(&self, path: &Path) -> PathBuf { if path.is_absolute() { path.to_owned() } else { diff --git a/brush-interactive/src/interactive_shell.rs b/brush-interactive/src/interactive_shell.rs index 8b1eac68..0a2bcd91 100644 --- a/brush-interactive/src/interactive_shell.rs +++ b/brush-interactive/src/interactive_shell.rs @@ -236,6 +236,8 @@ impl EditorHelper { line: &str, pos: usize, ) -> rustyline::Result<(usize, Vec)> { + let working_dir = self.shell.working_dir.clone(); + // Intentionally ignore any errors that arise. let completion_future = self.shell.get_completions(line, pos); tokio::pin!(completion_future); @@ -261,9 +263,17 @@ impl EditorHelper { if completions.options.treat_as_filenames { for candidate in &mut completions.candidates { // Check if it's a directory. - if !candidate.ends_with(std::path::MAIN_SEPARATOR) && Path::new(candidate).is_dir() - { - candidate.push(std::path::MAIN_SEPARATOR); + if !candidate.ends_with(std::path::MAIN_SEPARATOR) { + let candidate_path = Path::new(candidate); + let abs_candidate_path = if candidate_path.is_absolute() { + PathBuf::from(candidate_path) + } else { + working_dir.join(candidate_path) + }; + + if abs_candidate_path.is_dir() { + candidate.push(std::path::MAIN_SEPARATOR); + } } } }