Complete recipes names in PowerShell completion script (#651)

This commit is contained in:
Chris Nantau 2020-10-05 23:12:48 -03:00 committed by GitHub
parent a8361012d6
commit fbda8dd2c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View File

@ -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*" } | $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText Sort-Object -Property ListItemText
} }

View File

@ -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)] = &[( const BASH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[(
r#" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then r#" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
@ -165,11 +192,16 @@ impl Subcommand {
Shell::Fish => { Shell::Fish => {
script.insert_str(0, FISH_RECIPE_COMPLETIONS); script.insert_str(0, FISH_RECIPE_COMPLETIONS);
}, },
Shell::PowerShell =>
for (needle, replacement) in POWERSHELL_COMPLETION_REPLACEMENTS {
replace(&mut script, needle, replacement)?;
},
Shell::Zsh => Shell::Zsh =>
for (needle, replacement) in ZSH_COMPLETION_REPLACEMENTS { for (needle, replacement) in ZSH_COMPLETION_REPLACEMENTS {
replace(&mut script, needle, replacement)?; replace(&mut script, needle, replacement)?;
}, },
_ => {}, Shell::Elvish => {},
} }
println!("{}", script.trim()); println!("{}", script.trim());