-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Enhance autocomplete generation #5612
Conversation
c7ecc12
to
1d85dc9
Compare
b56063e
to
13afd94
Compare
ca1e203
to
966e641
Compare
98662ef
to
6572be0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add instructions on how to test this? I couldn't get autocompletions showing up after doing the following:
$ cargo install --path forc
$ cargo install --path forc-plugins/forc-crypto
$ forc completions
$ forc crypto <TAB>
$ forc-crypto <TAB>
pub struct CommandInfo { | ||
pub name: String, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
#[serde(default)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is #[serde(default)]
necessary for options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, otherwise it would error if the field is not set as null. With serde(default) the non-existence of the field is treated as None/null.
enum Target { | ||
/// Bourne Again SHell (bash) | ||
Bash, | ||
/// Elvish shell | ||
Elvish, | ||
/// Friendly Interactive SHell (fish) | ||
Fish, | ||
/// PowerShell | ||
PowerShell, | ||
/// Z SHell (zsh) | ||
Zsh, | ||
/// Fig | ||
Fig, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to redefine this, or can we just use clap_complete::Shell
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fig is not defined as a Shell.
forc-util/src/cli.rs
Outdated
possible_values: opt | ||
.get_possible_values() | ||
.map(|x| { | ||
x.iter() | ||
.map(|x| PossibleValues { | ||
name: x.get_name().to_owned(), | ||
help: x.get_help().unwrap_or_default().to_owned(), | ||
}) | ||
.collect::<Vec<_>>() | ||
}) | ||
.unwrap_or_default(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: it might be nice to move this to a helper function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this better now 3daa35f?
} | ||
} | ||
|
||
pub fn to_clap(&self) -> clap::App<'_> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should just store the original clap command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original clap definition is defined in an external binary in a different namespace and it cannot be serialized with serde directly. If that was possible this PR would have been way shorter.
} | ||
|
||
impl ArgInfo { | ||
pub fn to_clap(&self) -> clap::Arg<'_> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should just store the original clap arg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot, as the clap struct cannot be serialized, it would have been much easier if that was the case.
forc-util/src/cli.rs
Outdated
.get_possible_values() | ||
.map(|x| { | ||
x.iter() | ||
.map(|x| PossibleValues { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we account for aliases and hidden args?
let mut file = File::create(&forc_autocomplete_path).unwrap_or_else(|_| { | ||
panic!("Cannot write to the autocomplete file in path {forc_autocomplete_path}") | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely ask users before writing to their shell configuration. We should also tell the user what files are being written to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we should just print a shell command they should to autoload the autocomplete definition
forc/src/cli/commands/completions.rs
Outdated
"{}/.config/powershell/Microsoft.PowerShell_profile.ps1", | ||
dir | ||
)), | ||
Target::Bash => Some(format!("{}/.bashrc", dir)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On mac, this is bash_profile
Since this varies by OS, could we use a crate to get the right shell config file(s)?
forc/src/cli/commands/completions.rs
Outdated
|
||
if let Some(file_path) = user_shell_config { | ||
// Update the user_shell_config | ||
let file = File::open(&file_path).expect("Open the shell config file"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this cause a panic? What does this look like to the user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We definitely need to ask the user before modifying their shell config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also have some unit tests for this. This could be useful: https://docs.rs/completest/0.0.12/completest/
I got it working by manually adding It seems like there's an issue with autocompleting flags. For example, I would expect this: It also displays it as
I would also expect this: |
|
||
for line in reader.lines().map_while(Result::ok) { | ||
if line.contains(&forc_autocomplete_path) { | ||
println!("Forc completions is already installed"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
println!("Forc completions are already installed at {shell_config_path}");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the completions script has been updated, the user should not see this message. They should instead see something like "Forc completions were successfully updated at {forc_autocomplete_path}."
6033734
to
914cded
Compare
a100f54
to
a1f26d6
Compare
This PR fixes #5474, This PR enhances the autocomplete feature that is built-in ly forc. The first enhancement is to add support to generate a script for [fig](https://fig.io). The second enhancement is to add support to let each plugin (automatically through cli_examples) dump its clap configuration. This configuration is shared to the main `forc` binary which creates a single autocomplete script for the requested shell that is also aware of every `plugin`. If a plugin uses `cli_examples!` macro this will automatically inherit this feature without any additional change. The third improvement, still under development, is to install automatically the autocomplete feature instead of printing to the stdout (to address FuelLabs/fuelup#548)
Co-authored-by: Sophie Dankel <[email protected]>
Call forc_util::cli::register() explicitly instead of doing some dark magic behind through cli_examples macro
a1f26d6
to
9c2044f
Compare
It is not desired for [fig.io](https://github.com/withfig/autocomplete/actions/runs/7978839646/job/21928919907?pr=2280)
Pull request was converted to draft
This is the first part of #5612, which will be split into several smaller PRs
This is the first part of #5612, which will be split into several smaller PRs
## Description This fixes #5474 This is the first part of #5612, which will be split into several smaller PRs ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
Closing, not high priority. |
This PR fixes #5474,
This PR enhances the autocomplete feature that is built-in ly forc. The first enhancement is to add support to generate a script for fig.
The second enhancement is to add support to let each plugin (automatically through cli_examples) dump its clap configuration. This configuration is shared to the main
forc
binary which creates a single autocomplete script for the requested shell that is also aware of everyplugin
. If a plugin usescli_examples!
macro this will automatically inherit this feature without any additional change.The third improvement, still under development, is to install automatically the autocomplete feature instead of printing to the stdout (to address FuelLabs/fuelup#548)(This feature has been added on c7015d0)Description
Checklist
Breaking*
orNew Feature
labels where relevant.