/
ArgumentCompleters.ps1
252 lines (217 loc) · 8.17 KB
/
ArgumentCompleters.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<#
.Synopsis
TabExpansion2 argument completers.
Author: Roman Kuzmin
.Description
The script should be in the path. It is invoked once on TabExpansion2
defined by TabExpansion2.ps1 in order to register various completers.
This script reflects preferences of the author. Use it as the sample for
your completers. Multiple profiles *ArgumentCompleters.ps1 are supported.
#>
### FarHost completers
if ($Host.Name -ceq 'FarHost') {
### Find-FarFile:Name - names from the active panel
Register-ArgumentCompleter -CommandName Find-FarFile -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters)
@(foreach($_ in $Far.Panel.GetFiles()) {$_.Name}) -like "$wordToComplete*"
}
### Out-FarPanel:Columns - evaluated properties and column info template
Register-ArgumentCompleter -CommandName Out-FarPanel -ParameterName Columns -ScriptBlock {
$private:commandName, $private:parameterName, $private:wordToComplete, $private:commandAst, $private:boundParameters = $args
# properties
if (($private:ast = $commandAst.Parent) -is [System.Management.Automation.Language.PipelineAst] -and $ast.PipelineElements.Count -eq 2) {
try {
(& ([scriptblock]::Create($ast.PipelineElements[0])) | Get-Member $wordToComplete* -MemberType Properties).Name |
Sort-Object -Unique
}
catch {}
}
# column info template
$$ = "@{e=''; n=''; k=''; w=0; a=''}"
[System.Management.Automation.CompletionResult]::new($$, '@{...}', 'ParameterValue', $$)
}
}
### Parameter ComputerName for all cmdlets
Register-ArgumentCompleter -ParameterName ComputerName -ScriptBlock {
foreach($_ in Get-Item env:\*COMPUTERNAME* | Sort-Object Value) {
$_ = $_.Value
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
### Native git
Register-ArgumentCompleter -Native -CommandName git -ScriptBlock {
param($wordToComplete, $commandAst)
# if (!word)
# git x [Tab] ~ ast -ne "git" -- skip it
# git [Tab] ~ ast -eq "git" -- get all commands
$code = "$commandAst"
if (!$wordToComplete -and $code -ne 'git') {return}
if ($wordToComplete -notmatch '^\w?[\w\-]*$') {return}
if ($code -notmatch "^git\s*$wordToComplete$") {return}
# git 2.20.1.windows.1 needs --no-verbose
$wild = "$wordToComplete*"
$(foreach($line in git help --all --no-verbose) {
if ($line -match '^ \S') {
foreach($token in $line -split '\s+') {
if ($token -and $token -like $wild) {
$token
}
}
}
}) | Sort-Object
}
### Native dotnet
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | .{process{
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}}
}
### Native dotnet-suggest
if (Get-Command dotnet-suggest -ErrorAction Ignore) {
$names = @((Get-Item $HOME\.dotnet\tools\*.exe).ForEach{$_.Name})
Register-ArgumentCompleter -Native -CommandName $names -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$fullpath = (Get-Command $commandAst.CommandElements[0]).Source
$arguments = $commandAst.Extent.ToString().Replace('"', '\"')
dotnet-suggest get -e $fullpath --position $cursorPosition -- "$arguments" | .{process{
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}}
}
}
### Result processors
Register-ResultCompleter {
### .WORD[Tab] -> Equals, GetType, ToString, ForEach, Where.
param($result, $ast, $tokens, $positionOfCursor, $options)
if ($result.CompletionMatches) {return}
$line = $positionOfCursor.Line
foreach($m in [regex]::Matches($line, '\.(\w*)')) {
$m1 = $m.Groups[1]
if ($m1.Index + $m1.Length -eq $positionOfCursor.ColumnNumber - 1) {
$result.ReplacementIndex = $positionOfCursor.Offset - $m1.Length
$result.ReplacementLength = $m1.Length
foreach($_ in @('Equals(', 'GetType()', 'ToString()', 'ForEach{', 'Where{') -match "^$m1") {
$result.CompletionMatches.Add([System.Management.Automation.CompletionResult]::new($_, $_, 'Method', $_))
}
break
}
}
}
Register-ResultCompleter {
### Expand an alias to its definition
param($result, $ast, $tokens, $positionOfCursor, $options)
$token = foreach($_ in $tokens) {if ($_.Extent.EndOffset -eq $positionOfCursor.Offset) {$_; break}}
if (!$token -or $token.TokenFlags -ne 'CommandName') {return}
# aliases
$name = [WildcardPattern]::Escape("$token")
$aliases = @(Get-Alias $name -ErrorAction Ignore)
if ($aliases.Count -ne 1) {return}
# remove itself
for($i = $result.CompletionMatches.Count; --$i -ge 0) {
if ($result.CompletionMatches[$i].CompletionText -eq $name) {
$result.CompletionMatches.RemoveAt($i)
break
}
}
# insert first
$result.CompletionMatches.Insert(0, $(
$$ = $aliases[0].Definition
[System.Management.Automation.CompletionResult]::new($$, $$, 'Command', $$)
))
}
Register-ResultCompleter {
### Complete variable $*var
${private:*result}, $null, ${private:*tokens}, ${private:*positionOfCursor}, $null = $args
${private:*token} = foreach($_ in ${*tokens}) {if ($_.Extent.EndOffset -eq ${*positionOfCursor}.Offset) {$_; break}}
if (!${*token} -or ${*token} -notmatch '^\$(\*.*)') {return}
foreach($_ in Get-Variable "$($Matches[1])*") {
if ($_.Name[0] -ne '*') {
${*result}.CompletionMatches.Add($(
$$ = "`$$($_.Name)"
[System.Management.Automation.CompletionResult]::new($$, $$, 'Variable', $$)
))
}
}
}
### Input processors
Register-InputCompleter {
### Complete full type name [.x, [x*
param($ast, $tokens, $positionOfCursor, $options)
$token = foreach($_ in $tokens) {if ($_.Extent.EndOffset -eq $positionOfCursor.Offset) {$_; break}}
if (!$token -or $token.TokenFlags -eq 'TypeName') {return}
$line = $positionOfCursor.Line.Substring(0, $positionOfCursor.ColumnNumber - 1)
if ($line -notmatch '\[([\w.*?]+)$') {return}
$m0 = $Matches[0]
$m1 = '*' + $Matches[1] + '*'
[System.Management.Automation.CompletionResult[]]$results = @(
$(foreach($assembly in [System.AppDomain]::CurrentDomain.GetAssemblies()) {
try {
foreach($type in $assembly.GetExportedTypes()) {
$name = $type.FullName
$$ = $name.IndexOf('`')
if ($$ -ge 0) {
$name = $name.Substring(0, $$)
}
if ($name -like $m1) {
if ($$ -ge 0) {
"$name<>"
}
else {
$name
}
}
}
}
catch {
$Error.RemoveAt(0)
}
}) | Sort-Object -Unique | .{process{
$r = if ($_.EndsWith('<>')) {'[' + $_.Substring(0, $_.Length - 2)} else {"[$_"}
[System.Management.Automation.CompletionResult]::new($r, $_, 'Type', $_)
}}
)
[System.Management.Automation.CommandCompletion]::new($results, -1, $positionOfCursor.Offset - $m0.Length, $m0.Length)
}
Register-InputCompleter {
### Complete in comments: help tags or one line code
param($ast, $tokens, $positionOfCursor, $options)
$token = foreach($_ in $tokens) {if ($_.Extent.EndOffset -ge $positionOfCursor.Offset) {$_; break}}
if (!$token -or $token.Kind -ne 'Comment') {return}
$line = $positionOfCursor.Line
$caret = $positionOfCursor.ColumnNumber - 1
if ($caret -and $line[$caret - 1] -eq '#') {return}
# help tags
if ($line -match '^(\s*#*\s*)(\.\w*)' -and $caret -eq $Matches[0].Length) {
$part = $Matches[2]
[System.Management.Automation.CompletionResult[]]$results = @(
'.Synopsis'
'.Description'
'.Parameter'
'.Inputs'
'.Outputs'
'.Notes'
'.Example'
'.Link'
'.Component'
'.Role'
'.Functionality'
'.ForwardHelpTargetName'
'.ForwardHelpCategory'
'.RemoteHelpRunspace'
'.ExternalHelp'
) -like "$part*" | .{process{
[System.Management.Automation.CompletionResult]::new($_, $_, 'Text', $_)
}}
return [System.Management.Automation.CommandCompletion]::new($results, -1, $positionOfCursor.Offset - $part.Length, $part.Length)
}
# one line code
$offset = $positionOfCursor.Offset - $caret
if ($token.Extent.StartOffset -gt $offset) {
$line = $line.Substring($token.Extent.StartOffset - $offset)
$offset = $token.Extent.StartOffset
}
if ($line -match '^(\s*(?:<#)?#*\s*)(.+)') {
$inputScript = ''.PadRight($offset + $Matches[1].Length) + $Matches[2]
TabExpansion2 $inputScript $positionOfCursor.Offset $options
}
}