How to move a block of lines Up/Down

SyntaxEditor for Windows Forms Forum

Posted 10 years ago by Michael Dempsey - Sr. Developer, Teradata Corp
Version: 13.1.0311
Avatar

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 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

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 10 years ago by Michael Dempsey - Sr. Developer, Teradata Corp
Avatar

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 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

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 8 years ago by Frank Link - CEO, FLK Consulting UG
Avatar

Hi,

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.

regards

Frank

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

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
	context.Selection.SuspendEvents();

	// 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
	context.Selection.ResumeEvents();
}


Actipro Software Support

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.