@@ -7,14 +7,54 @@ import (
77)
88
99func Powershell (completers []string ) string {
10- snippet := `%v%v
10+ snippet := `using namespace System.Management.Automation
11+ using namespace System.Management.Automation.Language
1112
12- $_carapace_lazy = {
13+ %v%v
14+
15+ $_carapace_completer = {
16+ [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "", Scope="Function", Target="*")]
1317 param($wordToComplete, $commandAst, $cursorPosition)
14- $completer = $commandAst.CommandElements[0].Value -replace "\.exe$",""
15- carapace $completer powershell | Out-String | Invoke-Expression
16- & (Get-Item "Function:_${completer}_completer") $wordToComplete $commandAst $cursorPosition
18+ $commandElements = $commandAst.CommandElements
19+
20+ # double quoted value works but seems single quoted needs some fixing (e.g. "example 'acti" -> "example acti")
21+ $elems = @()
22+ foreach ($_ in $commandElements) {
23+ if ($_.Extent.StartOffset -gt $cursorPosition) {
24+ break
25+ }
26+ $t = $_.Extent.Text
27+ if ($_.Extent.EndOffset -gt $cursorPosition) {
28+ $t = $t.Substring(0, $_.Extent.Text.get_Length() - ($_.Extent.EndOffset - $cursorPosition))
29+ }
30+
31+ if ($t.Substring(0,1) -eq "'"){
32+ $t = $t.Substring(1)
33+ }
34+ if ($t.get_Length() -gt 0 -and $t.Substring($t.get_Length()-1) -eq "'"){
35+ $t = $t.Substring(0,$t.get_Length()-1)
36+ }
37+ if ($t.get_Length() -eq 0){
38+ $t = '""'
39+ }
40+ $elems += $t.replace('` + "`" + `,', ',') # quick fix
41+ }
42+
43+ $completions = @(
44+ if (!$wordToComplete) {
45+ carapace $elems[0] powershell $($elems| ForEach-Object {$_}) '' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) }
46+ } else {
47+ carapace $elems[0] powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) }
48+ }
49+ )
50+
51+ if ($completions.count -eq 0) {
52+ return "" # prevent default file completion
53+ }
54+
55+ $completions
1756}
57+
1858%v
1959`
2060
@@ -25,7 +65,7 @@ $_carapace_lazy = {
2565
2666 complete := make ([]string , 0 , len (completers )* 2 )
2767 for _ , completer := range completers {
28- complete = append (complete , fmt .Sprintf (`Register-ArgumentCompleter -Native -ScriptBlock $_carapace_lazy -CommandName '%v'%v,'%v.exe'` , completer , prefix , completer ))
68+ complete = append (complete , fmt .Sprintf (`Register-ArgumentCompleter -Native -ScriptBlock $_carapace_completer -CommandName '%v'%v,'%v.exe'` , completer , prefix , completer ))
2969 }
3070 return fmt .Sprintf (snippet , pathSnippet ("powershell" ), envSnippet ("powershell" ), strings .Join (complete , "\n " ))
3171}
0 commit comments