Skip to content

Commit 0235b58

Browse files
committed
[scripts/posh-vcpkg] Change TabExpansion to Register-ArgumentCompleter
1 parent 3a7bec0 commit 0235b58

File tree

5 files changed

+199
-41
lines changed

5 files changed

+199
-41
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
BeforeAll {
2+
Import-Module $PSScriptRoot/posh-vcpkg
3+
}
4+
5+
Describe 'Module posh-vcpkg tests' {
6+
7+
BeforeAll {
8+
9+
function Complete-InputCaret {
10+
[OutputType([System.Management.Automation.CommandCompletion])]
11+
param (
12+
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
13+
[string]$caretCursorCommand
14+
)
15+
$positionMatches = [regex]::Matches($caretCursorCommand, '\^')
16+
if ($positionMatches.Count -ne 1) {
17+
throw 'Invalid caret cursor command, please indicate by only one ^ character'
18+
}
19+
else {
20+
$command = [string]$caretCursorCommand.Replace('^', '');
21+
$cursorPosition = [int]$positionMatches[0].Index;
22+
return [System.Management.Automation.CommandCompletion]::CompleteInput($command, $cursorPosition, $null)
23+
}
24+
}
25+
26+
function Expand-CompletionText {
27+
param (
28+
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
29+
[System.Management.Automation.CommandCompletion]$CommandCompletion
30+
)
31+
return $CommandCompletion.CompletionMatches | Select-Object -ExpandProperty CompletionText
32+
}
33+
34+
}
35+
36+
Context 'Internal function tests' {
37+
38+
It 'Complete-InputCaret 1 caret string should success' {
39+
'aaaa^' | Complete-InputCaret | Should -Not -BeNullOrEmpty
40+
}
41+
42+
It 'Complete-InputCaret 0 caret string should throw' {
43+
{ 'aaaa' | Complete-InputCaret } | Should -Throw
44+
}
45+
46+
It 'Complete-InputCaret 2 caret string should throw' {
47+
{ 'aaaa^^' | Complete-InputCaret } | Should -Throw
48+
}
49+
50+
It 'Expand-CompletionText self should success' {
51+
$inputStr = 'Expand-CompletionText^'
52+
$res = $inputStr | Complete-InputCaret
53+
$res | Expand-CompletionText | Should -Contain 'Expand-CompletionText'
54+
}
55+
56+
}
57+
58+
Context 'Complete command name tests' {
59+
60+
It 'Should complete command word <comment> [<caretCmd>]' -TestCases (
61+
@{ comment = 'without word'; caretCmd = 'vcpkg ^'; expectedContain = 'version' },
62+
@{ comment = 'with word'; caretCmd = 'vcpkg ver^'; expectedContain = 'version' },
63+
@{ comment = 'with native exe'; caretCmd = 'vcpkg.exe ver^'; expectedContain = 'version' },
64+
@{ comment = 'with dot slash'; caretCmd = './vcpkg ver^'; expectedContain = 'version' },
65+
@{ comment = 'with dot backslash'; caretCmd = '.\vcpkg ver^'; expectedContain = 'version' }
66+
) {
67+
param($caretCmd, $expectedContain, $comment)
68+
$caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain
69+
}
70+
71+
}
72+
73+
Context 'Complete command spaces tests' {
74+
75+
It 'Should complete command <comment> [<caretCmd>]' -TestCases (
76+
@{ comment = 'spaces without argument'; caretCmd = 'vcpkg ^'; expectedContain = 'version' },
77+
@{ comment = 'before remaining'; caretCmd = 'vcpkg ver^'; expectedContain = 'version' },
78+
# @{ comment = 'with trailing spaces'; caretCmd = 'vcpkg ver ^'; expectedContain = 'version' },
79+
@{ comment = 'with leading spaces'; caretCmd = ' vcpkg ver^'; expectedContain = 'version' }
80+
) {
81+
param($caretCmd, $expectedContain, $comment)
82+
$caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain
83+
}
84+
85+
It 'Should complete command with trailing spaces [vcpkg ver ^]' -Skip {
86+
'vcpkg ver ^' | Complete-InputCaret | Expand-CompletionText | Should -Contain 'version'
87+
}
88+
89+
}
90+
91+
Context 'Complete command quotation tests' -Skip {
92+
93+
It "Should complete command with quoted word [vcpkg 'ver'^]" {
94+
"vcpkg 'ver'^" | Complete-InputCaret | Expand-CompletionText | Should -Contain 'version'
95+
}
96+
97+
It "Should complete command with quoted space [vcpkg ' '^]" {
98+
"vcpkg 'ver'^" | Complete-InputCaret | Expand-CompletionText | Should -Contain 'version'
99+
}
100+
101+
It "Should complete command with quoted word [vcpkg 'version'^]" {
102+
"vcpkg 'ver'^" | Complete-InputCaret | Expand-CompletionText | Should -Contain 'version'
103+
}
104+
105+
}
106+
107+
Context 'Complete command intermediate tests' {
108+
109+
It 'Should complete command <comment> [<caretCmd>]' -TestCases (
110+
@{ comment = 'end of word'; caretCmd = 'vcpkg version^'; expectedContain = 'version' },
111+
@{ comment = 'middle of word'; caretCmd = 'vcpkg ver^sion'; expectedContain = 'version' },
112+
@{ comment = 'front of word'; caretCmd = 'vcpkg ^version'; expectedContain = 'version' }
113+
) {
114+
param($caretCmd, $expectedContain, $comment)
115+
$caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain
116+
}
117+
118+
}
119+
120+
Context 'Complete subcommand tests' {
121+
122+
It 'Should complete subcommand [<expected>] from [<caretCmd>]' -TestCases (
123+
@{ caretCmd = 'vcpkg depend^'; expected = 'depend-info' },
124+
@{ caretCmd = 'vcpkg inst^'; expected = 'install' },
125+
@{ caretCmd = 'vcpkg int^'; expected = 'integrate' },
126+
@{ caretCmd = 'vcpkg rem^'; expected = 'remove' }
127+
) {
128+
param($caretCmd, $expected)
129+
@($caretCmd | Complete-InputCaret | Expand-CompletionText)[0] | Should -BeExactly $expected
130+
}
131+
132+
It 'Should complete subcommand two-level [powershell] from [vcpkg integrate power^]' -Skip {
133+
'vcpkg integrate power^' | Complete-InputCaret | Expand-CompletionText | Should -Contain 'powershell'
134+
}
135+
136+
}
137+
138+
Context 'Complete subcommand argument and options tests' -Skip {
139+
140+
It 'Should complete argument [<expected>] from [<caretCmd>]' -TestCases (
141+
@{ caretCmd = 'vcpkg install vcpkg-cmake^'; expectedContain = 'vcpkg-cmake-get-vars' },
142+
@{ caretCmd = 'vcpkg install vcpkg-cmake --^'; expectedContain = '--dry-run' }
143+
) {
144+
param($caretCmd, $expected)
145+
$caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain
146+
}
147+
148+
}
149+
150+
Context 'Complete complex tests' {
151+
152+
It 'Should complete complex line [<expected>] from [<caretCmd>]' -TestCases (
153+
@{ caretCmd = 'echo powershell | % { vcpkg ver^ $_ }; echo $?'; expectedContain = 'version' }
154+
) {
155+
param($caretCmd, $expected)
156+
$caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain
157+
}
158+
159+
}
160+
161+
}

