Advertisement
Guest User

Visual Studio Macro - Move Line Up/Down (Apply shortcut to CTRL+SHIFT+Up)

a guest
Mar 31st, 2010
1,444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.11 KB | None | 0 0
  1. '' Duplicates the current line (or selection of lines) and places the copy
  2.     '' one line below or above the current cursor position (based upon the parameter)
  3.     Sub CopyLine(ByVal movingDown As Boolean)
  4.         Dim objSel As TextSelection = DTE.ActiveDocument.Selection
  5.         ' store the original selection and cursor position
  6.         Dim topPoint As TextPoint = objSel.TopPoint
  7.         Dim bottomPoint As TextPoint = objSel.BottomPoint
  8.         Dim lTopLine As Long = topPoint.Line
  9.         Dim lTopColumn As Long = topPoint.LineCharOffset
  10.         Dim lBottomLine As Long = bottomPoint.Line
  11.         Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
  12.         Dim verticalOffset As Integer = 0
  13.         If (movingDown) Then
  14.             verticalOffset = (lBottomLine - lTopLine) + 1
  15.         End If
  16.  
  17.         If ((lTopLine <> lBottomLine) Or (lTopColumn <> lBottomColumn)) Then
  18.             ' A selection is present. Select all lines in their entirety
  19.             objSel.MoveToLineAndOffset(lBottomLine, 1)
  20.             objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
  21.         Else
  22.             ' No characters are selected, use the enitre current line
  23.             objSel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
  24.         End If
  25.         ' always extend the selection to the end of the lower line
  26.         objSel.EndOfLine(True)
  27.  
  28.         Dim linetext As String = objSel.Text
  29.         objSel.EndOfLine() ' de-select the current selection
  30.  
  31.         ' make the duplicate after the current selection:
  32.         objSel.NewLine()
  33.         objSel.Text = linetext
  34.  
  35.         ' restore the cursor to original position and selection
  36.         objSel.MoveToLineAndOffset(lBottomLine + verticalOffset, lBottomColumn)
  37.         objSel.MoveToLineAndOffset(lTopLine + verticalOffset, lTopColumn, True)
  38.     End Sub
  39.  
  40.     '' Duplicates the current line (or selection of lines) and places the copy
  41.     '' one line below the current cursor position
  42.     Sub CopyLineDown()
  43.         CopyLine(True)
  44.     End Sub
  45.  
  46.     '' Duplicates the current line (or selection of lines) and places the copy
  47.     '' one line above the current cursor position
  48.     Sub CopyLineUp()
  49.         CopyLine(False)
  50.     End Sub
  51.  
  52.     '' Moves the selected lines up one line. If no line is
  53.     '' selected, the current line is moved.
  54.     ''
  55.     Sub MoveLineUp()
  56.         DTE.UndoContext.Open("MoveLineUp")
  57.         Dim objSel As TextSelection = DTE.ActiveDocument.Selection
  58.         ' store the original selection and cursor position
  59.         Dim topPoint As TextPoint = objSel.TopPoint
  60.         Dim bottomPoint As TextPoint = objSel.BottomPoint
  61.         Dim lTopLine As Long = topPoint.Line
  62.         Dim lTopColumn As Long = topPoint.LineCharOffset
  63.         Dim lBottomLine As Long = bottomPoint.Line
  64.         Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
  65.  
  66.         ' move to the line above the top line
  67.         objSel.MoveToLineAndOffset(lTopLine - 1, 1)
  68.         ' and move it down, until its below the bottom line:
  69.         Do
  70.             DTE.ExecuteCommand("Edit.LineTranspose")
  71.         Loop Until (objSel.BottomPoint.Line >= lBottomLine)
  72.         ' Since the line we are on has moved up, our location in the file has changed:
  73.         lTopLine = lTopLine - 1
  74.         lBottomLine = lBottomLine - 1
  75.  
  76.         ' restore the cursor to original position and selection
  77.         objSel.MoveToLineAndOffset(lBottomLine, lBottomColumn)
  78.         objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
  79.         DTE.UndoContext.Close()
  80.     End Sub
  81.  
  82.     '' Moves the selected lines down one line. If no line is
  83.     '' selected, the current line is moved.
  84.     ''
  85.     Sub MoveLineDown()
  86.         DTE.UndoContext.Open("MoveLineDown")
  87.         Dim objSel As TextSelection = DTE.ActiveDocument.Selection
  88.         ' store the original selection and cursor position
  89.         Dim topPoint As TextPoint = objSel.TopPoint
  90.         Dim bottomPoint As TextPoint = objSel.BottomPoint
  91.         Dim lTopLine As Long = topPoint.Line
  92.         Dim lTopColumn As Long = topPoint.LineCharOffset
  93.         Dim lBottomLine As Long = bottomPoint.Line
  94.         Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
  95.  
  96.         ' move to the bottom line
  97.         objSel.MoveToLineAndOffset(lBottomLine, 1)
  98.         ' and move it down, which effectively moves the line below it up
  99.         ' then move the cursor up, always staying one line above the line
  100.         ' that is moving up, and keep moving it up until its above the top line:
  101.         'Dim lineCount As Long = lTopLine - lBottomLine
  102.         Dim lineCount As Long = lBottomLine - lTopLine
  103.         Do
  104.             DTE.ExecuteCommand("Edit.LineTranspose")
  105.             objSel.LineUp(False, 2)
  106.             lineCount = lineCount - 1
  107.         Loop Until (lineCount < 0)
  108.         ' Since the line we are on has moved down, our location in the file has changed:
  109.         lTopLine = lTopLine + 1
  110.         lBottomLine = lBottomLine + 1
  111.  
  112.         ' restore the cursor to original position and selection
  113.         objSel.MoveToLineAndOffset(lBottomLine, lBottomColumn)
  114.         objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
  115.         DTE.UndoContext.Close()
  116.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement