0

Visual Studio 2010: Zeile nach oben verschieben Makro

Um im Visual Studio ganze Zeilen per Tastenkombination nach unten bzw nach oben zu verschieben, kann man folgendes Makro verweden. Dies geht allerdings nur bis zur 2010er-Version. VS 2012 unterstützt keine Makros mehr. Die Funktionalität kann man sich aber dennoch durch Extensions holen. Zum Beispiel das Productivity Power Tools 2012.

Imports EnvDTE
Public Module MoveLines
	'' 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
End Module

(Quelle: http://pastebin.com/SJxYudSw)

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert