PowerShell ISE Addon: Yet another ISE function explorer

by Ravikanth on May 23, 2012

Fellow PowerShell MVP Jeff Hicks posted a nice script to find all functions and filters in an ISE script and show the same in a GUI dialog. Jeff used Regex to figure out the function and filter details. This got me thinking. Why can’t we do the same using PowerShell ISE scripting object model?

UPDATE: As Jeff pointed out in the comments, this works only with PowerShell 3.0. There is no -Passthru switch in PowerShell 2.0 for Out-GridView cmdlet.

In fact, the answer is yes.

$ScriptBlock = {
    $currentLine = $psISE.CurrentFile.Editor.CaretLine
    $currentColumn = $psISE.CurrentFile.Editor.CaretColumn
    $errors = $null
    $functions = [system.management.automation.psparser]::Tokenize($psISE.CurrentFile.Editor.Text, [ref]$errors) | `
                Where-Object {(($_.Content -Eq "Function") -or ($_.Content -eq "Filter")) -and $_.Type -eq "Keyword" } | `
                Select-Object @{"Name"="FunctionName"; "Expression"={
                        $psISE.CurrentFile.Editor.Select($_.StartLine, $_.EndColumn+1,$_.StartLine,$psISE.CurrentFile.Editor.GetLineLength($_.StartLine))
                        $psISE.CurrentFile.Editor.SelectedText
                    }},Content,StartLine, StartColumn
    $psISE.CurrentFile.Editor.SetCaretPosition($currentLine,$currentColumn)
    $selectedLine = $functions | Out-GridView -PassThru
    if ($selectedLine) { $psISE.CurrentFile.Editor.SetCaretPosition($selectedLine.StartLine,$selectedLine.StartColumn) }
}
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("ISE Function _Explorer",$scriptBlock,"Alt+E")

In the above solution, I am using PowerShell tokenizer to find out the functions and filters.

[system.management.automation.psparser]::Tokenize($psISE.CurrentFile.Editor.Text, [ref]$errors) | `
                Where-Object {(($_.Content -Eq "Function") -or ($_.Content -eq "Filter")) -and $_.Type -eq "Keyword" }

However, this won’t give us the names of the functions or filters. We need some additional work on that and I used the PowerShell ISE object model here. There is a small workaround. Instead of RegEx, I just select the text from each token given by the parser.

Select-Object @{"Name"="FunctionName"; "Expression"={
                        $psISE.CurrentFile.Editor.Select($_.StartLine, $_.EndColumn+1,$_.StartLine,$psISE.CurrentFile.Editor.GetLineLength($_.StartLine))
                        $psISE.CurrentFile.Editor.SelectedText
                    }},Content,StartLine, StartColumn

This is it. After this, I used Out-GridView to be able to select the function I need to go to.

ISE Function Explorer

Now, we can just select the function we need to go to and click OK.

This post is to just show how powerful ISE object model is and to demonstrate that — in PowerShell — there is always more than one way to do the same thing.

 

Ravikanth

A technology enthu and a Windows PowerShell MVP working on SharePoint solutions at Dell Inc. Has deep interests in Windows Server OS & Virtualization.

More Posts - Website - Twitter - Facebook

  • http://twitter.com/JeffHicks Jeffery Hicks

    Does your solution take into account situations where Function or Filter might be used on comment based help? Or where a function has been commented out?

  • http://twitter.com/JeffHicks Jeffery Hicks

    Guess I could have tested first. Very nice. Although Out-GridView doesn’t have a -passthru parameter in PowerShell v2.

  • http://www.ravichaganti.com/blog Ravikanth

     Oh, yes. That is a good point. I should update the post.

  • http://www.ravichaganti.com/blog Ravikanth

     Yes. Using Parser takes care of it.

  • Pingback: PowerShell ISE Addon: ISE Function Explorer using the PowerShell 3.0 parser — Ravikanth Chaganti

Previous post:

Next post: