Get new cursor position after Textchange

SyntaxEditor for WPF Forum

Posted 13 years ago by Tino Schnerwitzki
Version: 11.1.0545

i´m applying a textchange on the current editorline. This change replaces the text of the whole line. If the cursor is inside the replaced range its postion is lost and the caret moved to the end of the (replaced) line.

I tried to save the old cursor position by creating a TextSnapshotRange using the old snapshot and the cursors offset:

TextSnapshotRange range = new TextSnapshotRange(oldsnapshot, editor.Caret.Offset);
After applying the change I tried to translate that range to the current snapshot and set the cursors offset:

range = range.TranslateTo(editor.Document.CurrentSnapshot, TextRangeTrackingModes.Default);
editor.Caret.Offset = range.StartOffset;
This solution doesn´t work. Any ideas how to solve this?

Thanks in advance.

Comments (1)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Tino,

Snapshot translation won't work for what you're trying to do since it effectively pushes/pulls the translated offset back and forth based on text change operations. Whenever you delete a range, it moves the offset back. If you insert before the offset, it pushes it forward. A replace is effectively a delete followed by an insert.

You might just want to save the caret's position (line/col) and then restore it after the text change is applied.

Actipro Software Support

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

Add Comment

Please log in to a validated account to post comments.