Repository: rsteube/carapace (go)

Issue:
powershell: commandline not truncated to cursor position
commandline currently not truncated to cursor position so
```sh
example acti<TAB> positional1
```

is passed as
```sh
2021/05/27 16:32:12 powershell [/home/rsteube/go/bin/example _carapace powershell _ example acti positional1]
```

so previous words can't be completed


Pull request description:
powershell: truncate args to cursor position
fix #309 

Patch:
diff --git a/internal/powershell/snippet.go b/internal/powershell/snippet.go
index 7b94bc4..8724d03 100644
--- a/internal/powershell/snippet.go
+++ b/internal/powershell/snippet.go
@@ -13,22 +13,30 @@ func Snippet(cmd *cobra.Command) string {
 using namespace System.Management.Automation.Language
 Function _%v_completer {
     [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "", Scope="Function", Target="*")]
-    param($wordToComplete, $commandAst) #, $cursorPosition)
+    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 = $commandElements | ForEach-Object {
-       $t =$_.Extent.Text
-       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 = '""'
-       }
-       $t
+    $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
     }
 
     $completions = @(


Test patch:
diff --git a/example/cmd/_test/powershell.ps1 b/example/cmd/_test/powershell.ps1
index a0981d2..31a9837 100644
--- a/example/cmd/_test/powershell.ps1
+++ b/example/cmd/_test/powershell.ps1
@@ -2,22 +2,30 @@ using namespace System.Management.Automation
 using namespace System.Management.Automation.Language
 Function _example_completer {
     [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "", Scope="Function", Target="*")]
-    param($wordToComplete, $commandAst) #, $cursorPosition)
+    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 = $commandElements | ForEach-Object {
-       $t =$_.Extent.Text
-       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 = '""'
-       }
-       $t
+    $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
     }
 
     $completions = @(