Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d7f212

Browse files
committed
fix(complete): Hide dot files on dynamic completer
1 parent 77b3fdb commit 9d7f212

2 files changed

Lines changed: 10 additions & 24 deletions

File tree

clap_complete/src/engine/custom.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,10 @@ pub(crate) fn complete_path(
333333
}
334334

335335
if entry.metadata().map(|m| m.is_dir()).unwrap_or(false) {
336-
let mut suggestion = prefix.join(raw_file_name);
336+
let mut suggestion = prefix.join(&raw_file_name);
337337
suggestion.push(""); // Ensure trailing `/`
338-
let candidate = CompletionCandidate::new(suggestion.as_os_str().to_owned());
338+
let candidate = CompletionCandidate::new(suggestion.as_os_str().to_owned())
339+
.hide(is_hidden(&raw_file_name));
339340

340341
if is_wanted(&entry.path()) {
341342
completions.push(candidate);
@@ -344,8 +345,9 @@ pub(crate) fn complete_path(
344345
}
345346
} else {
346347
if is_wanted(&entry.path()) {
347-
let suggestion = prefix.join(raw_file_name);
348-
let candidate = CompletionCandidate::new(suggestion.as_os_str().to_owned());
348+
let suggestion = prefix.join(&raw_file_name);
349+
let candidate = CompletionCandidate::new(suggestion.as_os_str().to_owned())
350+
.hide(is_hidden(&raw_file_name));
349351
completions.push(candidate);
350352
}
351353
}
@@ -357,6 +359,10 @@ pub(crate) fn complete_path(
357359
completions
358360
}
359361

362+
fn is_hidden(file_name: &OsStr) -> bool {
363+
file_name.starts_with(".")
364+
}
365+
360366
fn split_file_name(path: &std::path::Path) -> (&std::path::Path, &OsStr) {
361367
// Workaround that `Path::new("name/").file_name()` reports `"name"`
362368
if path_has_name(path) {

clap_complete/tests/testsuite/engine.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,6 @@ fn suggest_value_hint_file_path() {
517517
assert_data_eq!(
518518
complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)),
519519
snapbox::str![[r#"
520-
.a_file
521-
.b_file
522-
.c_dir/
523-
.d_dir/
524520
a_file
525521
b_file
526522
c_dir/
@@ -535,10 +531,6 @@ d_dir/
535531
assert_data_eq!(
536532
complete!(cmd, "--input .[TAB]", current_dir = Some(testdir_path)),
537533
snapbox::str![[r#"
538-
./.a_file
539-
./.b_file
540-
./.c_dir/
541-
./.d_dir/
542534
./a_file
543535
./b_file
544536
./c_dir/
@@ -581,12 +573,8 @@ fn suggest_value_path_file() {
581573
assert_data_eq!(
582574
complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)),
583575
snapbox::str![[r#"
584-
.a_file
585-
.b_file
586576
a_file
587577
b_file
588-
.c_dir/
589-
.d_dir/
590578
c_dir/
591579
d_dir/
592580
- stdio
@@ -600,12 +588,8 @@ d_dir/
600588
assert_data_eq!(
601589
complete!(cmd, "--input .[TAB]", current_dir = Some(testdir_path)),
602590
snapbox::str![[r#"
603-
./.a_file
604-
./.b_file
605591
./a_file
606592
./b_file
607-
./.c_dir/
608-
./.d_dir/
609593
./c_dir/
610594
./d_dir/
611595
"#]],
@@ -645,8 +629,6 @@ fn suggest_value_path_dir() {
645629
complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)),
646630
snapbox::str![[r#"
647631
.
648-
.c_dir/
649-
.d_dir/
650632
c_dir/
651633
d_dir/
652634
"#]],
@@ -659,8 +641,6 @@ d_dir/
659641
assert_data_eq!(
660642
complete!(cmd, "--input .[TAB]", current_dir = Some(testdir_path)),
661643
snapbox::str![[r#"
662-
./.c_dir/
663-
./.d_dir/
664644
./c_dir/
665645
./d_dir/
666646
"#]],

0 commit comments

Comments
 (0)