-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathNew-FunctionFromSelectedText.ps1
116 lines (95 loc) · 3.49 KB
/
New-FunctionFromSelectedText.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
function New-FunctionFromSelectedText
{
function Get-VariableExtentText
{
param (
[parameter(ValueFromPipeline)]
[System.Management.Automation.Language.Ast]
$ast
)
begin
{
$VariablesToExclude = '$_', '$true', '$false', '$?', '$$', '${',
'{', '$PSCmdlet', '$PSScriptRoot'
}
process
{
$variableType = [System.Management.Automation.Language.VariableExpressionAst]
$ast.FindAll({$args[0] -is $variableType }, $false).Extent.Text |
foreach {$_ -replace '@', '$' } |
where {($VariablesToExclude -notcontains $_) } |
sort -Unique
}
}
function Get-UnAssignedVariable
{
param ($Scriptblock)
$ScriptblockAst = [scriptblock]::Create($Scriptblock).Ast
$AllVariables = $ScriptblockAst | Get-VariableExtentText
$Assignments = $ScriptblockAst.FindAll({$args[0] -is [System.Management.Automation.Language.AssignmentStatementAst]},$false)
if ($Assignments)
{
$AssignedToVariable = $Assignments.left | Get-VariableExtentText
Compare-Object $AssignedToVariable $AllVariables |
where {$_.sideindicator -like '=>'} |
select -ExpandProperty InputObject
}
else
{
$AllVariables
}
}
function Get-NewFunctionName
{
$MainWindow=@'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Topmost="True"
WindowStartupLocation="CenterScreen"
Title="PSHarp" Height="350" Width="425">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<TextBox x:Name="NameBox" Grid.Row="0" Margin="5"></TextBox>
<Button x:Name="DoneButton" Grid.Row="1" IsDefault="True" Margin="5">OK</Button>
</Grid>
</Window>
'@
$Window = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader ([xml]$MainWindow)))
$NameBox=$Window.FindName("NameBox")
$Button = $Window.FindName("DoneButton")
$Button.add_Click({
param($sender, $data)
$Window.close()
})
[void]$NameBox.Focus()
[void]$Window.ShowDialog()
$Namebox.text
}
$Name = Get-NewFunctionName
$SourceFile = $psISE.CurrentFile
$Scriptblock = $SourceFile.Editor.SelectedText
$Parameters = Get-UnAssignedVariable $Scriptblock
$Definition = "Function $Name `n{"
$Definition += "`n`t param (`n"
$OFS = ",`n`t`t"
$Definition += "`t`t$Parameters"
$Definition += "`n`t)`n"
$Definition += $Scriptblock
$Definition += "`n}"
$ReplaceCommand = "$Name "
foreach ($p in $Parameters)
{
$parameter = $p -replace '\$'
$ReplaceCommand += "-{0} {1} " -f $parameter, $p
}
$HighlightStartLine = $SourceFile.Editor.CaretLine
$HighlightStartColumn = $SourceFile.Editor.CaretColumn
$EscapedText = [regex]::Escape($SourceFile.Editor.SelectedText)
$SourceFile.Editor.Text = $SourceFile.editor.text -replace $EscapedText, $ReplaceCommand
$ISEFile=$psISE.CurrentPowerShellTab.Files.Add()
$psISE.CurrentPowerShellTab.Files.SetSelectedFile($ISEFile)
$ISEFile.Editor.Text = $Definition
}