@@ -5,27 +5,29 @@ function Set-ScriptExtent {
5
5
<#
6
6
. EXTERNALHELP ..\PowerShellEditorServices.Commands-help.xml
7
7
#>
8
- [CmdletBinding (PositionalBinding = $false , DefaultParameterSetName = ' __AllParameterSets' )]
8
+ [CmdletBinding (PositionalBinding = $false , DefaultParameterSetName = ' __AllParameterSets' )]
9
9
param (
10
- [Parameter (Position = 0 , Mandatory )]
11
- [psobject ]
12
- $Text ,
10
+ [Parameter (Position = 0 , Mandatory )]
11
+ [psobject ] $Text ,
13
12
14
- [Parameter (Mandatory , ParameterSetName = ' AsString' )]
13
+ [Parameter (Mandatory , ParameterSetName = ' AsString' )]
15
14
[switch ]
16
15
$AsString ,
17
16
18
- [Parameter (Mandatory , ParameterSetName = ' AsArray' )]
19
- [switch ]
20
- $AsArray ,
17
+ [Parameter (Mandatory , ParameterSetName = ' AsArray' )]
18
+ [switch ] $AsArray ,
21
19
22
20
[Parameter (ValueFromPipeline , ValueFromPipelineByPropertyName )]
23
- [System.Management.Automation.Language.IScriptExtent ]
24
- $Extent = (Find-Ast - AtCursor).Extent
21
+ [System.Management.Automation.Language.IScriptExtent ] $Extent = (Find-Ast - AtCursor).Extent
25
22
)
26
23
begin {
27
24
$fileContext = $psEditor.GetEditorContext ().CurrentFile
28
- $extentList = [System.Collections.Generic.List [[Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent , Microsoft.PowerShell.EditorServices ]]]::new()
25
+ $descendingComparer = [System.Collections.Generic.Comparer [int ]]::Create{
26
+ param ($x , $y ) return $y.CompareTo ($x )
27
+ }
28
+
29
+ $extentList = [System.Collections.Generic.SortedList [int , System.Management.Automation.Language.IScriptExtent ]]::new(
30
+ $descendingComparer )
29
31
}
30
32
process {
31
33
if ($Extent -isnot [Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent , Microsoft.PowerShell.EditorServices ]) {
@@ -34,11 +36,11 @@ function Set-ScriptExtent {
34
36
$Extent.StartOffset ,
35
37
$Extent.EndOffset )
36
38
}
37
- $extentList.Add ($Extent )
39
+
40
+ $extentList.Add ($Extent.StartOffset , $Extent )
38
41
}
39
- # Currently this kills the pipeline because we need to keep track and edit all extents for position tracking.
40
- # TODO: Consider queueing changes in a static property and adding a PassThru switch.
41
42
end {
43
+ $needsIndentFix = $false
42
44
switch ($PSCmdlet.ParameterSetName ) {
43
45
# Insert text as a single string expression.
44
46
AsString {
@@ -55,41 +57,16 @@ function Set-ScriptExtent {
55
57
}
56
58
}
57
59
58
- foreach ($aExtent in $extentList ) {
60
+ foreach ($kvp in $extentList.GetEnumerator ()) {
61
+ $aExtent = $kvp.Value
59
62
$aText = $Text
60
63
61
64
if ($needsIndentFix ) {
62
- # I'd rather let PSSA handle this when there are more formatting options.
63
65
$indentOffset = ' ' * ($aExtent.StartColumnNumber - 1 )
64
- $aText = $aText -split ' \r?\n' `
65
- -join ([Environment ]::NewLine + $indentOffset )
66
+ $aText = $aText -split ' \r?\n' -join ([Environment ]::NewLine + $indentOffset )
66
67
}
67
- $differenceOffset = $aText.Length - $aExtent.Text.Length
68
- $scriptText = $fileContext.GetText ()
69
68
70
69
$fileContext.InsertText ($aText , $aExtent )
71
-
72
- $newText = $scriptText.Remove ($aExtent.StartOffset , $aExtent.Text.Length ).Insert($aExtent.StartOffset , $aText )
73
-
74
- $timeoutLoop = 0
75
- while ($fileContext.GetText () -ne $newText ) {
76
- Start-Sleep - Milliseconds 30
77
- $timeoutLoop ++
78
-
79
- if ($timeoutLoop -gt 20 ) {
80
- $PSCmdlet.WriteDebug ((' Timed out waiting for change at range {0}, {1}' -f $aExtent.StartOffset ,
81
- $aExtent.EndOffset ))
82
- break
83
- }
84
- }
85
-
86
- if ($differenceOffset ) {
87
- $extentList.ForEach ({
88
- if ($args [0 ].StartOffset -ge $aExtent.EndOffset ) {
89
- $args [0 ].AddOffset($differenceOffset )
90
- }
91
- })
92
- }
93
70
}
94
71
}
95
72
}
0 commit comments