completes NU_LIB_DIRS with directory modules

this checks if `search dir + it + mod.nu` exists and adds that to the
completion items if so.
i renamed the outter `it` to `search_dir` to avoid it being shadowed by
the innermost one and avoid defining a binding with the same name.
This commit is contained in:
amtoine 2023-12-14 18:29:09 +01:00
parent 48f29b6332
commit 402acde5c0
No known key found for this signature in database
GPG Key ID: 37AAE9B486CFF1AB

View File

@ -5,7 +5,7 @@ use nu_protocol::{
}; };
use reedline::Suggestion; use reedline::Suggestion;
use std::{ use std::{
path::{is_separator, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR}, path::{is_separator, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR, Path},
sync::Arc, sync::Arc,
}; };
@ -91,16 +91,21 @@ impl Completer for DotNuCompletion {
// and transform them into suggestions // and transform them into suggestions
let output: Vec<Suggestion> = search_dirs let output: Vec<Suggestion> = search_dirs
.into_iter() .into_iter()
.flat_map(|it| { .flat_map(|search_dir| {
file_path_completion(span, &partial, &it, options) let completions = file_path_completion(span, &partial, &search_dir, options);
completions
.into_iter() .into_iter()
.filter(|it| { .filter(move |it| {
// Different base dir, so we list the .nu files or folders // Different base dir, so we list the .nu files or folders
if !is_current_folder { if !is_current_folder {
it.1.ends_with(".nu") || it.1.ends_with(SEP) it.1.ends_with(".nu") || it.1.ends_with(SEP)
} else { } else {
// Lib dirs, so we filter only the .nu files // Lib dirs, so we filter only the .nu files or directory modules
it.1.ends_with(".nu") if it.1.ends_with(SEP) {
Path::new(&search_dir).join(&it.1).join("mod.nu").exists()
} else {
it.1.ends_with(".nu")
}
} }
}) })
.map(move |x| Suggestion { .map(move |x| Suggestion {