::::::::: PowerShell :::::::::
Sunday, June 04, 2006
  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.com

Tags :
 
Comments:
Great job from yourself and MoW!

Just one thing... shouldn't the regex in the where clause on line 80 be changed from: '^[ge]et_'

To: '^[gs]et_'

Or perhaps: '^([gs]et|op)_'
 
Hello there, Justin~ Thanks a lot for the feedback there.

The RegEx, '^[gs]et_', you spotted is actually what's included in the default TabExpansion function that comes with PowerShell ;)

I think you have just found a bug~ Would you file a bug for this on Microsoft Connect?

I have fixed it as you have suggested(Well, i wanted go with '^([gs]et|op|remove|add)_' to go one step further but it's hard for me to add event handlers without "add|remove_" functions ;))

The updated Function is on
http://powershell.pastebin.com/764311
 
The link http://powershell.pastebin.com/764311 does not show any code as of today. Where else can I get it from?
 
How are you doing there, Alexander?

I haven't been able to update the tab expansion function for quite awhile and Mow(http://mow001.blogspot.com) has updated the expansion function on http://mow001.blogspot.com/2006/10/powershell-tabcompletion-part-5.html
 
Post a Comment

Links to this post:

Create a Link



<< Home
Let's get lazy with PowerShell!

Name:
Location: Flushing, NY, United States

Experimenting with a different format of blogs...

Links
ARCHIVES
10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /


Powered by Blogger