forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-ScriptExtent.ps1
72 lines (62 loc) · 2.58 KB
/
Set-ScriptExtent.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
function Set-ScriptExtent {
<#
.EXTERNALHELP ..\PowerShellEditorServices.Commands-help.xml
#>
[CmdletBinding(PositionalBinding = $false, DefaultParameterSetName = '__AllParameterSets')]
param(
[Parameter(Position = 0, Mandatory)]
[psobject] $Text,
[Parameter(Mandatory, ParameterSetName = 'AsString')]
[switch]
$AsString,
[Parameter(Mandatory, ParameterSetName = 'AsArray')]
[switch] $AsArray,
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[System.Management.Automation.Language.IScriptExtent] $Extent = (Find-Ast -AtCursor).Extent
)
begin {
$fileContext = $psEditor.GetEditorContext().CurrentFile
$descendingComparer = [System.Collections.Generic.Comparer[int]]::Create{
param($x, $y) return $y.CompareTo($x)
}
$extentList = [System.Collections.Generic.SortedList[int, System.Management.Automation.Language.IScriptExtent]]::new(
$descendingComparer)
}
process {
if ($Extent -isnot [Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent, Microsoft.PowerShell.EditorServices]) {
$Extent = [Microsoft.PowerShell.EditorServices.Extensions.FileScriptExtent, Microsoft.PowerShell.EditorServices]::FromOffsets(
$fileContext,
$Extent.StartOffset,
$Extent.EndOffset)
}
$extentList.Add($Extent.StartOffset, $Extent)
}
end {
$needsIndentFix = $false
switch ($PSCmdlet.ParameterSetName) {
# Insert text as a single string expression.
AsString {
$Text = "'{0}'" -f $Text.Replace("'", "''")
}
# Create a string expression for each line, separated by a comma.
AsArray {
$newLine = [Environment]::NewLine
$Text = "'" + ($Text.Replace("'", "''") -split '\r?\n' -join "',$newLine'") + "'"
if ($Text.Split("`n", [StringSplitOptions]::RemoveEmptyEntries).Count -gt 1) {
$needsIndentFix = $true
}
}
}
foreach ($kvp in $extentList.GetEnumerator()) {
$aExtent = $kvp.Value
$aText = $Text
if ($needsIndentFix) {
$indentOffset = ' ' * ($aExtent.StartColumnNumber - 1)
$aText = $aText -split '\r?\n' -join ([Environment]::NewLine + $indentOffset)
}
$fileContext.InsertText($aText, $aExtent)
}
}
}