| 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/::{Argrust-analyzer cargo clap_builder 4.6.6 builder/arg/Arg#, Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#, ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#, builderrust-analyzer cargo clap_builder 4.6.6 builder/}; |
| 4 | |
| 5 | 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/}; |
| 6 | |
| 7 | /// Generate fish completion file |
| 8 | /// |
| 9 | /// Note: The fish generator currently only supports named options (-o/--option), not positional arguments. |
| 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 Fishrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/Fish#; |
| 12 | |
| 13 | impl Generatorrust-analyzer cargo clap_complete 4.6.8 aot/generator/Generator# for Fishrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/Fish# { |
| 14 | fn file_namerust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/impl#[Fish][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}.fish") |
| 16 | } |
| 17 | |
| 18 | fn generaterust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/impl#[Fish][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/fish/impl#[Fish][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/fish/impl#[Fish][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 name = escape_namerust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_name().(bin_name); |
| 29 | let mut needs_fn_name = &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("__fish_{name}_needs_command")[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().; |
| 30 | let mut using_fn_name = &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("__fish_{name}_using_subcommand")[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().; |
| 31 | // Given `git --git-dir somedir status`, using `__fish_seen_subcommand_from` won't help us |
| 32 | // find out `status` is the real subcommand, and not `somedir`. However, when there are no subcommands, |
| 33 | // there is no need to use our custom stubs. |
| 34 | if cmd.has_subcommandsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]has_subcommands().() { |
| 35 | gen_subcommand_helpersrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/gen_subcommand_helpers().(&name, cmd, buf, needs_fn_name, using_fn_name); |
| 36 | } else { |
| 37 | needs_fn_name = "__fish_use_subcommand"; |
| 38 | using_fn_name = "__fish_seen_subcommand_from"; |
| 39 | } |
| 40 | |
| 41 | let mut buffer = 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().(); |
| 42 | gen_fish_innerrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/gen_fish_inner().( |
| 43 | bin_name, |
| 44 | &[], |
| 45 | cmd, |
| 46 | &mut buffer, |
| 47 | needs_fn_name, |
| 48 | using_fn_name, |
| 49 | ); |
| 50 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(buf, "{buffer}") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Escape string inside single quotes |
| 55 | fn escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_string().(string: &str, escape_comma: bool) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 56 | let string = string.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('\\', "\\\\").replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('\'', "\\'"); |
| 57 | if escape_comma { |
| 58 | string.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(',', "\\,") |
| 59 | } else { |
| 60 | string |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_help().(help: &builderrust-analyzer cargo clap_builder 4.6.6 builder/::StyledStrrust-analyzer cargo clap_builder 4.6.6 builder/styled_str/StyledStr#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 65 | escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/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', " "), false) |
| 66 | } |
| 67 | |
| 68 | fn escape_namerust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_name().(name: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 69 | name.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('-', "_") |
| 70 | } |
| 71 | |
| 72 | fn gen_fish_innerrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/gen_fish_inner().( |
| 73 | root_command: &str, |
| 74 | parent_commands: &[&str], |
| 75 | cmd: &Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#, |
| 76 | buffer: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 77 | needs_fn_name: &str, |
| 78 | using_fn_name: &str, |
| 79 | ) { |
| 80 | debugrust-analyzer cargo clap_complete 4.6.8 macros/debug!!("gen_fish_inner"); |
| 81 | // example : |
| 82 | // |
| 83 | // complete |
| 84 | // -c {command} |
| 85 | // -d "{description}" |
| 86 | // -s {short} |
| 87 | // -l {long} |
| 88 | // -a "{possible_arguments}" |
| 89 | // -r # if require parameter |
| 90 | // -f # don't use file completion |
| 91 | // -n "{needs_fn_name}" # complete for command "myprog" |
| 92 | // -n "{using_fn_name} subcmd1" # complete for command "myprog subcmd1" |
| 93 | |
| 94 | let mut basic_template = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("complete -c {root_command}"); |
| 95 | |
| 96 | if parent_commands.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 97 | if cmd.has_subcommandsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]has_subcommands().() { |
| 98 | basic_template.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!!(" -n \"{needs_fn_name}\"")); |
| 99 | } |
| 100 | } else { |
| 101 | let mut out = 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().(using_fn_name); |
| 102 | match parent_commands { |
| 103 | [] => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(), |
| 104 | [command] => { |
| 105 | out.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!!(" {command}")); |
| 106 | if cmd.has_subcommandsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]has_subcommands().() { |
| 107 | out.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("; and not __fish_seen_subcommand_from"); |
| 108 | } |
| 109 | let subcommands = cmd |
| 110 | .get_subcommandsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_subcommands().() |
| 111 | .flat_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flat_map().(Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#::get_name_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_name_and_visible_aliases().); |
| 112 | for name in subcommands { |
| 113 | out.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}")); |
| 114 | } |
| 115 | } |
| 116 | [command, subcommand] => out.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!!( |
| 117 | " {command}; and __fish_seen_subcommand_from {subcommand}" |
| 118 | )), |
| 119 | // HACK: Assuming subcommands are only nested less than 3 levels as more than that is |
| 120 | // unwieldy and takes more effort to support. |
| 121 | // For example, `rustup toolchain help install` is the longest valid command line of `rustup` |
| 122 | // that uses nested subcommands, and it cannot receive any flags to it. |
| 123 | _ => return, |
| 124 | } |
| 125 | basic_template.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!!(" -n \"{out}\"").as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 126 | } |
| 127 | |
| 128 | debugrust-analyzer cargo clap_complete 4.6.8 macros/debug!!("gen_fish_inner: parent_commands={parent_commands:?}"); |
| 129 | |
| 130 | for option in cmd.get_optsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_opts().() { |
| 131 | let mut template = basic_template.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(); |
| 132 | |
| 133 | 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().() { |
| 134 | for short in shorts { |
| 135 | template.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!!(" -s {short}").as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | 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().() { |
| 140 | for long in longs { |
| 141 | template.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!!(" -l {}", escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_string().(long, false)).as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(data) = option.get_helprust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_help().() { |
| 146 | template.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!!(" -d '{}'", escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_help().(data))); |
| 147 | } |
| 148 | |
| 149 | template.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(value_completionrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/value_completion().(option).as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 150 | |
| 151 | buffer.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(template.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 152 | buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\n'); |
| 153 | } |
| 154 | |
| 155 | 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().(cmd) { |
| 156 | let mut template = basic_template.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(); |
| 157 | |
| 158 | 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().() { |
| 159 | for short in shorts { |
| 160 | template.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!!(" -s {short}").as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | 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().() { |
| 165 | for long in longs { |
| 166 | template.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!!(" -l {}", escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_string().(long, false)).as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(data) = flag.get_helprust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_help().() { |
| 171 | template.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!!(" -d '{}'", escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_help().(data))); |
| 172 | } |
| 173 | |
| 174 | buffer.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(template.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 175 | buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\n'); |
| 176 | } |
| 177 | |
| 178 | let has_positionals = cmd.get_positionalsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_positionals().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]next().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().(); |
| 179 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().has_positionals { |
| 180 | basic_template.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(" -f"); |
| 181 | } |
| 182 | for subcommand in cmd.get_subcommandsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_subcommands().() { |
| 183 | for subcommand_name in subcommand.get_name_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_name_and_visible_aliases().() { |
| 184 | let mut template = basic_template.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(); |
| 185 | |
| 186 | template.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!!(" -a \"{subcommand_name}\"").as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 187 | |
| 188 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(data) = subcommand.get_aboutrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_about().() { |
| 189 | template.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!!(" -d '{}'", escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_help().(data)).as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 190 | } |
| 191 | |
| 192 | buffer.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(template.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 193 | buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\n'); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // generate options of subcommands |
| 198 | for subcommand in cmd.get_subcommandsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_subcommands().() { |
| 199 | for subcommand_name in subcommand.get_name_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_name_and_visible_aliases().() { |
| 200 | let mut parent_commands: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = parent_commands.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 201 | parent_commands.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(subcommand_name); |
| 202 | gen_fish_innerrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/gen_fish_inner().( |
| 203 | root_command, |
| 204 | &parent_commands, |
| 205 | subcommand, |
| 206 | buffer, |
| 207 | needs_fn_name, |
| 208 | using_fn_name, |
| 209 | ); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /// Print fish's helpers for easy handling subcommands. |
| 215 | fn gen_subcommand_helpersrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/gen_subcommand_helpers().( |
| 216 | bin_name: &str, |
| 217 | cmd: &Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#, |
| 218 | buf: &mut dyn Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 219 | needs_fn_name: &str, |
| 220 | using_fn_name: &str, |
| 221 | ) { |
| 222 | let mut optspecs = 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().(); |
| 223 | let cmd_opts = cmd.get_argumentsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_arguments().().filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|a| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().a.is_positionalrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]is_positional().()); |
| 224 | for option in cmd_opts { |
| 225 | optspecs.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(' '); |
| 226 | let mut has_short = false; |
| 227 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(short) = option.get_shortrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_short().() { |
| 228 | has_short = true; |
| 229 | optspecs.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(short); |
| 230 | } |
| 231 | |
| 232 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(long) = option.get_longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_long().() { |
| 233 | if has_short { |
| 234 | optspecs.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('/'); |
| 235 | } |
| 236 | optspecs.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_string().(long, false)); |
| 237 | } |
| 238 | |
| 239 | let is_an_option = option |
| 240 | .get_num_argsrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_num_args().() |
| 241 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|r| r.takes_valuesrust-analyzer cargo clap_builder 4.6.6 builder/range/impl#[ValueRange]takes_values().()) |
| 242 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(true); |
| 243 | if is_an_option { |
| 244 | optspecs.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('='); |
| 245 | } |
| 246 | } |
| 247 | let optspecs_fn_name = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("__fish_{bin_name}_global_optspecs"); |
| 248 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!( |
| 249 | buf, |
| 250 | "# Print an optspec for argparse to handle cmd's options that are independent of any subcommand. |
| 251 | function {optspecs_fn_name} |
| 252 | string join \\n{optspecs} |
| 253 | end |
| 254 | |
| 255 | function {needs_fn_name} |
| 256 | # Figure out if the current invocation already has a command. |
| 257 | set -l cmd (commandline -opc) |
| 258 | set -e cmd[1] |
| 259 | argparse -s ({optspecs_fn_name}) -- $cmd 2>/dev/null |
| 260 | or return |
| 261 | if set -q argv[1] |
| 262 | # Also print the command, so this can be used to figure out what it is. |
| 263 | echo $argv[1] |
| 264 | return 1 |
| 265 | end |
| 266 | return 0 |
| 267 | end |
| 268 | |
| 269 | function {using_fn_name} |
| 270 | set -l cmd ({needs_fn_name}) |
| 271 | test -z \"$cmd\" |
| 272 | and return 1 |
| 273 | contains -- $cmd[1] $argv |
| 274 | end |
| 275 | |
| 276 | " |
| 277 | ).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to write completion file"); |
| 278 | } |
| 279 | |
| 280 | fn value_completionrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/value_completion().(option: &Argrust-analyzer cargo clap_builder 4.6.6 builder/arg/Arg#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 281 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().option.get_num_argsrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_num_args().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("built").takes_valuesrust-analyzer cargo clap_builder 4.6.6 builder/range/impl#[ValueRange]takes_values().() { |
| 282 | return "".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(); |
| 283 | } |
| 284 | |
| 285 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(data) = utilsrust-analyzer cargo clap_complete 4.6.8 aot/generator/utils/::possible_valuesrust-analyzer cargo clap_complete 4.6.8 aot/generator/utils/possible_values().(option) { |
| 286 | // We return the possible values with their own empty description e.g. "a\t''\nb\t''" |
| 287 | // this makes sure that a and b don't get the description of the option or argument |
| 288 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 289 | " -r -f -a \"{}\"", |
| 290 | data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 291 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|value| if value.is_hide_setrust-analyzer cargo clap_builder 4.6.6 builder/possible_value/impl#[PossibleValue]is_hide_set().() { |
| 292 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 293 | } else { |
| 294 | // The help text after \t is wrapped in '' to make sure that the it is taken literally |
| 295 | // and there is no command substitution or variable expansion resulting in unexpected errors |
| 296 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 297 | "{}\\t'{}'", |
| 298 | escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_string().(value.get_namerust-analyzer cargo clap_builder 4.6.6 builder/possible_value/impl#[PossibleValue]get_name().(), true).as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(), |
| 299 | escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/fish/escape_help().(value.get_helprust-analyzer cargo clap_builder 4.6.6 builder/possible_value/impl#[PossibleValue]get_help().().unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().()) |
| 300 | )) |
| 301 | }) |
| 302 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>() |
| 303 | .joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().("\n") |
| 304 | ) |
| 305 | } else { |
| 306 | // NB! If you change this, please also update the table in `ValueHint` documentation. |
| 307 | match option.get_value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_value_hint().() { |
| 308 | ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::Unknownrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#Unknown# => " -r", |
| 309 | // fish has no built-in support to distinguish these |
| 310 | ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::AnyPathrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#AnyPath# | ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::FilePathrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#FilePath# | ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::ExecutablePathrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#ExecutablePath# => " -r -F", |
| 311 | ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::DirPathrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#DirPath# => " -r -f -a \"(__fish_complete_directories)\"", |
| 312 | // It seems fish has no built-in support for completing command + arguments as |
| 313 | // single string (CommandString). Complete just the command name. |
| 314 | ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::CommandStringrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#CommandString# | ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::CommandNamerust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#CommandName# => { |
| 315 | " -r -f -a \"(__fish_complete_command)\"" |
| 316 | } |
| 317 | ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::Usernamerust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#Username# => " -r -f -a \"(__fish_complete_users)\"", |
| 318 | ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::Hostnamerust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#Hostname# => " -r -f -a \"(__fish_print_hostnames)\"", |
| 319 | // Disable completion for others |
| 320 | _ => " -r -f", |
| 321 | } |
| 322 | .to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().() |
| 323 | } |
| 324 | } |