mirror of
https://github.com/nushell/nushell.git
synced 2025-01-23 06:39:17 +01:00
Ensure command name available for Argument completion locations (#2443)
This commit is contained in:
parent
ee71a35786
commit
e3c4d82798
@ -184,6 +184,7 @@ pub fn completion_location(line: &str, block: &Block, pos: usize) -> Vec<Complet
|
|||||||
if locations.is_empty() {
|
if locations.is_empty() {
|
||||||
vec![LocationType::Command.spanned(Span::unknown())]
|
vec![LocationType::Command.spanned(Span::unknown())]
|
||||||
} else {
|
} else {
|
||||||
|
let mut command = None;
|
||||||
let mut prev = None;
|
let mut prev = None;
|
||||||
for loc in locations {
|
for loc in locations {
|
||||||
// We don't use span.contains because we want to include the end. This handles the case
|
// We don't use span.contains because we want to include the end. This handles the case
|
||||||
@ -210,6 +211,10 @@ pub fn completion_location(line: &str, block: &Block, pos: usize) -> Vec<Complet
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let LocationType::Command = loc.item {
|
||||||
|
command = Some(String::from(loc.span.slice(line)));
|
||||||
|
}
|
||||||
|
|
||||||
prev = Some(loc);
|
prev = Some(loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,7 +226,7 @@ pub fn completion_location(line: &str, block: &Block, pos: usize) -> Vec<Complet
|
|||||||
vec![LocationType::Command.spanned(Span::unknown())]
|
vec![LocationType::Command.spanned(Span::unknown())]
|
||||||
} else {
|
} else {
|
||||||
// TODO this should be able to be mapped to a command
|
// TODO this should be able to be mapped to a command
|
||||||
vec![LocationType::Argument(None, None).spanned(Span::unknown())]
|
vec![LocationType::Argument(command, None).spanned(Span::unknown())]
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Cursor is before any possible completion location, so must be a command
|
// Cursor is before any possible completion location, so must be a command
|
||||||
@ -354,6 +359,17 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn has_correct_command_name_for_argument() {
|
||||||
|
let registry: VecRegistry = vec![Signature::build("cd")].into();
|
||||||
|
let line = "cd ";
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
completion_location(line, ®istry, 3),
|
||||||
|
vec![LocationType::Argument(Some("cd".to_string()), None)],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn completes_flags_with_just_a_single_hyphen() {
|
fn completes_flags_with_just_a_single_hyphen() {
|
||||||
let registry: VecRegistry = vec![Signature::build("du")
|
let registry: VecRegistry = vec![Signature::build("du")
|
||||||
|
@ -45,14 +45,12 @@ impl NuCompleter {
|
|||||||
flag_completer.complete(context, cmd, partial)
|
flag_completer.complete(context, cmd, partial)
|
||||||
}
|
}
|
||||||
|
|
||||||
LocationType::Argument(_cmd, _arg_name) => {
|
LocationType::Argument(cmd, _arg_name) => {
|
||||||
// TODO use cmd and arg_name to narrow things down further
|
|
||||||
let path_completer = crate::completion::path::Completer::new();
|
let path_completer = crate::completion::path::Completer::new();
|
||||||
let completed_paths = path_completer.complete(context, partial);
|
let completed_paths = path_completer.complete(context, partial);
|
||||||
if &line[..2] == "cd" {
|
match cmd.as_deref().unwrap_or("") {
|
||||||
autocomplete_only_folders(completed_paths)
|
"cd" => select_directory_suggestions(completed_paths),
|
||||||
} else {
|
_ => completed_paths,
|
||||||
completed_paths
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,16 +66,15 @@ impl NuCompleter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn autocomplete_only_folders(completed_paths: Vec<Suggestion>) -> Vec<Suggestion> {
|
fn select_directory_suggestions(completed_paths: Vec<Suggestion>) -> Vec<Suggestion> {
|
||||||
let mut result = Vec::new();
|
completed_paths
|
||||||
for path in completed_paths {
|
.into_iter()
|
||||||
let filepath = path.replacement.clone();
|
.filter(|suggestion| {
|
||||||
let md = metadata(filepath).expect("Expect filepath");
|
metadata(&suggestion.replacement)
|
||||||
if md.is_dir() {
|
.map(|md| md.is_dir())
|
||||||
result.push(path);
|
.unwrap_or(false)
|
||||||
}
|
})
|
||||||
}
|
.collect()
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn requote(item: Suggestion) -> Suggestion {
|
fn requote(item: Suggestion) -> Suggestion {
|
||||||
|
Loading…
Reference in New Issue
Block a user