-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSFunctionInfo.psm1
182 lines (152 loc) · 6.91 KB
/
PSFunctionInfo.psm1
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
Get-ChildItem $PSScriptRoot\functions\*.ps1 | ForEach-Object {
. $_.FullName
}
#load defaults if found
$defaults = Join-Path $home -ChildPath psfunctioninfo-defaults.json
if (Test-Path -path $defaults) {
$d = Get-Content -Path $defaults | ConvertFrom-JSON
$d.PSObject.properties | Foreach-Object {
$global:PSDefaultParameterValues["New-PSFunctionInfo:$($_.name)"] = $_.value
}
}
#Add VSCode Shortcuts
if ($host.name -eq 'Visual Studio Code Host') {
$global:PSDefaultParameterValues["New-PSFunctionInfo:Path"] = {$PSEditor.GetEditorContext().CurrentFile.Path}
#create an argument completer for the Name parameter
Register-ArgumentCompleter -CommandName New-PSFunctionInfo -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $WordToComplete, $commandAst, $fakeBoundParameter)
#PowerShell code to populate $WordToComplete
$ASTTokens = $PSEditor.GetEditorContext().CurrentFile.tokens
$NameList = @()
for ($i=0;$i -lt $ASTTokens.count;$i++) {
if ($ASTTokens[$i].text -eq 'function') {
$NameList+= $ASTTokens[$i+1].text
}
}
$NameList | Where-Object {$_ -like "$WordToComplete*"} |
ForEach-Object {
# completion text,listItem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
elseif ($host.name -eq 'Windows PowerShell ISE Host') {
#create shortcut for the ISE
$global:PSDefaultParameterValues["New-PSFunctionInfo:Path"] = {$PSIse.CurrentFile.FullPath}
#add a menu item
$sb = {
Function PickList {
# A WPF function picker for the PowerShell ISE
Param([string[]]$Name)
$title = "New-PSFunctionInfo"
$form = New-Object System.Windows.Window
#define what it looks like
$form.Title = $Title
$form.Height = 200
$form.Width = 300
$form.WindowStartupLocation = [System.Windows.WindowStartupLocation]::CenterScreen
$grid = New-Object System.Windows.Controls.Grid
$label = New-Object System.Windows.Controls.Label
$label.Content = "Select a function from the list and click OK."
$label.HorizontalAlignment = "left"
$label.VerticalAlignment = "top"
$grid.AddChild($label)
$combo = New-Object System.Windows.Controls.ListBox
$combo.AllowDrop = $true
$combo.Width = 160
$combo.Height = 75
$combo.HorizontalAlignment = "left"
$combo.VerticalAlignment = "top"
$combo.Margin = "15,25,0,0"
foreach ($item in $Name) {
[void]$combo.items.Add($item)
}
$combo.SelectedIndex = 0
$grid.AddChild($combo)
#initialize an array to hold all processed objects
$ok = New-Object System.windows.controls.button
$ok.Content = "_OK"
$ok.IsDefault = $True
$ok.Width = 75
$ok.Height = 25
$ok.HorizontalAlignment = "left"
$ok.Margin = "25,100,0,0"
$OK.Add_Click( {
$global:SelectedPickItem = $combo.SelectedItem
#$combo.SelectedItem | Out-Default
$form.Close()
})
$grid.AddChild($ok)
$cancel = New-Object System.Windows.Controls.Button
$cancel.Content = "_Cancel"
$cancel.Width = 75
$cancel.Height = 25
#$cancel.HorizontalAlignment = "right"
$cancel.Margin = "150,100,0,0"
$cancel.Add_Click( {
$form.Close()
})
$grid.AddChild($cancel)
$form.AddChild($grid)
[void]($form.ShowDialog())
}
if ($PSIse.CurrentFile.IsSaved) {
$Path = $PSIse.CurrentFile.FullPath
New-Variable ASTTokens -Force
New-Variable astErr -Force
$AST = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$ASTTokens, [ref]$astErr)
$file = [System.Collections.Generic.list[string[]]]::new()
Get-Content $path | ForEach-Object { $file.Add($_) }
$name = @()
for ($i = 0; $i -lt $ASTTokens.count; $i++) {
if ($ASTTokens[$i].text -eq 'function') {
$name += $ASTTokens[$i + 1].text
}
} #for
if ($name) {
Remove-Variable -Name SelectedPickItem -ErrorAction SilentlyContinue
PickList -name $name
#| Out-GridView -Title "Select a function" -OutputMode Single
if ($SelectedPickItem) {
New-PSFunctionInfo -Name $SelectedPickItem -path $path
$idx = $file.findIndex( { $args[0] -match "[Ff]unction(\s+)$SelectedPickItem" })
$idx++
#save and re-open the file
[void]$PSIse.CurrentPowerShellTab.files.Remove($PSIse.CurrentFile)
[void]$PSIse.CurrentPowerShellTab.Files.Add($path)
$r = $PSIse.CurrentPowerShellTab.Files.where( { $_.FullPath -eq $path })
$r.Editor.Focus()
if ($idx -ge 0) {
$r.Editor.SetCaretPosition($idx, 1)
$r.Editor.SelectCaretLine()
$r.Editor.EnsureVisible($idx)
}
}
} #if names found
else {
[System.Windows.MessageBox]::Show("No matching functions found in $Path", "New-PSFunctionInfo", "OK", "Information")
}
} #if saved
else {
[System.Windows.MessageBox]::Show("Please save your file first:$Path and then try again.", "New-PSFunctionInfo", "OK", "Warning")
}
}
$PSIse.CurrentPowerShellTab.AddOnsMenu.Submenus.add("New-PSFunctionInfo", $sb, $Null)
}
#create an argument completer
Register-ArgumentCompleter -CommandName Get-PSFunctionInfo -ParameterName FunctionName -ScriptBlock {
param($commandName, $parameterName, $WordToComplete, $commandAst, $fakeBoundParameter)
Get-ChildItem -path "Function:\$WordToComplete*" |
ForEach-Object {
# completion text,listItem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new($_.name, $_.name, 'ParameterValue', $_.name)
}
}
Register-ArgumentCompleter -CommandName Get-PSFunctionInfo -ParameterName Tag -ScriptBlock {
param($commandName, $parameterName, $WordToComplete, $commandAst, $fakeBoundParameter)
(Get-PSFunctionInfoTag).where({$_ -like "$WordToComplete*"}) |
ForEach-Object {
# completion text,listItem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}