Skip to content

feat(clap-complete/unstable-dynamic): add '~' to HOME dir completion support #5998

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion clap_complete/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ is_executable = { version = "1.0.1", optional = true }
shlex = { version = "1.3.0", optional = true }
completest = { version = "0.4.2", optional = true }
completest-pty = { version = "0.5.5", optional = true }
home = { version = "0.5.0", optional = true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With std::env::home_dir being undeprecated, we should be using this through a polyfll. I'll need to go create one before this can move forward.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(if it wasn't for that, I'd be hesitant to add this out of question of whether the extra dep is worth it)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


[dev-dependencies]
snapbox = { version = "0.6.0", features = ["diff", "dir", "examples"] }
Expand All @@ -55,7 +56,7 @@ required-features = ["unstable-dynamic"]
[features]
default = []
unstable-doc = ["unstable-dynamic"] # for docs.rs
unstable-dynamic = ["dep:clap_lex", "dep:shlex", "dep:is_executable", "clap/unstable-ext"]
unstable-dynamic = ["dep:clap_lex", "dep:shlex", "dep:is_executable", "dep:home", "clap/unstable-ext"]
unstable-shell-tests = ["dep:completest", "dep:completest-pty"]
debug = ["clap/debug"]

Expand Down
16 changes: 14 additions & 2 deletions clap_complete/src/engine/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,19 @@ impl ValueCompleter for PathCompleter {
current_dir_actual = std::env::current_dir().ok();
current_dir_actual.as_deref()
});
let mut candidates = complete_path(current, current_dir, filter);

let Some(current) = current.to_str() else {
return vec![];
};
Comment on lines +278 to +280
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be doing this

Copy link
Author

@eatradish eatradish May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should have used let mut current = current.to_string_lossy().to_string(); ?


#[cfg(unix)]
if current.starts_with("~") {
Comment on lines +282 to +283
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is marked as unix-only but the dep is always added

However, should we support this on Windows?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this feature should be supported on Windows, as it does not seem to be used on Windows.

Comment on lines +282 to +283
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, there are other variants of ~ expansion. Should we support that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about what they are, can you explain?

if let Some(home) = home_dir() {
current.replace_range(..1, &home.to_string_lossy());
}
}

let mut candidates = complete_path(&current, current_dir, filter);
if self.stdio && current.is_empty() {
candidates.push(CompletionCandidate::new("-").help(Some("stdio".into())));
}
Expand All @@ -283,7 +295,7 @@ impl ValueCompleter for PathCompleter {
}

pub(crate) fn complete_path(
value_os: &OsStr,
value_os: &str,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we changing this to be more restrictive?

Copy link
Author

@eatradish eatradish May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be a way for me to get &OsStr directly from String, maybe I should? (The reason for the change to String is that the replace_range method is only available for String)

let mut new_current = OsString::new();
new_current.write_str(&current).ok();

let mut candidates = complete_path(&new_current, current_dir, filter);

current_dir: Option<&std::path::Path>,
is_wanted: &dyn Fn(&std::path::Path) -> bool,
) -> Vec<CompletionCandidate> {
Expand Down
Loading