From 886dd02306c0a00fd7d485cc6951e7c3f246c318 Mon Sep 17 00:00:00 2001 From: WangWeiLin-MV <156736127+WangWeiLin-MV@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:33:15 +0800 Subject: [PATCH] [scripts/posh-vcpkg] Change TabExpansion to Register-ArgumentCompleter --- scripts/posh-vcpkg.psd1 | 12 ++++---- scripts/posh-vcpkg.psm1 | 63 +++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/scripts/posh-vcpkg.psd1 b/scripts/posh-vcpkg.psd1 index 3fb94fe7dc..a71de30595 100644 --- a/scripts/posh-vcpkg.psd1 +++ b/scripts/posh-vcpkg.psd1 @@ -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 = @() @@ -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') } } diff --git a/scripts/posh-vcpkg.psm1 b/scripts/posh-vcpkg.psm1 index 25ef99609f..9b1de25238 100644 --- a/scripts/posh-vcpkg.psm1 +++ b/scripts/posh-vcpkg.psm1 @@ -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) { - "^(?(\./|\.\\|)vcpkg(\.exe|)) (?.*)$" - { - & $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 + } +}