-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: added an map api for pathCompleter #5940
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
epage marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1191,3 +1191,97 @@ fn complete(cmd: &mut Command, args: impl AsRef<str>, current_dir: Option<&Path> | |
.collect::<Vec<_>>() | ||
.join("\n") | ||
} | ||
|
||
#[test] | ||
fn suggest_value_path_with_filter_and_mapper() { | ||
Comment on lines
+1195
to
+1196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intention of the split commits was for this test to overwrite the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All test additions should be done in the first commit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So @epage, i should do that same as i did for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
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(); | ||
|
||
fs::write(testdir_path.join("c_dir").join("Cargo.toml"), "").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()) | ||
.filter(|path| { | ||
path.file_name() | ||
.and_then(|n| n.to_str()) | ||
.map_or(false, |name| name.starts_with('c')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you ran clippy on this, it would complain about the use of |
||
}) | ||
.map(|candidate| { | ||
if candidate.get_value().to_string_lossy().contains("c_dir") { | ||
candidate | ||
.help(Some("Addable cargo package".into())) | ||
.display_order(Some(0)) | ||
} else { | ||
candidate.display_order(Some(1)) | ||
Comment on lines
+1224
to
+1225
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this this being done? display order should be taken care of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we filtered out |
||
} | ||
}), | ||
)), | ||
) | ||
.args_conflicts_with_subcommands(true); | ||
|
||
assert_data_eq!( | ||
complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)), | ||
snapbox::str![[r#" | ||
c_dir/ Addable cargo package | ||
d_dir/ | ||
"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn suggest_value_file_path_with_mapper() { | ||
let testdir = snapbox::dir::DirRoot::mutable_temp().unwrap(); | ||
let testdir_path = testdir.path().unwrap(); | ||
fs::write(testdir_path.join("a_file.txt"), "").unwrap(); | ||
fs::write(testdir_path.join("b_file.md"), "").unwrap(); | ||
fs::write(testdir_path.join("c_file.rs"), "").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::file() | ||
.current_dir(testdir_path.to_owned()) | ||
.map(|candidate| { | ||
let path = Path::new(candidate.get_value()); | ||
if let Some(ext) = path.extension().and_then(|e| e.to_str()) { | ||
match ext { | ||
"rs" => candidate | ||
.help(Some("Rust source file".into())) | ||
.display_order(Some(0)), | ||
"txt" => candidate | ||
.help(Some("Text file".into())) | ||
.display_order(Some(1)), | ||
_ => candidate.display_order(Some(2)) | ||
} | ||
} else { | ||
candidate.display_order(Some(3)) | ||
} | ||
}), | ||
)), | ||
) | ||
.args_conflicts_with_subcommands(true); | ||
|
||
assert_data_eq!( | ||
complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)), | ||
snapbox::str![[r#" | ||
c_file.rs Rust source file | ||
a_file.txt Text file | ||
b_file.md | ||
d_dir/ | ||
"#]], | ||
); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.