How to move a block of lines Up/Down

Posted 5 years ago by Avatar Mike Dempsey - Sr. Developer, Teradata Corp

Visual Studio (and the old editor control I used to use) supports Alt+Up and Alt+Down to move either the current line or a highlighted block of lines Up or Down.

In Syntax Editor I can bind Alt+Down to TransposeLinesCommand to move the current line down but I dont see a command that works on a block of lines, or any way to move line(s) Up.

I can add an OnKeyDown override to handle these shortcuts but what is the simplest way to perform these functions? 

Do I need to Delete/InsertText or is there a simpler way. 

Comments (5)

Posted 5 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Mike,

Sorry we don't have anything built in for the particular functionality.  You can use the methods in sequence like DeleteText and InsertText to do that sort of feature though via KeyDown as you said.

Actipro Software Support
Posted 5 years ago by Mike Dempsey - Sr. Developer, Teradata Corp

OK. I had hoped that there was at least a MoveText or something so that I dont need to freeze/unfreeze the undo buffer in addition to figuring out the text ranges and doing the Delete/Insert.
(It has to be an 'atomic' function from the user's perspective)

Posted 5 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Mike,

If you start an undo group, do your changes, and then end the group, it should be atomic to the end user.

Actipro Software Support
Posted 3 years ago by Frank Link - CEO, FLK Consulting UG


can you give me an example to do this. I have test it with DeleteText and InsertText but I can't find a correct way to do it.



Posted 3 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Frank,

Our TransposeLinesCommand code effectively does this:

// Get the document position of the first line
DocumentPosition documentPosition = context.Selection.EndDocumentPosition;
if (documentPosition.Line == context.Document.Lines.Count - 1)
	documentPosition = new DocumentPosition(documentPosition.Line - 1, documentPosition.Character);

// Get the offset range
int startOffset = context.Document.Lines[documentPosition.Line].StartOffset;
int endOffset = context.Document.Lines[documentPosition.Line + 1].EndOffset;

// Extract the two lines of text from the document
string[] lines = context.Document.GetSubstring(startOffset, endOffset - startOffset, LineTerminator.Newline).Split(new char[] { '\n' });

try {
	// Suspend selection events

	// Replace with the transposed lines
	context.Document.ReplaceText(DocumentModificationType.TransposeLines, startOffset, endOffset - startOffset, lines[1] + "\n" + lines[0], DocumentModificationOptions.None, new WeakReference(context.View));

	// Move the caret to the correct offset on the last line
	context.SyntaxEditor.Caret.Offset = context.Document.PositionToOffset(new DocumentPosition(documentPosition.Line + 1, documentPosition.Character));
finally {
	// Resume selection events

Actipro Software Support
Information The latest build of this product (2018.1 build 0341) was released 1 month ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.