# PowerShell completion script for <<>> Register-ArgumentCompleter -Native -CommandName '<<>>' -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) $line = $commandAst.ToString() $elements = @() if ($commandAst.CommandElements.Count -gt 0) { $elements = $commandAst.CommandElements | ForEach-Object { $_.Value } } $commandName = if ($elements.Count -gt 0) { $elements[0] } else { "" } $currentWord = $wordToComplete $previousWord = "" $wordIndex = 0 $found = $false for ($i = 0; $i -lt $elements.Count; $i++) { if ($elements[$i] -eq $currentWord) { $wordIndex = $i if ($i -gt 0) { $previousWord = $elements[$i - 1] } $found = $true break } } if (-not $found) { $wordIndex = $elements.Count if ($elements.Count -gt 0) { $previousWord = $elements[-1] } } $args = @( "-f", ($line -replace '-', '^') "-C", $cursorPosition.ToString() "-w", ($currentWord -replace '-', '^') "-p", ($previousWord -replace '-', '^') "-c", ($commandName -replace '-', '^') "-i", $wordIndex.ToString() "-F", "Powershell" ) foreach ($element in $elements) { $args += "-a" $args += ($element -replace '-', '^') } $output = & <<>> __comp $args 2>&1 $output = $output -replace "`r`n", "`n" -replace "`r", "`n" if (-not $output) { return @() } $lines = $output -split "`n" if ($lines.Count -eq 0) { return @() } $firstLine = $lines[0].Trim() if ($firstLine -eq "_file_") { if ($lines.Count -gt 1) { $fileSuggestions = $lines[1..($lines.Count-1)] } else { $fileSuggestions = @() } $completionResults = @() $fileSuggestions | ForEach-Object { $path = $_ $isDirectory = $path.EndsWith([System.IO.Path]::DirectorySeparatorChar) -or $path.EndsWith('/') $completionType = if ($isDirectory) { 'ProviderContainer' } else { 'ProviderItem' } $completionResults += [System.Management.Automation.CompletionResult]::new($path, $path, $completionType, $path) } return $completionResults } else { $completionResults = @() foreach ($line in $lines) { $trimmedLine = $line.Trim() if ($trimmedLine -match '^([^$]+)\$\((.+)\)$') { $text = $matches[1] $description = $matches[2] $completionResults += [System.Management.Automation.CompletionResult]::new( $text, $text, 'ParameterValue', $description ) } else { $text = $trimmedLine $resultType = if ($text.StartsWith('-')) { 'ParameterName' } else { 'ParameterValue' } $completionResults += [System.Management.Automation.CompletionResult]::new( $text, $text, $resultType, $text ) } } return $completionResults } }