Created
September 8, 2011 01:14
-
-
Save jstangroome/1202352 to your computer and use it in GitHub Desktop.
Add Ctrl+Shift+E shortcut to expand aliased commands and parameter names in the selected text in the PowerShell ISE to their full form.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just dot-source this in your profile script. | |
function Expand-Alias { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$Code | |
) | |
[ref]$CodeErrors = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Management.Automation.PSParseError] | |
$Tokens = [System.Management.Automation.PSParser]::Tokenize($Code, $CodeErrors) | |
if ($CodeErrors.Value) { | |
Write-Error "$($MyInvocation.MyCommand): Error parsing code" | |
return | |
} | |
$NewCode = New-Object -TypeName System.Text.StringBuilder -ArgumentList ($Code.Length * 2) | |
$Offset = 0 | |
$LastType = $null | |
foreach ($Token in $Tokens) { | |
if ($Token.Start -gt $Offset) { | |
$NewCode.Append($Code.Substring($Offset, $Token.Start - $Offset)) | Out-Null | |
} | |
$Content = $Code.Substring($Token.Start, $Token.Length) | |
if ($Token.Type -eq 'Command') { | |
$LastCommand = Get-Command -Name $Token.Content | | |
Where-Object { $_.Name -eq $Token.content } | |
if ($LastCommand.CommandType -eq 'Alias') { | |
$Content = $LastCommand.Definition | |
} | |
} elseif ($Token.Type -eq 'CommandParameter') { | |
$ParamName = $Token.Content.Substring(1) | |
$AliasedParam = $LastCommand.Parameters.Values | | |
Where-Object { $_.Aliases | Where-Object { $_ -eq $ParamName } } | |
if ($AliasedParam) { | |
$Content = '-' + $AliasedParam.Name | |
} else { | |
$Params = @( | |
$LastCommand.Parameters.Values | | |
Where-Object { $_.Name -like "$ParamName*" } | |
) | |
if ($Params.Length -eq 1) { | |
$Content = '-' + $Params[0].Name | |
} | |
} | |
} | |
$NewCode.Append($Content) | Out-Null | |
$Offset = $Token.Start + $Token.Length | |
$LastType = $Token.Type | |
} | |
$NewCode.Append($Code.Substring($Offset)) | Out-Null | |
$ExpandedCode = $NewCode.ToString() | |
[System.Management.Automation.PSParser]::Tokenize($ExpandedCode, $CodeErrors) | Out-Null | |
if ($CodeErrors.Value) { | |
Write-Error "$($MyInvocation.MyCommand): Error after expanding aliases" | |
return | |
} | |
return $ExpandedCode | |
} | |
function Expand-ISEAlias { | |
$Code = $psISE.CurrentFile.Editor.SelectedText | |
if ($Code.Length) { | |
$Code = Expand-Alias -Code $Code | |
$psISE.CurrentFile.Editor.InsertText($Code) | |
} | |
} | |
if (Get-Variable -Name ExpandIseAliasMenu -ErrorAction SilentlyContinue) { | |
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Remove($ExpandIseAliasMenu) | Out-Null | |
} | |
$ExpandIseAliasMenu = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Expand Alias', { Expand-ISEAlias }, 'Ctrl+Shift+E') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment