ISE Functions – in a Module

The module linked below adds three functions to the ISE editor:

  • Indent
  • Outdent
  • A primitive interpretation of the Xedit “All” macro

To try it out just copy the folder from here: CJW-ISE-Functions.zip  and place it under your Modules folder.  If you don’t have a Modules folder yet, see This Post on the PowerShell Team Blog, or This Post on Thomas’s Blog.

Here’s a screenshot of the “All” function:

 

As you’d expect, the Indent and Outdent functions do exactly what they say; default is to in/out-dent by 4 spaces.  Indent is mapped to Ctrl-F12 and Outdent to Ctrl-F11.

I’m never quite sure if SkyDrive is going to work, so here’s the CJW-ISE-Functions.psm1 file.  This was created with the Module Module!:

Cheers,

Chris

 

  1 ################################################################################
  2 
  3 Function Get-ISEMatchingLines {
  4 <#
  5 .Synopsis
  6     Displays lines matching the given pattern from the currently active ISE Editor window
  7 .Description
  8     This is a poor-man's-version of the Xedit "ALL" command.  All lines matching the given parameter
  9     are displayed as output
 10 
 11 .Parameter Pattern
 12 .Example
 13     Get-ISEMatchingLines "# Todo"
 14 .ReturnValue
 15     Output lines matching pattern
 16 .Link
 17 
 18 .Notes
 19  NAME:      Get-ISEMatchingLines
 20  AUTHOR:    Chris Warwick
 21  LASTEDIT:  01/05/2009 22:30:12
 22 #Requires -Version 2.0
 23 #>
 24 
 25 [CmdletBinding(SupportsShouldProcess=$False, SupportsTransactions=$False, ConfirmImpact='None')]
 26 
 27 Param([Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][String]$Pattern)
 28 
 29     Process{
 30         If ($host.Name -ne 'Windows PowerShell ISE Host'){Throw 'This function only runs in PowerShell ISE'}
 31         $psISE.CurrentOpenedFile.Editor.Text -split "`n"|Select-String -pattern $Pattern
 32     }
 33 } 
 34 
 35 ################################################################################
 36 
 37 Function Indent-ISESelection {
 38 <#
 39 .Synopsis
 40     Indents the current editor selection by the specified number of characters
 41 .Description
 42     The editor current selection is indented by having a given number of spaces (default=4) 
 43     inserted before each line in the selection.  Note that if the selection does not start
 44     on a line-break boundary this cannot be detected and indenting will occur from the start
 45     of the selection irrespective of its actual place in the line.
 46 
 47 .Parameter Spaces
 48 .Example
 49     Indent-ISESelection 8
 50 .ReturnValue
 51     Selected lines indented
 52 .Link
 53 
 54 .Notes
 55  NAME:      Indent-ISESelection
 56  AUTHOR:    Chris Warwick
 57  LASTEDIT:  01/05/2009 22:30:12
 58 #Requires -Version 2.0
 59 #>
 60 
 61 [CmdletBinding(SupportsShouldProcess=$False, SupportsTransactions=$False, ConfirmImpact='None')]
 62 
 63 Param([Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$false)][Int]$indent=4)
 64 
 65     Process{
 66         If ($host.Name -ne 'Windows PowerShell ISE Host'){Throw 'This function only runs in PowerShell ISE'}
 67         $psISE.CurrentOpenedFile.Editor.InsertText(
 68             ($psISE.CurrentOpenedFile.Editor.SelectedText -split "`n"|%{$_ -Replace '^', (' '*$Indent)}) -join "`n"
 69         )
 70     }
 71 } 
 72 
 73 ################################################################################
 74 
 75 Function Outdent-ISESelection {
 76 <#
 77 .Synopsis
 78     Outdents the current editor selection by the specified number of characters
 79 .Description
 80     The editor current selection is outdented by having a given number of spaces (default=4) 
 81     removed before each line in the selection.  Note that if the selection does not start
 82     on a line-break boundary this cannot be detected and outdenting will occur from the start
 83     of the selection irrespective of its actual place in the line.
 84     
 85     The outdenting process will only remove whitespace characters from the start of each line.
 86     If whitespace characters run out then no shifting will occur
 87 
 88 .Parameter Spaces
 89 .Example
 90     Outdent-ISESelection 8
 91 .ReturnValue
 92     Selected lines outdented
 93 .Link
 94 
 95 .Notes
 96  NAME:      Outdent-ISESelection
 97  AUTHOR:    Chris Warwick
 98  LASTEDIT:  01/05/2009 22:30:12
 99 #Requires -Version 2.0
100 #>
101 
102 [CmdletBinding(SupportsShouldProcess=$False, SupportsTransactions=$False, ConfirmImpact='None')]
103 
104 Param([Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$false)][Int]$Outdent=4)
105 
106     Process{
107         If ($host.Name -ne 'Windows PowerShell ISE Host'){Throw 'This function only runs in PowerShell ISE'}
108         $psISE.CurrentOpenedFile.Editor.InsertText(
109             ($psISE.CurrentOpenedFile.Editor.SelectedText -split "`n"|%{$_ -Replace "^\s{0,$Outdent}"}) -join "`n"
110         )
111     }
112 } 
113 
114 ################################################################################
115             
116 $Null=$psISE.CustomMenu.Submenus.Add('Indent', {Indent-ISESelection}, 'Ctrl+F12')
117 $Null=$psISE.CustomMenu.Submenus.Add('Outdent', {Outdent-ISESelection}, 'Ctrl+F11')
118 
119 Export-ModuleMember Get-ISEMatchingLines, Indent-ISESelection, Outdent-ISESelection
120 
121 Set-Alias All Get-ISEMatchingLines -Scope Global
122 Set-Alias Indent Indent-ISESelection -Scope Global
123 Set-Alias Outdent Outdent-ISESelection -Scope Global
124 
125 ################################################################################

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