Extending TabExpansion function
EDIT:* Changed "new" to "New-Object"(Thanks Martin and Mow) in the source on PasteBin.com and jEdit IDE comments, "#{{{", "#}}}" are gone.
Moreover, there is a little bit of change on Mow's tab expansion on PowerShell Tab Completion
2, that when caching for loaded assembly type names occur, progress bars(with help from
/\/\o\/\/ again~) are displayed. So when you try out
[System.m[tab], you will see two progress bars during caching. But it actually seems to slow down caching process a bit
EDIT2:* Screenshot: Showing progressbars while caching assembly type names to speed up TabExpansion.
The following happens the first time and the first time only(well that is until you actually delete $global:dtAssemblies) during first Type Name expansion
[Sys[tab]EDIT3:* Modified TabExpansion
http://powershell.pastebin.com/764311.
Thanks, justin for pointing out the regex problem~
Inspired by
/\/\o\/\/'s PowerShell Tab Completion
1 &
2 and was playing around with him on
#powershell on FreeNode, I just decided to expand the tabExpansion function a bit more(with tips from Mow on IRC chatroom for Sorting and expanding partial variable names).
In the second part of Mow's
PowerShell Tab Completion Part 2, Mow has saved cached assembly type names in
$global:dtAssemblies.
Default tabExpansion function does not handle variable names with scope identifier appeneded. So the following will not work
$global:[tab]
$local:[tab]
$script:[tab]
$private:[tab]
So why not just extend tabExpansion function.
While at it, let's support "$variable:", "$env:" and "$function:" as well since they have the same variable name matching patterns.
'(.*^\$)(\w+):(\w*)$' {
$type = $matches[2]; # function, variable, etc.. that are not scopes
$prefix = $matches[1] + $type; # $ + function
$typeName = $matches[3]; # e.g. in '$function:C', value will be 'C'
if ($_ScopeNames -contains $type) {
# Scope Variable Name Expansion
foreach ($scopeVariable in
(Get-Variable "$($typeName)*" -Scope $type Sort-Object name)) {
$prefix + ":" + $scopeVariable.Name
}
} else {
# Type name expansion($function:, $variable, $env: ,etc)
foreach ($t in (Get-ChildItem ($type + ":" + $typeName + '*') Sort-Object name)) {
$prefix + ":" + $t.Name
}
}
break;
}
I am not sure what to call "$function:", "$variable:" and "$env:" so I just called them as "Type".
In the above code snippet, '(.*^\$)(\w+):(\w*) matches "$function:", "$env:" or anything like "$" + "foo" + ":" and variable names with scope name appended are expanded differently from the ones with types.(<- well how should i be expressing this in clearer terms...) Scope names are checked with
if ($_ScopeNames -contains $type) and "$_ScopeNames" is an array containing "global", "local", "script" and "private" and "$_ScopeNames" is declared in "begin{}" block of TabExpansion function like the following (All Regular Expression checks are processed inside "process{}" block.)
begin {
$_Method = [Management.Automation.PSMemberTypes] 'Method,CodeMethod,ScriptMethod,ParameterizedProperty'
$_ScopeNames = @("global", "local", "script", "private")
}
Now, not only the following works,
$global:[tab]
$local:[tab]
$script:[tab]
$private:[tab]
But also, you can expand variable names for
$env:[tab]
$function:[tab]
$variable:[tab]
A simple extension to TabExpansion makes life on Console much less painful... ;)
You can find the full source of my TabExpansion function, which includes sources posted on Mow's blogs "PowerShell Tab Completion
1 &
2", on
powershell.pastebin.comTags :
Monad msh PowerShell