scripts/posh-vcpkg.psd1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
@{
22

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

66
# Version number of this module.
7-
ModuleVersion = '0.0.1'
7+
ModuleVersion = '0.0.2'
88

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

12-
# Minimum version of the Windows PowerShell engine required by this module
13-
PowerShellVersion = '5.0'
12+
# Minimum version of the PowerShell engine required by this module
13+
PowerShellVersion = '5.1'
1414

15-
FunctionsToExport = @('TabExpansion')
15+
FunctionsToExport = @()
1616
CmdletsToExport = @()
1717
VariablesToExport = @()
1818
AliasesToExport = @()
@@ -24,7 +24,7 @@ PrivateData =
2424
PSData =
2525
@{
2626
# Tags applied to this module. These help with module discovery in online galleries.
27-
Tags = @('vcpkg', 'tab', 'tab-completion', 'tab-expansion', 'tabexpansion')
27+
Tags = @('vcpkg', 'tab', 'tab-completion', 'Register-ArgumentCompleter')
2828
}
2929
}
3030

scripts/posh-vcpkg.psm1

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
1-
param()
1+
Register-ArgumentCompleter -Native -CommandName vcpkg -ScriptBlock {
2+
param(
3+
[string]$wordToComplete,
4+
[System.Management.Automation.Language.CommandAst]$commandAst,
5+
[int]$cursorPosition
6+
)
27

3-
if (Get-Module posh-vcpkg) { return }
8+
if ($cursorPosition -lt $commandAst.CommandElements[0].Extent.EndOffset) {
9+
return
10+
}
411

5-
if ($PSVersionTable.PSVersion.Major -lt 5) {
6-
Write-Warning ("posh-vcpkg does not support PowerShell versions before 5.0.")
7-
return
8-
}
12+
[string]$commandText = $commandAst.CommandElements[0].Value
913

10-
if (Test-Path Function:\TabExpansion) {
11-
Rename-Item Function:\TabExpansion VcpkgTabExpansionBackup
12-
}
14+
[string[]]$textsBeforeCursor = $commandAst.CommandElements |
15+
Select-Object -Skip 1 | ForEach-Object {
16+
if ($_.Extent.EndOffset -le $cursorPosition) {
17+
$_.Extent.Text
18+
}
19+
elseif ($_.Extent.StartOffset -lt $cursorPosition) {
20+
$_.Extent.Text.Substring(0, $cursorPosition - $_.Extent.StartOffset)
21+
}
22+
}
1323

14-
function TabExpansion($line, $lastWord) {
15-
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
24+
$spaceToComplete = if ($wordToComplete -eq '') { '' } else { $null }
1625

17-
switch -regex ($lastBlock) {
18-
"^(?<vcpkgexe>(\./|\.\\|)vcpkg(\.exe|)) (?<remaining>.*)$"
19-
{
20-
& $matches['vcpkgexe'] autocomplete $matches['remaining']
21-
return
26+
if (Get-Command $commandText -ErrorAction SilentlyContinue) {
27+
$completions = & $commandText autocomplete @textsBeforeCursor $spaceToComplete
28+
if ($null -eq $completions) {
29+
return ''
2230
}
23-
24-
# Fall back on existing tab expansion
25-
default {
26-
if (Test-Path Function:\VcpkgTabExpansionBackup) {
27-
VcpkgTabExpansionBackup $line $lastWord
28-
}
31+
else {
32+
return $completions
2933
}
3034
}
3135
}
32-
33-
$exportModuleMemberParams = @{
34-
Function = @(
35-
'TabExpansion'
36-
)
37-
}
38-
39-
Export-ModuleMember @exportModuleMemberParams

vcpkg-init/mint-standalone-bundle.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ $scripts_dependencies = @(
5252

5353
$scripts_exclusions = @(
5454
'buildsystems/msbuild/applocal.ps1',
55-
'posh-vcpkg/0.0.1/posh-vcpkg.psm1',
56-
'posh-vcpkg/0.0.1/posh-vcpkg.psd1'
55+
'posh-vcpkg/'
5756
)
5857

5958
if (Test-Path $TempDir) {
@@ -101,9 +100,11 @@ try {
101100
Set-Content -Path "out/scripts/buildsystems/msbuild/vcpkg.props" -Value $propsContent -NoNewline -Encoding Ascii
102101

103102
Copy-Item -Path "$PSScriptRoot/vcpkg.targets" -Destination 'out/scripts/buildsystems/msbuild/vcpkg.targets'
104-
New-Item -Path 'out/scripts/posh-vcpkg/0.0.1' -ItemType 'Directory' -Force
105-
Copy-Item -Path "$ArchIndependentSignedFilesRoot/scripts/posh-vcpkg.psm1" -Destination 'out/scripts/posh-vcpkg/0.0.1/posh-vcpkg.psm1'
106-
Copy-Item -Path "$ArchIndependentSignedFilesRoot/scripts/posh-vcpkg.psd1" -Destination 'out/scripts/posh-vcpkg/0.0.1/posh-vcpkg.psd1'
103+
104+
$poshVersion = (Import-PowerShellDataFile -Path "$ArchIndependentSignedFilesRoot/scripts/posh-vcpkg.psd1").ModuleVersion
105+
New-Item -Path "out/scripts/posh-vcpkg/$poshVersion" -ItemType 'Directory' -Force
106+
Copy-Item -Path "$ArchIndependentSignedFilesRoot/scripts/posh-vcpkg.psm1" -Destination "out/scripts/posh-vcpkg/$poshVersion/posh-vcpkg.psm1"
107+
Copy-Item -Path "$ArchIndependentSignedFilesRoot/scripts/posh-vcpkg.psd1" -Destination "out/scripts/posh-vcpkg/$poshVersion/posh-vcpkg.psd1"
107108

108109
Copy-Item -Path "$ArchIndependentSignedFilesRoot/vcpkg-artifacts" -Destination 'out/vcpkg-artifacts' -Recurse
109110

0 commit comments

Comments
 (0)