Skip to content

Commit

Permalink
[scripts/posh-vcpkg] Change TabExpansion to Register-ArgumentCompleter
Browse files Browse the repository at this point in the history
  • Loading branch information
WangWeiLin-MV committed Nov 8, 2024
1 parent 4b48438 commit 886dd02
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
12 changes: 6 additions & 6 deletions scripts/posh-vcpkg.psd1
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
@{

# Script module or binary module file associated with this manifest.
ModuleToProcess = 'posh-vcpkg.psm1'
RootModule = 'posh-vcpkg.psm1'

# Version number of this module.
ModuleVersion = '0.0.1'
ModuleVersion = '0.0.2'

# ID used to uniquely identify this module
GUID = '948f02ab-fc99-4a53-8335-b6556eef129b'

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.0'
# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '5.1'

FunctionsToExport = @('TabExpansion')
FunctionsToExport = @()
CmdletsToExport = @()
VariablesToExport = @()
AliasesToExport = @()
Expand All @@ -24,7 +24,7 @@ PrivateData =
PSData =
@{
# Tags applied to this module. These help with module discovery in online galleries.
Tags = @('vcpkg', 'tab', 'tab-completion', 'tab-expansion', 'tabexpansion')
Tags = @('vcpkg', 'tab', 'tab-completion', 'Register-ArgumentCompleter')
}
}

Expand Down
63 changes: 32 additions & 31 deletions scripts/posh-vcpkg.psm1
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
}
}

0 comments on commit 886dd02

Please sign in to comment.