Debian Codegraph @

src/aot/shells/powershell.rs

raw symbols SCIP
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/::builderrust-analyzer cargo clap_builder 4.6.6 builder/::StyledStrrust-analyzer cargo clap_builder 4.6.6 builder/styled_str/StyledStr#;
4 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#};
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 powershell 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 PowerShellrust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/PowerShell#;
12
13 impl Generatorrust-analyzer cargo clap_complete 4.6.8 aot/generator/Generator# for PowerShellrust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/PowerShell# {
14 fn file_namerust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/impl#[PowerShell][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}.ps1")
16 }
17
18 fn generaterust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/impl#[PowerShell][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/powershell/impl#[PowerShell][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/powershell/impl#[PowerShell][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/powershell/generate_inner().(cmd, "");
29
30 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
31 buf,
32 r#"
33 using namespace System.Management.Automation
34 using namespace System.Management.Automation.Language
35
36 Register-ArgumentCompleter -Native -CommandName '{bin_name}' -ScriptBlock {{
37 param($wordToComplete, $commandAst, $cursorPosition)
38
39 $commandElements = $commandAst.CommandElements
40 $command = @(
41 '{bin_name}'
42 for ($i = 1; $i -lt $commandElements.Count; $i++) {{
43 $element = $commandElements[$i]
44 if ($element -isnot [StringConstantExpressionAst] -or
45 $element.StringConstantType -ne [StringConstantType]::BareWord -or
46 $element.Value.StartsWith('-') -or
47 $element.Value -eq $wordToComplete) {{
48 break
49 }}
50 $element.Value
51 }}) -join ';'
52
53 $completions = @(switch ($command) {{{subcommands_cases}
54 }})
55
56 $completions.Where{{ $_.CompletionText -like "$wordToComplete*" }} |
57 Sort-Object -Property ListItemText
58 }}
59 "#
60 )
61 }
62 }
63
64 // Escape string inside single quotes
65 fn escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/escape_string().(string: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
66 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().('’', "'’")
67 }
68
69 fn escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/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# {
70 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(help) = help {
71 let help_str = help.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
72 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().help_str.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() {
73 return escape_stringrust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/escape_string().(&help_str.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('\n', " "));
74 }
75 }
76 data.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#to_string().()
77 }
78
79 fn generate_innerrust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/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# {
80 debugrust-analyzer cargo clap_complete 4.6.8 macros/debug!!("generate_inner");
81
82 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().() {
83 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().()]
84 } else {
85 p.get_name_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_name_and_visible_aliases().()
86 .into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().()
87 .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}"))
88 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
89 };
90
91 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().();
92 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 [CompletionResult]::new(");
93
94 for option in p.get_optsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_opts().() {
95 generate_aliasesrust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/generate_aliases().(&mut completions, &preamble, option);
96 }
97
98 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) {
99 generate_aliasesrust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/generate_aliases().(&mut completions, &preamble, &flag);
100 }
101
102 for subcommand in p.get_subcommandsrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_subcommands().() {
103 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().() {
104 let tooltip = escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/escape_help().(subcommand.get_aboutrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_about().(), name);
105 completions.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&preamble);
106 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!!(
107 "'{name}', '{name}', [CompletionResultType]::ParameterValue, '{tooltip}')"
108 ));
109 }
110 }
111
112 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().();
113 for command_name in &command_names {
114 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!!(
115 r"
116 '{command_name}' {{{completions}
117 break
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 command_name in &command_names {
124 let subcommand_subcommands_cases = generate_innerrust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/generate_inner().(subcommand, command_name);
125 subcommands_cases.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&subcommand_subcommands_cases);
126 }
127 }
128
129 subcommands_cases
130 }
131
132 fn generate_aliasesrust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/generate_aliases().(completions: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, preamble: &Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, arg: &Argrust-analyzer cargo clap_builder 4.6.6 builder/arg/Arg#) {
133 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write# as _;
134
135 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(aliases) = arg.get_short_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_short_and_visible_aliases().() {
136 let tooltip = escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/escape_help().(arg.get_helprust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_help().(), aliases[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().);
137 for alias in aliases {
138 let _ = writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
139 completions,
140 "{preamble}'-{alias}', '-{alias}{}', [CompletionResultType]::ParameterName, '{tooltip}')",
141 // make PowerShell realize there is a difference between `-s` and `-S`
142 if alias.is_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_uppercase().() { " " } else { "" },
143 );
144 }
145 }
146 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(aliases) = arg.get_long_and_visible_aliasesrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_long_and_visible_aliases().() {
147 let tooltip = escape_helprust-analyzer cargo clap_complete 4.6.8 aot/shells/powershell/escape_help().(arg.get_helprust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]get_help().(), aliases[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().);
148 for alias in aliases {
149 let _ = writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
150 completions,
151 "{preamble}'--{alias}', '--{alias}', [CompletionResultType]::ParameterName, '{tooltip}')"
152 );
153 }
154 }
155 }