PoshCode Logo PowerShell Code Repository

ISE-Lines (modification of post by view diff)
View followups from Bernd Kriszio | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/772"></script>download | new post

ISE-Lines module v 1.0
DEVELOPED FOR CTP3

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

  1. #requires -version 2.0
  2. ## ISE-Lines module v 1.0
  3. ## DEVELOPED FOR CTP3
  4. ## See comments for each function for changes ...
  5. ##############################################################################################################
  6. ## Provides Line cmdlets for working with ISE
  7. ## Duplicate-Line - Duplicates current line
  8. ## Conflate-Line - Conflates current and next line
  9. ## MoveUp-Line - Moves current line up
  10. ## MoveDown-Line - Moves current line down
  11. ## Delete-TrailingBlanks - Deletes trailing blanks in the whole script
  12. ##############################################################################################################
  13.  
  14. ## Duplicate-Line
  15. ##############################################################################################################
  16. ## Duplicates current line
  17. ##############################################################################################################
  18. function Duplicate-Line
  19. {
  20.     $editor = $psISE.CurrentOpenedFile.Editor
  21.     $caretLine = $editor.CaretLine
  22.     $caretColumn = $editor.CaretColumn
  23.     $text = $editor.Text.Split("`n")
  24.     $line = $text[$caretLine -1]
  25.     $newText = $text[0..($caretLine -1)]
  26.     $newText += $line
  27.     $newText += $text[$caretLine..($text.Count -1)]
  28.     $editor.Text = [String]::Join("`n", $newText)
  29.     $editor.SetCaretPosition($caretLine, $caretColumn)
  30. }
  31.  
  32. ## Conflate-Line
  33. ##############################################################################################################
  34. ## Conflates current and next line
  35. ##############################################################################################################
  36. function Conflate-Line
  37. {
  38.     $editor = $psISE.CurrentOpenedFile.Editor
  39.     $caretLine = $editor.CaretLine
  40.     $caretColumn = $editor.CaretColumn
  41.     $text = $editor.Text.Split("`n")
  42.     if ( $caretLine -ne $text.Count )
  43.     {
  44.         $line = $text[$caretLine -1] + $text[$caretLine] -replace ("`r", "")
  45.         $newText = @()
  46.         if ( $caretLine -gt 1 )
  47.         {
  48.             $newText = $text[0..($caretLine -2)]
  49.         }
  50.         $newText += $line
  51.         $newText += $text[($caretLine +1)..($text.Count -1)]
  52.         $editor.Text = [String]::Join("`n", $newText)
  53.         $editor.SetCaretPosition($caretLine, $caretColumn)
  54.     }
  55. }
  56.  
  57. ## MoveUp-Line
  58. ##############################################################################################################
  59. ## Moves current line up
  60. ##############################################################################################################
  61. function MoveUp-Line
  62. {
  63.     $editor = $psISE.CurrentOpenedFile.Editor
  64.     $caretLine = $editor.CaretLine
  65.     if ( $caretLine -ne 1 )
  66.     {
  67.         $caretColumn = $editor.CaretColumn
  68.         $text = $editor.Text.Split("`n")
  69.         $line = $text[$caretLine -1]
  70.         $lineBefore = $text[$caretLine -2]
  71.         $newText = @()
  72.         if ( $caretLine -gt 2 )
  73.         {
  74.             $newText = $text[0..($caretLine -3)]
  75.         }
  76.         $newText += $line
  77.         $newText += $lineBefore
  78.         if ( $caretLine -ne $text.Count )
  79.         {
  80.             $newText += $text[$caretLine..($text.Count -1)]
  81.         }
  82.         $editor.Text = [String]::Join("`n", $newText)
  83.         $editor.SetCaretPosition($caretLine - 1, $caretColumn)
  84.     }
  85. }
  86.  
  87. ## MoveDown-Line
  88. ##############################################################################################################
  89. ## Moves current line down
  90. ##############################################################################################################
  91. function MoveDown-Line
  92. {
  93.     $editor = $psISE.CurrentOpenedFile.Editor
  94.     $caretLine = $editor.CaretLine
  95.     $caretColumn = $editor.CaretColumn
  96.     $text = $editor.Text.Split("`n")
  97.     if ( $caretLine -ne $text.Count )
  98.     {
  99.         $line = $text[$caretLine -1]
  100.         $lineAfter = $text[$caretLine]
  101.         $newText = @()
  102.         if ( $caretLine -ne 1 )
  103.         {
  104.             $newText = $text[0..($caretLine -2)]
  105.         }
  106.         $newText += $lineAfter
  107.         $newText += $line
  108.         if ( $caretLine -lt $text.Count -1 )
  109.         {
  110.             $newText += $text[($caretLine +1)..($text.Count -1)]
  111.         }
  112.         $editor.Text = [String]::Join("`n", $newText)
  113.         $editor.SetCaretPosition($caretLine +1, $caretColumn)
  114.     }
  115. }
  116.  
  117. ## Delete-TrailingBlanks
  118. ##############################################################################################################
  119. ## Deletes trailing blanks in the whole script
  120. ##############################################################################################################
  121. function Delete-TrailingBlanks
  122. {
  123.     $editor = $psISE.CurrentOpenedFile.Editor
  124.     $caretLine = $editor.CaretLine
  125.     $newText = @()
  126.     foreach ( $line in $editor.Text.Split("`n") )
  127.     {
  128.         $newText += $line -replace ("\s+$", "")
  129.     }
  130.     $editor.Text = [String]::Join("`n", $newText)
  131.     $editor.SetCaretPosition($caretLine, 1)
  132. }
  133.  
  134. ##############################################################################################################
  135. ## Inserts a submenu Lines to ISE's Custum Menu
  136. ## Inserts command Duplicate Line to submenu Lines
  137. ## Inserts command Conflate Line Selected to submenu Lines
  138. ## Inserts command Move Up Line to submenu Lines
  139. ## Inserts command Move Down Line to submenu Lines
  140. ## Inserts command Delete Trailing Blanks to submenu Lines
  141. ##############################################################################################################
  142. if (-not( $psISE.CustomMenu.Submenus | where { $_.DisplayName -eq "Line" } ) )
  143. {
  144.     $lineMenu = $psISE.CustomMenu.Submenus.Add("_Lines", $null, $null)
  145.     $null = $lineMenu.Submenus.Add("Duplicate Line", {Duplicate-Line}, "Ctrl+Alt+D")
  146.     $null = $lineMenu.Submenus.Add("Conflate Line", {Conflate-Line}, "Ctrl+Alt+J")
  147.     $null = $lineMenu.Submenus.Add("Move Up Line", {MoveUp-Line}, "Ctrl+Shift+Up")
  148.     $null = $lineMenu.Submenus.Add("Move Down Line", {MoveDown-Line}, "Ctrl+Shift+Down")
  149.     $null = $lineMenu.Submenus.Add("Delete Trailing Blanks", {Delete-TrailingBlanks}, "Ctrl+Shift+Del")
  150. }

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