Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(complete): Allow completing '.' #5692

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clap_complete/src/engine/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ pub(crate) fn complete_path(
};
debug!("complete_path: search_root={search_root:?}, prefix={prefix:?}");

if value_os.is_empty() && is_wanted(&search_root) {
completions.push(".".into());
}

for entry in std::fs::read_dir(&search_root)
.ok()
.into_iter()
Expand Down
35 changes: 35 additions & 0 deletions clap_complete/tests/testsuite/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,41 @@ d_dir/
);
}

#[test]
fn suggest_value_path_dir() {
let testdir = snapbox::dir::DirRoot::mutable_temp().unwrap();
let testdir_path = testdir.path().unwrap();
fs::write(testdir_path.join("a_file"), "").unwrap();
fs::write(testdir_path.join("b_file"), "").unwrap();
fs::create_dir_all(testdir_path.join("c_dir")).unwrap();
fs::create_dir_all(testdir_path.join("d_dir")).unwrap();

let mut cmd = Command::new("dynamic")
.arg(
clap::Arg::new("input")
.long("input")
.short('i')
.add(ArgValueCompleter::new(
PathCompleter::dir().current_dir(testdir_path.to_owned()),
)),
)
.args_conflicts_with_subcommands(true);

assert_data_eq!(
complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)),
snapbox::str![[r#"
.
c_dir/
d_dir/
"#]],
);

assert_data_eq!(
complete!(cmd, "--input c[TAB]", current_dir = Some(testdir_path)),
snapbox::str!["c_dir/"],
);
}

#[test]
fn suggest_custom_arg_value() {
fn custom_completer() -> Vec<CompletionCandidate> {
Expand Down
Loading