Skip to content

Commit dac07fe

Browse files
SeeminglySciencedaviwil
authored andcommitted
Update help docs for Commands submodule
This change fixes some mistakes in the help documents for the Commands module and adds additional examples.
1 parent cae9aab commit dac07fe

10 files changed

+143
-65
lines changed

module/docs/ConvertFrom-ScriptExtent.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ ConvertFrom-ScriptExtent -Extent <IScriptExtent[]> [-BufferPosition] [-Start] [-
2626

2727
## DESCRIPTION
2828

29-
Translates IScriptExtent object properties into constructors for some common PowerShell EditorServices types.
29+
The ConvertFrom-ScriptExtent function converts ScriptExtent objects to types used in methods found in the $psEditor API.
3030

3131
## EXAMPLES
3232

3333
### -------------------------- EXAMPLE 1 --------------------------
3434

3535
```powershell
36-
$sb = { Get-ChildItem 'Documents' }
37-
$sb.Ast | Find-Ast { $_ -eq 'Documents' } | ConvertFrom-ScriptExtent -BufferRange
36+
$range = Find-Ast -First { [System.Management.Automation.Language.CommandAst] } |
37+
ConvertFrom-ScriptExtent -BufferRange
38+
39+
$psEditor.GetEditorContext().SetSelection($range)
3840
```
3941

40-
Gets the buffer range of the string expression "Documents".
42+
Convert the extent of the first CommandAst to a BufferRange and use that to select it with the $psEditor API.
4143

4244
## PARAMETERS
4345

@@ -129,18 +131,21 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
129131
130132
### System.Management.Automation.Language.IScriptExtent
131133
132-
You can pipe IScriptExtent objects to be converted.
134+
You can pass ScriptExtent objects to this function. You can also pass objects with a property named "Extent" such as ASTs from Find-Ast or tokens from Get-Token.
133135
134136
## OUTPUTS
135137
136138
### Microsoft.PowerShell.EditorServices.BufferRange
137139
138140
### Microsoft.PowerShell.EditorServices.BufferPosition
139141
140-
This function will return an extent converted to one of the above types depending on switch
141-
choices.
142+
This function will return the converted object of one of the above types depending on parameter switch choices.
142143
143144
## NOTES
144145
145146
## RELATED LINKS
146147
148+
[ConvertTo-ScriptExtent](ConvertTo-ScriptExtent.md)
149+
[Test-ScriptExtent](Test-ScriptExtent.md)
150+
[Set-ScriptExtent](Set-ScriptExtent.md)
151+
[Join-ScriptExtent](Join-ScriptExtent.md)

module/docs/ConvertTo-ScriptExtent.md

+24-22
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Converts position and range objects from PowerShellEditorServices to ScriptExten
1212

1313
## SYNTAX
1414

15-
### ByObject
15+
### ByExtent
1616

1717
```powershell
18-
ConvertTo-ScriptExtent [-InputObject <IScriptExtent>] [<CommonParameters>]
18+
ConvertTo-ScriptExtent [-Extent <IScriptExtent>] [<CommonParameters>]
1919
```
2020

2121
### ByPosition
@@ -41,7 +41,7 @@ ConvertTo-ScriptExtent [-FilePath <String>] [-StartBuffer <BufferPosition>] [-En
4141

4242
## DESCRIPTION
4343

44-
Converts position and range objects from PowerShellEditorServices to ScriptExtent objects.
44+
The ConvertTo-ScriptExtent function can be used to convert any object with position related properties to a ScriptExtent object. You can also specify the parameters directly to manually create ScriptExtent objects.
4545

4646
## EXAMPLES
4747

@@ -51,17 +51,25 @@ Converts position and range objects from PowerShellEditorServices to ScriptExten
5151
$psEditor.GetEditorContext().SelectedRange | ConvertTo-ScriptExtent
5252
```
5353

54-
Returns a InternalScriptExtent object of the currently selected range.
54+
Returns a ScriptExtent object of the currently selected range.
55+
56+
### -------------------------- EXAMPLE 2 --------------------------
57+
58+
```powershell
59+
ConvertTo-ScriptExtent -StartOffset 10 -EndOffset 100
60+
```
61+
62+
Returns a ScriptExtent object from a start and end offset.
5563

5664
## PARAMETERS
5765

58-
### -InputObject
66+
### -Extent
5967

60-
This is here so we can pass script extent objects through without any processing.
68+
Specifies a ScriptExtent object to use as a base to create a new editor context aware ScriptExtent object.
6169

6270
```yaml
6371
Type: IScriptExtent
64-
Parameter Sets: ByObject
72+
Parameter Sets: ByExtent
6573
Aliases:
6674

6775
Required: False
@@ -223,29 +231,23 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
223231
224232
### System.Object
225233
226-
You can pass any object with any of the following properties.
234+
You can pass any object with properties that have position related names. Below is a list of all the property names that can be bound as parameters through the pipeline.
227235
228-
StartLineNumber, StartLine, Line
229-
EndLineNumber, EndLine
230-
StartColumnNumber, StartColumn, Column
231-
EndColumnNumber, EndColumn
232-
StartOffsetNumber, StartOffset, Offset
233-
EndOffsetNumber, EndOffset
234-
StartBuffer, Start
235-
EndBuffer, End
236+
StartLineNumber, StartLine, Line, EndLineNumber, EndLine, StartColumnNumber, StartColumn, Column, EndColumnNumber, EndColumn, StartOffsetNumber, StartOffset, Offset, EndOffsetNumber, EndOffset, StartBuffer, Start, EndBuffer, End
236237
237-
Objects of type IScriptExtent will be passed through with no processing.
238+
You can also pass IScriptExtent objects to be converted to context aware versions.
238239
239240
## OUTPUTS
240241
241-
### System.Management.Automation.Language.IScriptExtent
242-
243-
### System.Management.Automation.Language.InternalScriptExtent
242+
### Microsoft.PowerShell.EditorServices.FullScriptExtent
244243
245-
This function will return any IScriptExtent object passed without processing. Objects created
246-
by this function will be of type InternalScriptExtent.
244+
The converted ScriptExtent object will be returned to the pipeline.
247245
248246
## NOTES
249247
250248
## RELATED LINKS
251249
250+
[ConvertFrom-ScriptExtent](ConvertFrom-ScriptExtent.md)
251+
[Test-ScriptExtent](Test-ScriptExtent.md)
252+
[Set-ScriptExtent](Set-ScriptExtent.md)
253+
[Join-ScriptExtent](Join-ScriptExtent.md)

module/docs/Find-Ast.md

+53-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ schema: 2.0.0
88

99
## SYNOPSIS
1010

11-
Search for a ast within an ast.
11+
Find a specific element in an abstract syntax tree (AST).
1212

1313
## SYNTAX
1414

@@ -27,7 +27,9 @@ Find-Ast [-AtCursor] [<CommonParameters>]
2727

2828
## DESCRIPTION
2929

30-
The Find-Ast function can be used to easily find a specific ast from a starting ast. By default children asts will be searched, but ancestor asts can also be searched by specifying the "Ancestor" switch parameter.
30+
The Find-Ast function can be used to easily find a specific AST within a script file. All ASTs following the inital starting ast will be searched, including those that are not part of the same tree.
31+
32+
The behavior of the search (such as direction and criteria) can be changed with parameters.
3133

3234
Additionally, you can find the Ast closest to the cursor with the "AtCursor" switch parameter.
3335

@@ -76,16 +78,50 @@ Returns all variable expressions used in a dot source expression.
7678
### -------------------------- EXAMPLE 6 --------------------------
7779

7880
```powershell
79-
Find-Ast { 'PowerShellVersion' -eq $_ } | Find-Ast -First | Set-ScriptExtent -Text "'4.0'"
81+
Find-Ast { 'PowerShellVersion' -eq $_ } |
82+
Find-Ast -First |
83+
Set-ScriptExtent -Text '4.0' -AsString
84+
```
85+
86+
This example sets the required PowerShell version in a module manifest to 4.0.
87+
88+
First it finds the AST of the PowerShellVersion manifest field, then finds the first AST directly after it and changes the text to '4.0'. This will not work as is if the field is commented.
89+
90+
### -------------------------- EXAMPLE 7 --------------------------
91+
92+
```powershell
93+
Find-Ast { $_.ArgumentName -eq 'ParameterSetName' -and $_.Argument.Value -eq 'ByPosition' } |
94+
Find-Ast -First -Ancestor { $_ -is [System.Management.Automation.Language.ParameterAst] } |
95+
ForEach-Object { $_.Name.VariablePath.UserPath }
96+
```
97+
98+
This example gets a list of all parameters that belong to the parameter set 'ByPosition'. First it uses the ArgumentName and Argument properties of NamedAttributeArgumentAst to find the ASTs of arguments to the Parameter attribute that declare the the parameter set 'ByPosition'. It then finds the closest parent ParameterAst and retrieves the name from it.
99+
100+
### -------------------------- EXAMPLE 8 --------------------------
101+
102+
```powershell
103+
$companyName = Find-Ast {
104+
$_.Value -eq 'CompanyName' -or
105+
(Find-Ast -Ast $_ -First -Before).Value -eq 'CompanyName'
106+
}
107+
108+
$previousField = $companyName[0] | Find-Ast -First -Before { $_.StringConstantType -eq 'BareWord' }
109+
110+
$companyNameComments = $companyName.Extent, $previousField.Extent |
111+
Join-ScriptExtent |
112+
Get-Token |
113+
Where-Object Kind -eq 'Comment'
114+
115+
$fullManifestElement = $companyNameComments.Extent, $companyName.Extent | Join-ScriptExtent
80116
```
81117

82-
First finds the ast of the PowerShellVersion manifest tag, then finds the first ast after it and changes the text to '4.0'. This will not work as is if the field is commented.
118+
This example shows off ways you can combine the position functions together to get very specific portions of a script file. The result of this example is a ScriptExtent that includes a manifest field, value, and all comments above it.
83119

84120
## PARAMETERS
85121

86122
### -FilterScript
87123

88-
Specifies a ScriptBlock that returns $true if an ast should be returned. Uses $PSItem and $_ like Where-Object. If not specified all asts will be returned.
124+
Specifies a ScriptBlock that returns $true if an AST should be returned. Uses $PSItem and $_ like Where-Object. If not specified all ASTs will be returned.
89125

90126
```yaml
91127
Type: ScriptBlock
@@ -101,7 +137,7 @@ Accept wildcard characters: False
101137
102138
### -Ast
103139
104-
Specifies the starting ast. The default is the ast of the current file in PowerShell Editor Services.
140+
Specifies the starting AST. The default is the AST of the current file in PowerShell Editor Services.
105141
106142
```yaml
107143
Type: Ast
@@ -133,7 +169,7 @@ Accept wildcard characters: False
133169
134170
### -Family
135171
136-
If specified only children of the starting ast will be searched. If specified with the "Before" parameter then only ancestors will be searched.
172+
If specified only children of the starting AST will be searched. If specified with the "Before" parameter then only ancestors will be searched.
137173
138174
```yaml
139175
Type: SwitchParameter
@@ -149,7 +185,7 @@ Accept wildcard characters: False
149185
150186
### -First
151187
152-
If specified will return only the first result. This will be the closest ast that matches.
188+
If specified will return only the first result. This will be the closest AST that matches.
153189
154190
```yaml
155191
Type: SwitchParameter
@@ -165,7 +201,7 @@ Accept wildcard characters: False
165201
166202
### -Last
167203
168-
If specified will return only the last result. This will be the furthest ast that matches.
204+
If specified will return only the last result. This will be the furthest AST that matches.
169205
170206
```yaml
171207
Type: SwitchParameter
@@ -181,7 +217,7 @@ Accept wildcard characters: False
181217
182218
### -Ancestor
183219
184-
If specified will only search ancestors of the starting ast. This is a convenience parameter that acts the same as the "Family" and "Before" parameters when used together.
220+
If specified will only search ancestors of the starting AST. This is a convenience parameter that acts the same as the "Family" and "Before" parameters when used together.
185221
186222
```yaml
187223
Type: SwitchParameter
@@ -197,7 +233,7 @@ Accept wildcard characters: False
197233
198234
### -IncludeStartingAst
199235
200-
If specified the starting ast will be included if matched.
236+
If specified the starting AST will be included if matched.
201237
202238
```yaml
203239
Type: SwitchParameter
@@ -213,7 +249,7 @@ Accept wildcard characters: False
213249
214250
### -AtCursor
215251
216-
If specified, this function will return the smallest ast that the cursor is within. Requires PowerShell Editor Services.
252+
If specified, this function will return the smallest AST that the cursor is within.
217253
218254
```yaml
219255
Type: SwitchParameter
@@ -235,15 +271,18 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
235271
236272
### System.Management.Automation.Language.Ast
237273
238-
You can pass asts to search to this function.
274+
You can pass ASTs to search to this function.
239275
240276
## OUTPUTS
241277
242278
### System.Management.Automation.Language.Ast
243279
244-
Asts that match the criteria will be returned to the pipeline.
280+
ASTs that match the criteria will be returned to the pipeline.
245281
246282
## NOTES
247283
248284
## RELATED LINKS
249285
286+
[Get-Token](Get-Token.md)
287+
[Set-ScriptExtent](Set-ScriptExtent.md)
288+
[ConvertTo-ScriptExtent](ConvertTo-ScriptExtent.md)

module/docs/Import-EditorCommand.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ schema: 2.0.0
88

99
## SYNOPSIS
1010

11-
Imports commands with the PSEditorCommand attribute into PowerShell Editor Services.
11+
Imports commands with the EditorCommand attribute into PowerShell Editor Services.
1212

1313
## SYNTAX
1414

1515
### ByModule
1616

1717
```powershell
18-
Import-EditorCommand [-Module] <PSModuleInfo[]> [-Force] [-PassThru] [<CommonParameters>]
18+
Import-EditorCommand [-Module] <string[]> [-Force] [-PassThru] [<CommonParameters>]
1919
```
2020

2121
### ByCommand
2222

2323
```powershell
24-
Import-EditorCommand [-Command] <CommandInfo[]> [-Force] [-PassThru] [<CommonParameters>]
24+
Import-EditorCommand [-Command] <string[]> [-Force] [-PassThru] [<CommonParameters>]
2525
```
2626

2727
## DESCRIPTION
@@ -30,6 +30,8 @@ The Import-EditorCommand function will search the specified module for functions
3030

3131
Alternatively, you can specify command info objects (like those from the Get-Command cmdlet) to be processed directly.
3232

33+
To tag a command as an editor command, attach the attribute 'Microsoft.PowerShell.EditorServices.Extensions.EditorCommandAttribute' to the function like you would with 'CmdletBindingAttribute'. The attribute accepts the named parameters 'Name', 'DisplayName', and 'SuppressOutput'.
34+
3335
## EXAMPLES
3436

3537
### -------------------------- EXAMPLE 1 --------------------------
@@ -48,6 +50,23 @@ Get-Command *Editor* | Import-EditorCommand -PassThru
4850

4951
Registers all editor commands that contain "Editor" in the name and return all successful imports.
5052

53+
### -------------------------- EXAMPLE 3 --------------------------
54+
55+
```powershell
56+
function Invoke-MyEditorCommand {
57+
[CmdletBinding()]
58+
[Microsoft.PowerShell.EditorServices.Extensions.EditorCommand(DisplayName='My Command', SuppressOutput)]
59+
param()
60+
end {
61+
ConvertTo-ScriptExtent -Offset 0 | Set-ScriptExtent -Text 'My Command!'
62+
}
63+
}
64+
65+
Get-Command Invoke-MyEditorCommand | Import-EditorCommand
66+
```
67+
68+
This example declares the function Invoke-MyEditorCommand with the EditorCommand attribute and then imports it as an editor command.
69+
5170
## PARAMETERS
5271

5372
### -Module
@@ -68,7 +87,7 @@ Accept wildcard characters: False
6887
6988
### -Command
7089
71-
Specifies the functions to register as editor commands. If the function does not have the PSEditorCommand attribute it will be ignored.
90+
Specifies the functions to register as editor commands. If the function does not have the EditorCommand attribute it will be ignored.
7291
7392
```yaml
7493
Type: string[]
@@ -135,3 +154,5 @@ will be returned. This function does not output to the pipeline otherwise.
135154
136155
## RELATED LINKS
137156
157+
[Register-EditorCommand](Register-EditorCommand.md)
158+
[Unregister-EditorCommand](Unregister-EditorCommand.md)

0 commit comments

Comments
 (0)