The answers proposed work, but none of them are as nice as eclipse with regard to how they preserve the existing paste buffer, the currently selected characters, and they do not allow the user to operate upon a range of lines. Here is a solution I came up with that preserves the paste buffer, the current character selection, and works with or without a selection (that may or may not span multiple rows):
'' Duplicates the current line (or selection of lines) and places the copy
'' one line below or above the current cursor position (based upon the parameter)
Sub CopyLine(ByVal movingDown As Boolean)
Dim objSel As TextSelection = DTE.ActiveDocument.Selection
' store the original selection and cursor position
Dim topPoint As TextPoint = objSel.TopPoint
Dim bottomPoint As TextPoint = objSel.BottomPoint
Dim lTopLine As Long = topPoint.Line
Dim lTopColumn As Long = topPoint.LineCharOffset
Dim lBottomLine As Long = bottomPoint.Line
Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
Dim verticalOffset As Integer = 0
If (movingDown) Then
verticalOffset = (lBottomLine - lTopLine) + 1
End If
If ((lTopLine <> lBottomLine) Or (lTopColumn <> lBottomColumn)) Then
' A selection is present. Select all lines in their entirety
objSel.MoveToLineAndOffset(lBottomLine, 1)
objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
Else
' No characters are selected, use the enitre current line
objSel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
End If
' always extend the selection to the end of the lower line
objSel.EndOfLine(True)
Dim linetext As String = objSel.Text
objSel.EndOfLine() ' de-select the current selection
' make the duplicate after the current selection:
objSel.NewLine()
objSel.Text = linetext
' restore the cursor to original position and selection
objSel.MoveToLineAndOffset(lBottomLine + verticalOffset, lBottomColumn)
objSel.MoveToLineAndOffset(lTopLine + verticalOffset, lTopColumn, True)
End Sub
'' Duplicates the current line (or selection of lines) and places the copy
'' one line below the current cursor position
Sub CopyLineDown()
CopyLine(True)
End Sub
'' Duplicates the current line (or selection of lines) and places the copy
'' one line above the current cursor position
Sub CopyLineUp()
CopyLine(False)
End Sub
'' Moves the selected lines up one line. If no line is
'' selected, the current line is moved.
''
Sub MoveLineUp()
DTE.UndoContext.Open("MoveLineUp")
Dim objSel As TextSelection = DTE.ActiveDocument.Selection
' store the original selection and cursor position
Dim topPoint As TextPoint = objSel.TopPoint
Dim bottomPoint As TextPoint = objSel.BottomPoint
Dim lTopLine As Long = topPoint.Line
Dim lTopColumn As Long = topPoint.LineCharOffset
Dim lBottomLine As Long = bottomPoint.Line
Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
' move to the line above the top line
objSel.MoveToLineAndOffset(lTopLine - 1, 1)
' and move it down, until its below the bottom line:
Do
DTE.ExecuteCommand("Edit.LineTranspose")
Loop Until (objSel.BottomPoint.Line >= lBottomLine)
' Since the line we are on has moved up, our location in the file has changed:
lTopLine = lTopLine - 1
lBottomLine = lBottomLine - 1
' restore the cursor to original position and selection
objSel.MoveToLineAndOffset(lBottomLine, lBottomColumn)
objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
DTE.UndoContext.Close()
End Sub
'' Moves the selected lines down one line. If no line is
'' selected, the current line is moved.
''
Sub MoveLineDown()
DTE.UndoContext.Open("MoveLineDown")
Dim objSel As TextSelection = DTE.ActiveDocument.Selection
' store the original selection and cursor position
Dim topPoint As TextPoint = objSel.TopPoint
Dim bottomPoint As TextPoint = objSel.BottomPoint
Dim lTopLine As Long = topPoint.Line
Dim lTopColumn As Long = topPoint.LineCharOffset
Dim lBottomLine As Long = bottomPoint.Line
Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
' move to the bottom line
objSel.MoveToLineAndOffset(lBottomLine, 1)
' and move it down, which effectively moves the line below it up
' then move the cursor up, always staying one line above the line
' that is moving up, and keep moving it up until its above the top line:
'Dim lineCount As Long = lTopLine - lBottomLine
Dim lineCount As Long = lBottomLine - lTopLine
Do
DTE.ExecuteCommand("Edit.LineTranspose")
objSel.LineUp(False, 2)
lineCount = lineCount - 1
Loop Until (lineCount < 0)
' Since the line we are on has moved down, our location in the file has changed:
lTopLine = lTopLine + 1
lBottomLine = lBottomLine + 1
' restore the cursor to original position and selection
objSel.MoveToLineAndOffset(lBottomLine, lBottomColumn)
objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
DTE.UndoContext.Close()
End Sub
I edited this post to add the UndoContext mechanism (suggested by Nicolas Dorier) at the beginning of the MoveLineUp() and MoveLineDown() methods and closing it at their end.