PoshCode Logo PowerShell Code Repository

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

ISE-Lines module v 1.1 ( Conflate-Line improved )
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.1
  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. ## v 1.1 fixed bug on last but one line and remove line continuation character while joining
  36. ##############################################################################################################
  37. function Conflate-Line
  38. {
  39.     $editor = $psISE.CurrentOpenedFile.Editor
  40.     $caretLine = $editor.CaretLine
  41.     $caretColumn = $editor.CaretColumn
  42.     $text = $editor.Text.Split("`n")
  43.     if ( $caretLine -ne $text.Count )
  44.     {
  45.         $line = $text[$caretLine -1] + $text[$caretLine] -replace ("(``)?`r", "")
  46.         $newText = @()
  47.         if ( $caretLine -gt 1 )
  48.         {
  49.             $newText = $text[0..($caretLine -2)]
  50.         }
  51.         $newText += $line
  52.         if ( $caretLine -ne $text.Count - 1)
  53.         {
  54.             $newText += $text[($caretLine +1)..($text.Count -1)]
  55.         }
  56.         $editor.Text = [String]::Join("`n", $newText)
  57.         $editor.SetCaretPosition($caretLine, $caretColumn)
  58.     }
  59. }
  60.  
  61. ## MoveUp-Line
  62. ##############################################################################################################
  63. ## Moves current line up
  64. ##############################################################################################################
  65. function MoveUp-Line
  66. {
  67.     $editor = $psISE.CurrentOpenedFile.Editor
  68.     $caretLine = $editor.CaretLine
  69.     if ( $caretLine -ne 1 )
  70.     {
  71.         $caretColumn = $editor.CaretColumn
  72.         $text = $editor.Text.Split("`n")
  73.         $line = $text[$caretLine -1]
  74.         $lineBefore = $text[$caretLine -2]
  75.         $newText = @()
  76.         if ( $caretLine -gt 2 )
  77.         {
  78.             $newText = $text[0..($caretLine -3)]
  79.         }
  80.         $newText += $line
  81.         $newText += $lineBefore
  82.         if ( $caretLine -ne $text.Count )
  83.         {
  84.             $newText += $text[$caretLine..($text.Count -1)]
  85.         }
  86.         $editor.Text = [String]::Join("`n", $newText)
  87.         $editor.SetCaretPosition($caretLine - 1, $caretColumn)
  88.     }
  89. }
  90.  
  91. ## MoveDown-Line
  92. ##############################################################################################################
  93. ## Moves current line down
  94. ##############################################################################################################
  95. function MoveDown-Line
  96. {
  97.     $editor = $psISE.CurrentOpenedFile.Editor
  98.     $caretLine = $editor.CaretLine
  99.     $caretColumn = $editor.CaretColumn
  100.     $text = $editor.Text.Split("`n")
  101.     if ( $caretLine -ne $text.Count )
  102.     {
  103.         $line = $text[$caretLine -1]
  104.         $lineAfter = $text[$caretLine]
  105.         $newText = @()
  106.         if ( $caretLine -ne 1 )
  107.         {
  108.             $newText = $text[0..($caretLine -2)]
  109.         }
  110.         $newText += $lineAfter
  111.         $newText += $line
  112.         if ( $caretLine -lt $text.Count -1 )
  113.         {
  114.             $newText += $text[($caretLine +1)..($text.Count -1)]
  115.         }
  116.         $editor.Text = [String]::Join("`n", $newText)
  117.         $editor.SetCaretPosition($caretLine +1, $caretColumn)
  118.     }
  119. }
  120.  
  121. ## Delete-TrailingBlanks
  122. ##############################################################################################################
  123. ## Deletes trailing blanks in the whole script
  124. ##############################################################################################################
  125. function Delete-TrailingBlanks
  126. {
  127.     $editor = $psISE.CurrentOpenedFile.Editor
  128.     $caretLine = $editor.CaretLine
  129.     $newText = @()
  130.     foreach ( $line in $editor.Text.Split("`n") )
  131.     {
  132.         $newText += $line -replace ("\s+$", "")
  133.     }
  134.     $editor.Text = [String]::Join("`n", $newText)
  135.     $editor.SetCaretPosition($caretLine, 1)
  136. }
  137.  
  138. ##############################################################################################################
  139. ## Inserts a submenu Lines to ISE's Custum Menu
  140. ## Inserts command Duplicate Line to submenu Lines
  141. ## Inserts command Conflate Line Selected to submenu Lines
  142. ## Inserts command Move Up Line to submenu Lines
  143. ## Inserts command Move Down Line to submenu Lines
  144. ## Inserts command Delete Trailing Blanks to submenu Lines
  145. ##############################################################################################################
  146. if (-not( $psISE.CustomMenu.Submenus | where { $_.DisplayName -eq "Line" } ) )
  147. {
  148.     $lineMenu = $psISE.CustomMenu.Submenus.Add("_Lines", $null, $null)
  149.     $null = $lineMenu.Submenus.Add("Duplicate Line", {Duplicate-Line}, "Ctrl+Alt+D")
  150.     $null = $lineMenu.Submenus.Add("Conflate Line", {Conflate-Line}, "Ctrl+Alt+J")
  151.     $null = $lineMenu.Submenus.Add("Move Up Line", {MoveUp-Line}, "Ctrl+Shift+Up")
  152.     $null = $lineMenu.Submenus.Add("Move Down Line", {MoveDown-Line}, "Ctrl+Shift+Down")
  153.     $null = $lineMenu.Submenus.Add("Delete Trailing Blanks", {Delete-TrailingBlanks}, "Ctrl+Shift+Del")
  154. }

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