]> git.proxmox.com Git - rustc.git/blob - src/vendor/clap/src/completions/powershell.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / vendor / clap / src / completions / powershell.rs
1
2 // Std
3 use std::io::Write;
4
5 // Internal
6 use app::parser::Parser;
7
8 pub struct PowerShellGen<'a, 'b>
9 where 'a: 'b
10 {
11 p: &'b Parser<'a, 'b>,
12 }
13
14 impl<'a, 'b> PowerShellGen<'a, 'b> {
15 pub fn new(p: &'b Parser<'a, 'b>) -> Self { PowerShellGen { p: p } }
16
17 pub fn generate_to<W: Write>(&self, buf: &mut W) {
18 let bin_name = self.p.meta.bin_name.as_ref().unwrap();
19
20 let (subcommands_detection_cases, subcommands_cases) = generate_inner(self.p, "");
21
22 let mut bin_names = vec![
23 bin_name.to_string(),
24 format!("./{0}", bin_name),
25 ];
26 if cfg!(windows) {
27 bin_names.push(format!("{0}.exe", bin_name));
28 bin_names.push(format!(r".\{0}", bin_name));
29 bin_names.push(format!(r".\{0}.exe", bin_name));
30 bin_names.push(format!(r"./{0}.exe", bin_name));
31 }
32
33 let bin_names = bin_names.iter().fold(String::new(), |previous, current| {
34 format!("{0}, '{1}'", previous, current)
35 });
36 let bin_names = bin_names.trim_left_matches(", ");
37
38 let result = format!(r#"
39 @({bin_names}) | %{{
40 Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {{
41 param($wordToComplete, $commandAst, $cursorPosition)
42
43 $command = '_{bin_name}'
44 $commandAst.CommandElements |
45 Select-Object -Skip 1 |
46 %{{
47 switch ($_.ToString()) {{
48 {subcommands_detection_cases}
49 }}
50 }}
51
52 $completions = @()
53
54 switch ($command) {{
55 {subcommands_cases}
56 }}
57
58 $completions |
59 ?{{ $_ -like "$wordToComplete*" }} |
60 Sort-Object |
61 %{{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }}
62 }}
63 }}
64 "#,
65 bin_names = bin_names,
66 bin_name = bin_name,
67 subcommands_detection_cases = subcommands_detection_cases,
68 subcommands_cases = subcommands_cases
69 );
70
71 w!(buf, result.as_bytes());
72 }
73 }
74
75 fn generate_inner<'a, 'b>(p: &Parser<'a, 'b>, previous_command_name: &str) -> (String, String) {
76 debugln!("PowerShellGen::generate_inner;");
77 let command_name = format!("{}_{}", previous_command_name, &p.meta.name);
78
79 let mut subcommands_detection_cases = if previous_command_name == "" {
80 String::new()
81 } else {
82 format!(r"
83 '{0}' {{
84 $command += '_{0}'
85 break
86 }}
87 ",
88 &p.meta.name)
89 };
90
91 let mut completions = String::new();
92 for subcommand in &p.subcommands {
93 completions.push_str(&format!("'{}', ", &subcommand.p.meta.name));
94 }
95 for short in &p.short_list {
96 completions.push_str(&format!("'-{}', ", short));
97 }
98 for long in &p.long_list {
99 completions.push_str(&format!("'--{}', ", long));
100 }
101
102 let mut subcommands_cases = format!(r"
103 '{}' {{
104 $completions = @({})
105 }}
106 ",
107 &command_name,
108 completions.trim_right_matches(", "));
109
110 for subcommand in &p.subcommands {
111 let (subcommand_subcommands_detection_cases, subcommand_subcommands_cases) =
112 generate_inner(&subcommand.p, &command_name);
113 subcommands_detection_cases.push_str(&subcommand_subcommands_detection_cases);
114 subcommands_cases.push_str(&subcommand_subcommands_cases);
115 }
116
117 (subcommands_detection_cases, subcommands_cases)
118 }