package powershell
import (
_ "embed"
"fmt"
"runtime"
"strings"
shlex "github.com/carapace-sh/carapace-shlex/v2"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)
const snippetOriginal = `using namespace System.Management.Automation
using namespace System.Management.Automation.Language
Function _%v_completer {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "", Scope="Function", Target="*")]
param($wordToComplete, $commandAst, $cursorPosition)
$commandElements = $commandAst.CommandElements
# double quoted value works but seems single quoted needs some fixing (e.g. "example 'acti" -> "example acti")
$elems = @()
foreach ($_ in $commandElements) {
if ($_.Extent.StartOffset -gt $cursorPosition) {
break
}
$t = $_.Extent.Text
if ($_.Extent.EndOffset -gt $cursorPosition) {
$t = $t.Substring(0, $_.Extent.Text.get_Length() - ($_.Extent.EndOffset - $cursorPosition))
}
if ($t.Substring(0,1) -eq "'"){
$t = $t.Substring(1)
}
if ($t.get_Length() -gt 0 -and $t.Substring($t.get_Length()-1) -eq "'"){
$t = $t.Substring(0,$t.get_Length()-1)
}
if ($t.get_Length() -eq 0){
$t = '""'
}
$elems += $t.replace('` + "`" + `,', ',') # quick fix
}
$completions = @(
if (!$wordToComplete) {
%v _carapace powershell $($elems| ForEach-Object {$_}) '' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) }
} else {
%v _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) }
}
)
if ($completions.count -eq 0) {
return "" # prevent default file completion
}
$completions
}
Register-ArgumentCompleter -Native -ScriptBlock (Get-Item "Function:_%v_completer").ScriptBlock -CommandName '%v'%v,%v
`
const snippetMultiSingle = `using namespace System.Management.Automation
using namespace System.Management.Automation.Language
$_%[2]v_completion = {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "", Scope="Function", Target="*")]
param($wordToComplete, $commandAst, $cursorPosition)
$commandElements = $commandAst.CommandElements
# double quoted value works but seems single quoted needs some fixing (e.g. "example 'acti" -> "example acti")
$elems = @()
foreach ($_ in $commandElements) {
if ($_.Extent.StartOffset -gt $cursorPosition) {
break
}
$t = $_.Extent.Text
if ($_.Extent.EndOffset -gt $cursorPosition) {
$t = $t.Substring(0, $_.Extent.Text.get_Length() - ($_.Extent.EndOffset - $cursorPosition))
}
if ($t.Substring(0,1) -eq "'"){
$t = $t.Substring(1)
}
if ($t.get_Length() -gt 0 -and $t.Substring($t.get_Length()-1) -eq "'"){
$t = $t.Substring(0,$t.get_Length()-1)
}
if ($t.get_Length() -eq 0){
$t = '""'
}
$elems += $t.replace('` + "`" + `,', ',') # quick fix
}
$completions = @(
if (!$wordToComplete) {
%[1]v %[3]v _carapace powershell $($elems| ForEach-Object {$_}) '' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) }
} else {
%[1]v %[3]v _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) }
}
)
if ($completions.count -eq 0) {
return "" # prevent default file completion
}
$completions
}
%[4]v
`
const snippetMulti = `using namespace System.Management.Automation
using namespace System.Management.Automation.Language
%[4]v
$_%[1]v_completer = {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "", Scope="Function", Target="*")]
param($wordToComplete, $commandAst, $cursorPosition)
$commandElements = $commandAst.CommandElements
$elems = @()
foreach ($_ in $commandElements) {
if ($_.Extent.StartOffset -gt $cursorPosition) {
break
}
$t = $_.Extent.Text
if ($_.Extent.EndOffset -gt $cursorPosition) {
$t = $t.Substring(0, $_.Extent.Text.get_Length() - ($_.Extent.EndOffset - $cursorPosition))
}
if ($t.Substring(0,1) -eq "'"){
$t = $t.Substring(1)
}
if ($t.get_Length() -gt 0 -and $t.Substring($t.get_Length()-1) -eq "'"){
$t = $t.Substring(0,$t.get_Length()-1)
}
if ($t.get_Length() -eq 0){
$t = '""'
}
$elems += $t.replace('` + "`" + `,', ',') # quick fix
}
$completions = @(
if (!$wordToComplete) {
%[2]v ($elems[0] -replace ('\.exe$', '')) _carapace powershell $($elems| ForEach-Object {$_}) '' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) }
} else {
%[2]v ($elems[0] -replace ('\.exe$', '')) _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) }
}
)
if ($completions.count -eq 0) {
return "" # prevent default file completion
}
$completions
}
%[3]v
`
func sanitize(s string ) string {
return strings .ReplaceAll (s , "-" , "__" )
}
func Snippet (cmd *cobra .Command ) string {
return SnippetSingle (cmd .Name (), false )
}
func SnippetMulti (names []string , defaultName string , snippetFuncs string ) string {
complete := make ([]string , 0 , len (names ))
for _ , name := range names {
prefix := " # "
if runtime .GOOS == "windows" {
prefix = ""
}
complete = append (complete , fmt .Sprintf (`Register-ArgumentCompleter -Native -ScriptBlock $_%v_completer -CommandName %v%v%v` , sanitize (defaultName ), shlex .Quote (name , shlex .Powershell ), prefix , shlex .Quote (name +".exe" , shlex .Powershell )))
}
return fmt .Sprintf (snippetMulti , sanitize (defaultName ), shlex .Quote (uid .Executable (), shlex .Powershell ), strings .Join (complete , "\n" ), snippetFuncs )
}
func SnippetSingle (command string , explicitCommand bool ) string {
prefix := " # "
if runtime .GOOS == "windows" {
prefix = ""
}
if explicitCommand {
complete := fmt .Sprintf (`Register-ArgumentCompleter -Native -ScriptBlock $_%v_completion -CommandName %v%v%v` , sanitize (command ), shlex .Quote (command , shlex .Powershell ), prefix , shlex .Quote (command +".exe" , shlex .Powershell ))
return fmt .Sprintf (snippetMultiSingle , shlex .Quote (uid .Executable (), shlex .Powershell ), sanitize (command ), shlex .Quote (command , shlex .Powershell ), complete )
}
return fmt .Sprintf (snippetOriginal ,
sanitize (command ),
shlex .Quote (uid .Executable (), shlex .Powershell ),
shlex .Quote (uid .Executable (), shlex .Powershell ),
sanitize (command ),
shlex .Escape (command , shlex .Powershell ),
prefix ,
shlex .Escape (command +".exe" , shlex .Powershell ))
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .