From fbda8dd2c7af235b7751793856c98d9e215291ce Mon Sep 17 00:00:00 2001 From: Chris Nantau Date: Mon, 5 Oct 2020 23:12:48 -0300 Subject: [PATCH] Complete recipes names in PowerShell completion script (#651) --- completions/just.powershell | 20 ++++++++++++++++++++ src/subcommand.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/completions/just.powershell b/completions/just.powershell index aba5992..32d0dd5 100644 --- a/completions/just.powershell +++ b/completions/just.powershell @@ -60,6 +60,26 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock { } }) + function Get-JustFileRecipes([string[]]$CommandElements) { + $justFileIndex = $commandElements.IndexOf("--justfile"); + + if ($justFileIndex -ne -1 && $justFileIndex + 1 -le $commandElements.Length) { + $justFileLocation = $commandElements[$justFileIndex + 1] + } + + $justArgs = @("--summary") + + if (Test-Path $justFileLocation) { + $justArgs += @("--justfile", $justFileLocation) + } + + $recipes = $(just @justArgs) -split ' ' + return $recipes | ForEach-Object { [CompletionResult]::new($_) } + } + + $elementValues = $commandElements | Select-Object -ExpandProperty Value + $recipes = Get-JustFileRecipes -CommandElements $elementValues + $completions += $recipes $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | Sort-Object -Property ListItemText } diff --git a/src/subcommand.rs b/src/subcommand.rs index 1f82f4f..bcf8188 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -113,6 +113,33 @@ _just "$@""#, ), ]; +const POWERSHELL_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[( + r#"$completions.Where{ $_.CompletionText -like "$wordToComplete*" } | + Sort-Object -Property ListItemText"#, + r#"function Get-JustFileRecipes([string[]]$CommandElements) { + $justFileIndex = $commandElements.IndexOf("--justfile"); + + if ($justFileIndex -ne -1 && $justFileIndex + 1 -le $commandElements.Length) { + $justFileLocation = $commandElements[$justFileIndex + 1] + } + + $justArgs = @("--summary") + + if (Test-Path $justFileLocation) { + $justArgs += @("--justfile", $justFileLocation) + } + + $recipes = $(just @justArgs) -split ' ' + return $recipes | ForEach-Object { [CompletionResult]::new($_) } + } + + $elementValues = $commandElements | Select-Object -ExpandProperty Value + $recipes = Get-JustFileRecipes -CommandElements $elementValues + $completions += $recipes + $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | + Sort-Object -Property ListItemText"#, +)]; + const BASH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[( r#" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) @@ -165,11 +192,16 @@ impl Subcommand { Shell::Fish => { script.insert_str(0, FISH_RECIPE_COMPLETIONS); }, + Shell::PowerShell => + for (needle, replacement) in POWERSHELL_COMPLETION_REPLACEMENTS { + replace(&mut script, needle, replacement)?; + }, + Shell::Zsh => for (needle, replacement) in ZSH_COMPLETION_REPLACEMENTS { replace(&mut script, needle, replacement)?; }, - _ => {}, + Shell::Elvish => {}, } println!("{}", script.trim());