| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 2 | |
| 3 | use claprust-analyzer cargo clap 4.6.6 crate/::Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#; |
| 4 | use claprust-analyzer cargo clap 4.6.6 crate/::builderrust-analyzer cargo clap_builder 4.6.6 builder/::StyledStrrust-analyzer cargo clap_builder 4.6.6 builder/styled_str/StyledStr#; |
| 5 | |
| 6 | use craterust-analyzer cargo clap_complete 4.6.8 crate/::INTERNAL_ERROR_MSGrust-analyzer cargo clap_complete 4.6.8 INTERNAL_ERROR_MSG.; |
| 7 | use craterust-analyzer cargo clap_complete 4.6.8 crate/::generatorrust-analyzer cargo clap_complete 4.6.8 generator/::{Generatorrust-analyzer cargo clap_complete 4.6.8 aot/generator/Generator#, utilsrust-analyzer cargo clap_complete 4.6.8 aot/generator/utils/}; |
| 8 | |
| 9 | /// Generate elvish completion file |
| 10 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 11 | pub struct Elvishrust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/Elvish#; |
| 12 | |
| 13 | impl Generatorrust-analyzer cargo clap_complete 4.6.8 aot/generator/Generator# for Elvishrust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/Elvish# { |
| 14 | fn file_namerust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/impl#[Elvish][Generator]file_name().(&self, name: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 15 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{name}.elv") |
| 16 | } |
| 17 | |
| 18 | fn generaterust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/impl#[Elvish][Generator]generate().(&self, cmd: &Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#, buf: &mut dyn Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#) { |
| 19 | self.try_generaterust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/impl#[Elvish][Generator]try_generate().(cmd, buf) |
| 20 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to write completion file"); |
| 21 | } |
| 22 | |
| 23 | fn try_generaterust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/impl#[Elvish][Generator]try_generate().(&self, cmd: &Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#, buf: &mut dyn Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 24 | let bin_name = cmd |
| 25 | .get_bin_namerust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_bin_name().() |
| 26 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("crate::generate should have set the bin_name"); |
| 27 | |
| 28 | let subcommands_cases = generate_innerrust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/generate_inner().(cmd, ""); |
| 29 | |
| 30 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!( |
| 31 | buf, |
| 32 | r#" |
| 33 | use builtin; |
| 34 | use str; |
| 35 | |
| 36 | set edit:completion:arg-completer[{bin_name}] = {{|@words| |
| 37 | fn spaces {{|n| |
| 38 | builtin:repeat $n ' ' | str:join '' |
| 39 | }} |
| 40 | fn cand {{|text desc| |
| 41 | edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc |
| 42 | }} |
| 43 | var command = '{bin_name}' |
| 44 | for word $words[1..-1] {{ |
| 45 | if (str:has-prefix $word '-') {{ |
| 46 | break |
| 47 | }} |
| 48 | set command = $command';'$word |
| 49 | }} |
| 50 | var completions = [{subcommands_cases} |
| 51 | ] |
| 52 | $completions[$command] |
| 53 | }} |
| 54 | "#, |
| 55 | ) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Escape string inside single quotes |
| 60 | fn escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/escape_string().(string: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 61 | string.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('\'', "''") |
| 62 | } |
| 63 | |
| 64 | fn escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/escape_help().<T: ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#>(help: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&StyledStrrust-analyzer cargo clap_builder 4.6.6 builder/styled_str/StyledStr#>, data: T) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 65 | match help { |
| 66 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(help) => escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/escape_string().(&help.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('\n', " ")), |
| 67 | _ => data.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#to_string().(), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn generate_innerrust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/generate_inner().(p: &Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#, previous_command_name: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 72 | debugrust-analyzer cargo clap_complete 4.6.8 macros/debug!!("generate_inner"); |
| 73 | |
| 74 | let command_names = if previous_command_name.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 75 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![p.get_bin_namerust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_bin_name().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().(INTERNAL_ERROR_MSGrust-analyzer cargo clap_complete 4.6.8 INTERNAL_ERROR_MSG.).to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()] |
| 76 | } else { |
| 77 | p.get_name_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_name_and_visible_aliases().() |
| 78 | .into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().() |
| 79 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|name| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{previous_command_name};{name}")) |
| 80 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 81 | }; |
| 82 | |
| 83 | let mut completions = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 84 | let preamble = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("\n cand "); |
| 85 | |
| 86 | for option in p.get_optsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_opts().() { |
| 87 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(shorts) = option.get_short_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_short_and_visible_aliases().() { |
| 88 | let tooltip = escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/escape_help().(option.get_helprust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_help().(), shorts[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 89 | for short in shorts { |
| 90 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&preamble); |
| 91 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("-{short} '{tooltip}'").as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(longs) = option.get_long_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_long_and_visible_aliases().() { |
| 96 | let tooltip = escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/escape_help().(option.get_helprust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_help().(), longs[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 97 | for long in longs { |
| 98 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&preamble); |
| 99 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("--{long} '{tooltip}'").as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | for flag in utilsrust-analyzer cargo clap_complete 4.6.8 aot/generator/utils/::flagsrust-analyzer cargo clap_complete 4.6.8 aot/generator/utils/flags().(p) { |
| 105 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(shorts) = flag.get_short_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_short_and_visible_aliases().() { |
| 106 | let tooltip = escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/escape_help().(flag.get_helprust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_help().(), shorts[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 107 | for short in shorts { |
| 108 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&preamble); |
| 109 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("-{short} '{tooltip}'").as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(longs) = flag.get_long_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_long_and_visible_aliases().() { |
| 114 | let tooltip = escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/escape_help().(flag.get_helprust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_help().(), longs[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 115 | for long in longs { |
| 116 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&preamble); |
| 117 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("--{long} '{tooltip}'").as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | for subcommand in p.get_subcommandsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_subcommands().() { |
| 123 | for name in subcommand.get_name_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_name_and_visible_aliases().() { |
| 124 | let tooltip = escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/escape_help().(subcommand.get_aboutrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_about().(), name); |
| 125 | |
| 126 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&preamble); |
| 127 | completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{name} '{tooltip}'").as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | let mut subcommands_cases = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 132 | for command_name in &command_names { |
| 133 | subcommands_cases.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 134 | r" |
| 135 | &'{command_name}'= {{{completions} |
| 136 | }}" |
| 137 | )); |
| 138 | } |
| 139 | |
| 140 | for subcommand in p.get_subcommandsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_subcommands().() { |
| 141 | for command_name in &command_names { |
| 142 | let subcommand_subcommands_cases = generate_innerrust-analyzer cargo clap_complete 4.6.8 aot/shells/elvish/generate_inner().(subcommand, command_name); |
| 143 | subcommands_cases.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&subcommand_subcommands_cases); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | subcommands_cases |
| 148 | } |