Skip to content

Commit 81203e1

Browse files
committed
Move fix to DotNuCompletion
1 parent 11e74b8 commit 81203e1

File tree

3 files changed

+56
-84
lines changed

3 files changed

+56
-84
lines changed

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

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -322,46 +322,16 @@ impl NuCompleter {
322322
|| prev_expr_str == b"overlay use"
323323
|| prev_expr_str == b"source-env"
324324
{
325-
// Apart from default completer, which searches for .nu files from NU_LIB_DIRS,
326-
// we also support the case where .nu files are in other folders, like Python
327-
// virtual environments.
328-
let mut default_completer = DotNuCompletion::new();
329-
let default_suggestions = self.process_completion(
330-
&mut default_completer,
331-
&working_set,
332-
prefix,
333-
new_span,
334-
fake_offset,
335-
pos,
336-
);
337-
if !default_suggestions.is_empty() || prefix.len() < 2 {
338-
return default_suggestions;
339-
}
325+
let mut completer = DotNuCompletion::new();
340326

341-
// When user types the beginning of a path, sometimes DotNuCompletion doesn't give a result.
342-
// We will search subdirectories and files, then. We only switch to these provider if user
343-
// has typed at least 2 characters for the path.
344-
let mut file_completer =
345-
FileCompletion::new_with_suffix(".nu".to_string());
346-
let mut file_suggestions = self.process_completion(
347-
&mut file_completer,
348-
&working_set,
349-
prefix,
350-
new_span,
351-
fake_offset,
352-
pos,
353-
);
354-
let mut dir_completer = DirectoryCompletion::new();
355-
let mut dir_suggestions = self.process_completion(
356-
&mut dir_completer,
327+
return self.process_completion(
328+
&mut completer,
357329
&working_set,
358330
prefix,
359331
new_span,
360332
fake_offset,
361333
pos,
362334
);
363-
file_suggestions.append(&mut dir_suggestions);
364-
return file_suggestions;
365335
} else if prev_expr_str == b"ls" {
366336
let mut completer = FileCompletion::new();
367337

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,19 @@ impl Completer for DotNuCompletion {
3535
let prefix_str = prefix_str.replace('`', "");
3636
let mut search_dirs: Vec<PathBuf> = vec![];
3737

38-
// If prefix_str is only a word we want to search in the current dir
39-
let (base, partial) = prefix_str
40-
.rsplit_once(is_separator)
41-
.unwrap_or((".", &prefix_str));
38+
let (base, partial) = if prefix_str == "~" {
39+
("~", "")
40+
} else if let Some((parent, remain)) = prefix_str.split_once(is_separator) {
41+
// If prefix_str is only a word we want to search in the current dir.
42+
// "/xx" should be split to "/" and "xx".
43+
if parent.is_empty() {
44+
("/", remain)
45+
} else {
46+
(parent, remain)
47+
}
48+
} else {
49+
(".", prefix_str.as_str())
50+
};
4251
let base_dir = base.replace(is_separator, MAIN_SEPARATOR_STR);
4352

4453
// Fetch the lib dirs
@@ -61,15 +70,26 @@ impl Completer for DotNuCompletion {
6170
// rsplit_once removes the separator
6271
let cwd = working_set.permanent_state.cwd(None);
6372
if base_dir != "." {
64-
// Search in base_dir as well as lib_dirs
73+
let expanded_base_dir = expand_tilde(&base_dir);
74+
let is_base_dir_relative = expanded_base_dir.is_relative();
75+
// Search in base_dir as well as lib_dirs.
76+
// After expanded, base_dir can be a relative path or absolute path.
77+
// If relative, we join "current working dir" with it to get subdirectory and add to search_dirs.
78+
// If absolute, we add it to search_dirs.
6579
if let Ok(mut cwd) = cwd {
66-
cwd.push(&base_dir);
67-
search_dirs.push(cwd.into_std_path_buf());
80+
if is_base_dir_relative {
81+
cwd.push(&base_dir);
82+
search_dirs.push(cwd.into_std_path_buf());
83+
} else {
84+
search_dirs.push(expanded_base_dir);
85+
}
86+
}
87+
if is_base_dir_relative {
88+
search_dirs.extend(lib_dirs.into_iter().map(|mut dir| {
89+
dir.push(&base_dir);
90+
dir
91+
}));
6892
}
69-
search_dirs.extend(lib_dirs.into_iter().map(|mut dir| {
70-
dir.push(&base_dir);
71-
dir
72-
}));
7393
} else {
7494
if let Ok(cwd) = cwd {
7595
search_dirs.push(cwd.into_std_path_buf());
@@ -105,7 +125,9 @@ impl Completer for DotNuCompletion {
105125
let mut span_offset = 0;
106126
let mut value = x.path.to_string();
107127
// Complete only the last path component
108-
if base_dir != "." {
128+
if base_dir == "/" {
129+
span_offset = base_dir.len()
130+
} else if base_dir != "." {
109131
span_offset = base_dir.len() + 1
110132
}
111133
// Retain only one '`'

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

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,12 @@ use std::path::Path;
1212
use super::{completion_common::FileSuggestion, SemanticSuggestion};
1313

1414
#[derive(Clone, Default)]
15-
pub struct FileCompletion {
16-
suffix: String,
17-
}
15+
pub struct FileCompletion {}
1816

1917
impl FileCompletion {
2018
pub fn new() -> Self {
2119
Self::default()
2220
}
23-
pub fn new_with_suffix(suffix: String) -> Self {
24-
Self { suffix }
25-
}
2621
}
2722

2823
impl Completer for FileCompletion {
@@ -42,46 +37,31 @@ impl Completer for FileCompletion {
4237
readjusted,
4338
} = adjust_if_intermediate(prefix, working_set, span);
4439

45-
let cwd = working_set
46-
.permanent_state
47-
.cwd(None)
48-
.ok()
49-
.and_then(|p| p.into_os_string().into_string().ok());
50-
51-
let cwds = if let Some(d) = cwd {
52-
vec![d]
53-
} else {
54-
Vec::new()
55-
};
56-
57-
let mut files = complete_item(
40+
#[allow(deprecated)]
41+
let items: Vec<_> = complete_item(
5842
readjusted,
5943
span,
6044
&prefix,
61-
&cwds,
45+
&[&working_set.permanent_state.current_work_dir()],
6246
options,
6347
working_set.permanent_state,
6448
stack,
65-
);
66-
if !self.suffix.is_empty() {
67-
files.retain(|f| f.path.trim_end_matches('`').ends_with(&self.suffix));
68-
}
69-
let items: Vec<_> = files
70-
.into_iter()
71-
.map(move |x| SemanticSuggestion {
72-
suggestion: Suggestion {
73-
value: x.path,
74-
style: x.style,
75-
span: reedline::Span {
76-
start: x.span.start - offset,
77-
end: x.span.end - offset,
78-
},
79-
..Suggestion::default()
49+
)
50+
.into_iter()
51+
.map(move |x| SemanticSuggestion {
52+
suggestion: Suggestion {
53+
value: x.path,
54+
style: x.style,
55+
span: reedline::Span {
56+
start: x.span.start - offset,
57+
end: x.span.end - offset,
8058
},
81-
// TODO????
82-
kind: None,
83-
})
84-
.collect();
59+
..Suggestion::default()
60+
},
61+
// TODO????
62+
kind: None,
63+
})
64+
.collect();
8565

8666
// Sort results prioritizing the non hidden folders
8767

0 commit comments

Comments
 (0)