Select groups of code in ISE.

New(er) ISE comes with lots of cool features. One of them is group-matching in editor. This is more than expected from modern editor. But wouldn’t that be handy to have possibility to easily select whole group? If you put cursor at the border of group – matching charter will be highlighted to simplify job. But for me – that’s too much work. How about simple function that would select whole group for me?

function Select-Group {            
    if ($Editor = $psISE.CurrentFile.Editor) {            
        if ($Editor.CanGoToMatch) {            
            $startLine, $startColumn =             
                $Editor.CaretLine, $Editor.CaretColumn            
            $Editor.GoToMatch()            
            $endLine, $endColumn =             
                $Editor.CaretLine, $Editor.CaretColumn            
            $Editor.GoToMatch()            
            # We can't be sure caret was at beg of group...            
            if ($startLine -gt $endLine) {            
                $Editor.Select($endLine, $endColumn,             
                    $startLine, $startColumn)            
            } else {            
                $Editor.Select($startLine, $startColumn,             
                    $endLine, $endColumn)            
            }            
        }            
    }            
}

Now all you have to do is assign hotkey to it and you can select groups at will. Enjoy! Puszczam oczko

Update: quick tip needs quick update – forgot that sometimes (often?) group is all on one line, and column has to be checked in such scenario to avoid exception, so if clause that decides on $Editor.Select syntax should be a bit different:

(($startLine -gt $endLine) -or            
    (($startLine -eq $endLine) -and ($startColumn -gt $endColumn)))

Hope now I haven’t missed any other important exception. Uśmiech

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s