-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[scripts/posh-vcpkg] Change TabExpansion to Register-ArgumentCompleter
- Loading branch information
1 parent
4b48438
commit 886dd02
Showing
2 changed files
with
38 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,40 @@ | ||
param() | ||
|
||
if (Get-Module posh-vcpkg) { return } | ||
|
||
if ($PSVersionTable.PSVersion.Major -lt 5) { | ||
Write-Warning ("posh-vcpkg does not support PowerShell versions before 5.0.") | ||
return | ||
} | ||
|
||
if (Test-Path Function:\TabExpansion) { | ||
Rename-Item Function:\TabExpansion VcpkgTabExpansionBackup | ||
} | ||
Register-ArgumentCompleter -Native -CommandName vcpkg -ScriptBlock { | ||
param( | ||
[string]$wordToComplete, | ||
[System.Management.Automation.Language.CommandAst]$commandAst, | ||
[int]$cursorPosition | ||
) | ||
|
||
function TabExpansion($line, $lastWord) { | ||
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart() | ||
if ($cursorPosition -lt $commandAst.CommandElements[0].Extent.EndOffset) { | ||
return | ||
} | ||
|
||
switch -regex ($lastBlock) { | ||
"^(?<vcpkgexe>(\./|\.\\|)vcpkg(\.exe|)) (?<remaining>.*)$" | ||
{ | ||
& $matches['vcpkgexe'] autocomplete $matches['remaining'] | ||
return | ||
} | ||
[string]$commandText = $commandAst.CommandElements[0].Value | ||
|
||
# Fall back on existing tab expansion | ||
default { | ||
if (Test-Path Function:\VcpkgTabExpansionBackup) { | ||
VcpkgTabExpansionBackup $line $lastWord | ||
[string[]]$textsBeforeCursor = $commandAst.CommandElements | | ||
Select-Object -Skip 1 | ForEach-Object { | ||
if ($_.Extent.EndOffset -le $cursorPosition) { | ||
$_.Extent.Text | ||
} | ||
elseif ($_.Extent.StartOffset -lt $cursorPosition) { | ||
$_.Extent.Text.Substring(0, $cursorPosition - $_.Extent.StartOffset) | ||
} | ||
} | ||
} | ||
} | ||
|
||
$exportModuleMemberParams = @{ | ||
Function = @( | ||
'TabExpansion' | ||
$spaceToComplete = if ($wordToComplete -ne '') { $null } | ||
elseif ($PSNativeCommandArgumentPassing -in 'Standard', 'Windows') { '' } | ||
else { '""' } | ||
|
||
[PowerShell]$cmd = [PowerShell]::Create().AddCommand($commandText).AddParameters( | ||
@('autocomplete') + $textsBeforeCursor + $spaceToComplete | ||
) | ||
} | ||
|
||
Export-ModuleMember @exportModuleMemberParams | ||
[string[]]$completions = $cmd.Invoke() | ||
|
||
if ($cmd.HadErrors -or $completions.Count -eq 0) { | ||
return @() | ||
} | ||
else { | ||
return $completions | ||
} | ||
} |