PoshCode Logo PowerShell Code Repository

ISE-Lines by Scott Hardwick 22 months ago (modification of post by Bernd Kriszio view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1727"></script>download | new post

ISE-Lines module v 1.2
Updated for PowerShell 2.0 RTM
Original authors Poetter & Kriszio

Provides Line cmdlets for working with ISE

Duplicate-Line – Duplicates current line
Conflate-Line – Conflates current and next line
MoveUp-Line – Moves current line up
MoveDown-Line – Moves current line down
Delete-TrailingBlanks – Deletes trailing blanks in the whole script
Delete-BlankLines – Remove blank lines from the selected text

  1. #requires -version 2.0
  2. ## ISE-Lines module v 1.2
  3. ##############################################################################################################
  4. ## Provides Line cmdlets for working with ISE
  5. ## Duplicate-Line - Duplicates current line
  6. ## Conflate-Line - Conflates current and next line
  7. ## MoveUp-Line - Moves current line up
  8. ## MoveDown-Line - Moves current line down
  9. ## Delete-TrailingBlanks - Deletes trailing blanks in the whole script
  10. ##
  11. ## Usage within ISE or Microsoft.PowershellISE_profile.ps1:
  12. ## Import-Module ISE-Lines.psm1
  13. ##
  14. ##############################################################################################################
  15. ## History:
  16. ## 1.2 - Minor alterations to work with PowerShell 2.0 RTM and Documentation updates (Hardwick)
  17. ##       Include Delete-BlankLines function (author Kriszio I believe)
  18. ## 1.1 - Bugfix and remove line continuation character while joining for Conflate-Line function (Kriszio)
  19. ## 1.0 - Initial release (Poetter)
  20. ##############################################################################################################
  21.  
  22. ## Duplicate-Line
  23. ##############################################################################################################
  24. ## Duplicates current line
  25. ##############################################################################################################
  26. function Duplicate-Line
  27. {
  28.     $editor = $psISE.CurrentFile.Editor
  29.     $caretLine = $editor.CaretLine
  30.     $caretColumn = $editor.CaretColumn
  31.     $text = $editor.Text.Split("`n")
  32.     $line = $text[$caretLine -1]
  33.     $newText = $text[0..($caretLine -1)]
  34.     $newText += $line
  35.     $newText += $text[$caretLine..($text.Count -1)]
  36.     $editor.Text = [String]::Join("`n", $newText)
  37.     $editor.SetCaretPosition($caretLine, $caretColumn)
  38. }
  39.  
  40. ## Conflate-Line
  41. ##############################################################################################################
  42. ## Conflates current and next line
  43. ## v 1.1 fixed bug on last but one line and remove line continuation character while joining
  44. ##############################################################################################################
  45. function Conflate-Line
  46. {
  47.     $editor = $psISE.CurrentFile.Editor
  48.     $caretLine = $editor.CaretLine
  49.     $caretColumn = $editor.CaretColumn
  50.     $text = $editor.Text.Split("`n")
  51.     if ( $caretLine -ne $text.Count )
  52.     {
  53.         $line = $text[$caretLine -1] + $text[$caretLine] -replace ("(``)?`r", "")
  54.         $newText = @()
  55.         if ( $caretLine -gt 1 )
  56.         {
  57.             $newText = $text[0..($caretLine -2)]
  58.         }
  59.         $newText += $line
  60.         if ( $caretLine -ne $text.Count - 1)
  61.         {
  62.             $newText += $text[($caretLine +1)..($text.Count -1)]
  63.         }
  64.         $editor.Text = [String]::Join("`n", $newText)
  65.         $editor.SetCaretPosition($caretLine, $caretColumn)
  66.     }
  67. }
  68.  
  69. ## MoveUp-Line
  70. ##############################################################################################################
  71. ## Moves current line up
  72. ##############################################################################################################
  73. function MoveUp-Line
  74. {
  75.     $editor = $psISE.CurrentFile.Editor
  76.     $caretLine = $editor.CaretLine
  77.     if ( $caretLine -ne 1 )
  78.     {
  79.         $caretColumn = $editor.CaretColumn
  80.         $text = $editor.Text.Split("`n")
  81.         $line = $text[$caretLine -1]
  82.         $lineBefore = $text[$caretLine -2]
  83.         $newText = @()
  84.         if ( $caretLine -gt 2 )
  85.         {
  86.             $newText = $text[0..($caretLine -3)]
  87.         }
  88.         $newText += $line
  89.         $newText += $lineBefore
  90.         if ( $caretLine -ne $text.Count )
  91.         {
  92.             $newText += $text[$caretLine..($text.Count -1)]
  93.         }
  94.         $editor.Text = [String]::Join("`n", $newText)
  95.         $editor.SetCaretPosition($caretLine - 1, $caretColumn)
  96.     }
  97. }
  98.  
  99. ## MoveDown-Line
  100. ##############################################################################################################
  101. ## Moves current line down
  102. ##############################################################################################################
  103. function MoveDown-Line
  104. {
  105.     $editor = $psISE.CurrentFile.Editor
  106.     $caretLine = $editor.CaretLine
  107.     $caretColumn = $editor.CaretColumn
  108.     $text = $editor.Text.Split("`n")
  109.     if ( $caretLine -ne $text.Count )
  110.     {
  111.         $line = $text[$caretLine -1]
  112.         $lineAfter = $text[$caretLine]
  113.         $newText = @()
  114.         if ( $caretLine -ne 1 )
  115.         {
  116.             $newText = $text[0..($caretLine -2)]
  117.         }
  118.         $newText += $lineAfter
  119.         $newText += $line
  120.         if ( $caretLine -lt $text.Count -1 )
  121.         {
  122.             $newText += $text[($caretLine +1)..($text.Count -1)]
  123.         }
  124.         $editor.Text = [String]::Join("`n", $newText)
  125.         $editor.SetCaretPosition($caretLine +1, $caretColumn)
  126.     }
  127. }
  128.  
  129. ## Delete-TrailingBlanks
  130. ##############################################################################################################
  131. ## Deletes trailing blanks in the whole script
  132. ##############################################################################################################
  133. function Delete-TrailingBlanks
  134. {
  135.     $editor = $psISE.CurrentFile.Editor
  136.     $caretLine = $editor.CaretLine
  137.     $newText = @()
  138.     foreach ( $line in $editor.Text.Split("`n") )
  139.     {
  140.         $newText += $line -replace ("\s+$", "")
  141.     }
  142.     $editor.Text = [String]::Join("`n", $newText)
  143.     $editor.SetCaretPosition($caretLine, 1)
  144. }
  145.  
  146. ## Delete-BlankLines
  147. ##############################################################################################################
  148. ## Deletes blank lines from the selected text
  149. ##############################################################################################################
  150. function Delete-BlankLines
  151. {
  152. # Code from the ISECream Archive (http://psisecream.codeplex.com/), originally named Remove-IseEmptyLines
  153.  
  154.     # Todo it would be nice to keep the caretposition, but I found no easy way
  155.     # of course you can split the string into an array of lines
  156.     $editor = $psISE.CurrentFile.Editor
  157.     #$caretLine = $editor.CaretLine
  158.     if ($editor.SelectedText)
  159.     {
  160.         Write-Host 'selected'
  161.         $editor.InsertText(($editor.SelectedText -replace '(?m)\s*$', ''))
  162.     }
  163.     else
  164.     {
  165.         $editor.Text = $editor.Text -replace '(?m)\s*$', ''
  166.     }
  167.     $editor.SetCaretPosition(1, 1)
  168. }
  169.  
  170.  
  171. ##############################################################################################################
  172. ## Inserts a submenu Lines to ISE's Custum Menu
  173. ## Inserts command Duplicate Line to submenu Lines
  174. ## Inserts command Conflate Line Selected to submenu Lines
  175. ## Inserts command Move Up Line to submenu Lines
  176. ## Inserts command Move Down Line to submenu Lines
  177. ## Inserts command Delete Trailing Blanks to submenu Lines
  178. ## Inserts command Delete Blank Lines to submenu Lines
  179. ##############################################################################################################
  180. if (-not( $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus | where { $_.DisplayName -eq "Lines" } ) )
  181. {
  182.         $linesMenu = $psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add("_Lines",$null,$null)
  183.         $null = $linesMenu.Submenus.Add("Duplicate Line", {Duplicate-Line}, "Ctrl+Alt+D")
  184.         $null = $linesMenu.Submenus.Add("Conflate Line", {Conflate-Line}, "Ctrl+Alt+J")
  185.         $null = $linesMenu.Submenus.Add("Move Up Line", {MoveUp-Line}, "Ctrl+Shift+Up")
  186.         $null = $linesMenu.Submenus.Add("Move Down Line", {MoveDown-Line}, "Ctrl+Shift+Down")
  187.         $null = $linesMenu.Submenus.Add("Delete Trailing Blanks", {Delete-TrailingBlanks}, "Ctrl+Shift+Del")
  188.         $null = $linesMenu.Submenus.Add("Delete Blank Lines", {Delete-BlankLines}, "Ctrl+Shift+End")
  189. }
  190.  
  191. # If you are using IsePack (http://code.msdn.microsoft.com/PowerShellPack) and IseCream (http://psisecream.codeplex.com/),
  192. # you can use this code to add your menu items. The added benefits are that you can specify the order of the menu items and
  193. # if the shortcut already exists it will add the menu item without the shortcut instead of failing as the default does.
  194. # Add-IseMenu -Name "Lines" @{            
  195. #    "Duplicate Line"  = {Duplicate-Line}| Add-Member NoteProperty order  1 -PassThru | Add-Member NoteProperty ShortcutKey "Ctrl+Alt+D" -PassThru
  196. #    "Conflate Line" = {Conflate-Line}| Add-Member NoteProperty order  2 -PassThru | Add-Member NoteProperty ShortcutKey "Ctrl+Alt+J" -PassThru
  197. #    "Move Up Line" = {MoveUp-Line}| Add-Member NoteProperty order  3 -PassThru | Add-Member NoteProperty ShortcutKey "Ctrl+Shift+Up" -PassThru
  198. #    "Move Down Line"   = {MoveDown-Line}| Add-Member NoteProperty order  4 -PassThru | Add-Member NoteProperty ShortcutKey "Ctrl+Shift+Down" -PassThru
  199. #    "Delete Trailing Blanks" = {Delete-TrailingBlanks}| Add-Member NoteProperty order  5 -PassThru | Add-Member NoteProperty ShortcutKey "Ctrl+Shift+Del" -PassThru
  200. #    "Delete Blank Lines" = {Delete-BlankLines} | Add-Member NoteProperty order  6 -PassThru | Add-Member NoteProperty ShortcutKey "Ctrl+Shift+End" -PassThru
  201. #    }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me