Skip to content

Commit b6d83f9

Browse files
committed
refactor: String -> PathBuf
1 parent 006e39d commit b6d83f9

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

crates/nu-cli/src/completions/dotnu_completions.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use nu_protocol::{
44
Span,
55
};
66
use reedline::Suggestion;
7-
use std::path::{is_separator, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR};
7+
use std::path::{is_separator, PathBuf, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR};
88

99
use super::SemanticSuggestion;
1010

@@ -29,7 +29,7 @@ impl Completer for DotNuCompletion {
2929
options: &CompletionOptions,
3030
) -> Vec<SemanticSuggestion> {
3131
let prefix_str = String::from_utf8_lossy(prefix).replace('`', "");
32-
let mut search_dirs: Vec<String> = vec![];
32+
let mut search_dirs: Vec<PathBuf> = vec![];
3333

3434
// If prefix_str is only a word we want to search in the current dir
3535
let (base, partial) = prefix_str
@@ -38,7 +38,7 @@ impl Completer for DotNuCompletion {
3838
let base_dir = base.replace(is_separator, MAIN_SEPARATOR_STR);
3939

4040
// Fetch the lib dirs
41-
let lib_dirs: Vec<String> = working_set
41+
let lib_dirs: Vec<PathBuf> = working_set
4242
.find_variable(b"$NU_LIB_DIRS")
4343
.and_then(|vid| working_set.get_variable(vid).const_val.as_ref())
4444
.or(working_set.get_env_var("NU_LIB_DIRS"))
@@ -52,33 +52,27 @@ impl Completer for DotNuCompletion {
5252
.expect("internal error: failed to convert lib path")
5353
})
5454
})
55-
.map(|it| {
56-
it.into_os_string()
57-
.into_string()
58-
.expect("internal error: failed to convert OS path")
59-
})
6055
.collect()
6156
})
6257
.unwrap_or_default();
6358

6459
// Check if the base_dir is a folder
6560
// rsplit_once removes the separator
66-
let cwd = working_set
67-
.permanent_state
68-
.cwd(None)
69-
.ok()
70-
.map(|pb| pb.to_string_lossy().into_owned())
71-
.unwrap_or_default();
61+
let cwd = working_set.permanent_state.cwd(None);
7262
if base_dir != "." {
7363
// Search in base_dir as well as lib_dirs
74-
search_dirs.push(format!("{cwd}{SEP}{base_dir}"));
75-
search_dirs.extend(
76-
lib_dirs
77-
.into_iter()
78-
.map(|dir| format!("{dir}{SEP}{base_dir}")),
79-
);
64+
if let Ok(mut cwd) = cwd {
65+
cwd.push(&base_dir);
66+
search_dirs.push(cwd.into_std_path_buf());
67+
}
68+
search_dirs.extend(lib_dirs.into_iter().map(|mut dir| {
69+
dir.push(&base_dir);
70+
dir
71+
}));
8072
} else {
81-
search_dirs.push(cwd);
73+
if let Ok(cwd) = cwd {
74+
search_dirs.push(cwd.into_std_path_buf());
75+
}
8276
search_dirs.extend(lib_dirs);
8377
}
8478

@@ -88,7 +82,10 @@ impl Completer for DotNuCompletion {
8882
let completions = file_path_completion(
8983
span,
9084
partial,
91-
&search_dirs.iter().map(|d| d.as_str()).collect::<Vec<_>>(),
85+
&search_dirs
86+
.iter()
87+
.map(|d| d.to_str().unwrap_or_default())
88+
.collect::<Vec<_>>(),
9289
options,
9390
working_set.permanent_state,
9491
stack,

0 commit comments

Comments
 (0)