Skip to content
Myr

powershell.rs

This file

Path

crates/core/flags/complete/powershell.rs

85 lines · 2.6 KiB · Rust

Last changed by

style: apply rustfmtAndrew Gallantbefore adoptionbb8172f

History of this file

Take it

The walk stopped after 200 commits: lines older than 5f5da48 point at it.

globset: support nested alternatesLuke Sandbergbefore adoption5f5da48

lines 1–36why, one step back
1/*!
2Provides completions for ripgrep's CLI for PowerShell.3*/45use crate::flags::defs::FLAGS;67const TEMPLATE: &'static str = "8using namespace System.Management.Automation9using namespace System.Management.Automation.Language1011Register-ArgumentCompleter -Native -CommandName 'rg' -ScriptBlock {12 param($wordToComplete, $commandAst, $cursorPosition)13 $commandElements = $commandAst.CommandElements14 $command = @(15 'rg'16 for ($i = 1; $i -lt $commandElements.Count; $i++) {17 $element = $commandElements[$i]18 if ($element -isnot [StringConstantExpressionAst] -or19 $element.StringConstantType -ne [StringConstantType]::BareWord -or20 $element.Value.StartsWith('-')) {21 break22 }23 $element.Value24 }) -join ';'2526 $completions = @(switch ($command) {27 'rg' {28!FLAGS!29 }30 })3132 $completions.Where{ $_.CompletionText -like \"$wordToComplete*\" } |33 Sort-Object -Property ListItemText34}35";36

style: apply rustfmtAndrew Gallantbefore adoptionbb8172f

line 37why, one step back
37const TEMPLATE_FLAG: &'static str = "[CompletionResult]::new('!DASH_NAME!', '!NAME!', [CompletionResultType]::ParameterName, '!DOC!')";

globset: support nested alternatesLuke Sandbergbefore adoption5f5da48

lines 38–85why, one step back
38
39/// Generate completions for PowerShell.40///41/// Note that these completions are based on what was produced for ripgrep <=1342/// using Clap 2.x. Improvements on this are welcome.43pub(crate) fn generate() -> String {44 let mut flags = String::new();45 for (i, flag) in FLAGS.iter().enumerate() {46 let doc = flag.doc_short().replace("'", "''");4748 let dash_name = format!("--{}", flag.name_long());49 let name = flag.name_long();50 if i > 0 {51 flags.push('\n');52 }53 flags.push_str(" ");54 flags.push_str(55 &TEMPLATE_FLAG56 .replace("!DASH_NAME!", &dash_name)57 .replace("!NAME!", &name)58 .replace("!DOC!", &doc),59 );6061 if let Some(byte) = flag.name_short() {62 let dash_name = format!("-{}", char::from(byte));63 let name = char::from(byte).to_string();64 flags.push_str("\n ");65 flags.push_str(66 &TEMPLATE_FLAG67 .replace("!DASH_NAME!", &dash_name)68 .replace("!NAME!", &name)69 .replace("!DOC!", &doc),70 );71 }7273 if let Some(negated) = flag.name_negated() {74 let dash_name = format!("--{negated}");75 flags.push_str("\n ");76 flags.push_str(77 &TEMPLATE_FLAG78 .replace("!DASH_NAME!", &dash_name)79 .replace("!NAME!", &negated)80 .replace("!DOC!", &doc),81 );82 }83 }84 TEMPLATE.trim_start().replace("!FLAGS!", &flags)85}