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();
}