Text and Current Line Offsets Not Matching in SyntaxEditor

SyntaxEditor for WPF Forum

The latest build of this product (v25.1.1) was released 3 months ago, which was before this thread was created.
Posted 2 days ago by david lin
Version: 24.1.1
Platform: .NET 8
Environment: Windows 10 (64-bit)
Avatar

Hello Actipro Developers,

I'm currently implementing a custom shortcut for line breaks in SyntaxEditor through code.
Since I couldn’t find any official method that directly supports this feature in the documentation, I decided to implement it myself.

Here is my code:

 private void OnEditorPreviewKeyDown(object sender, KeyEventArgs e)
 {
     var isEnter = e.Key == Key.Return;

     var index = editor.ActiveView.Selection.CaretOffset;

     var mods = Keyboard.Modifiers;

     if (isEnter)
     {
         if ((mods & ModifierKeys.Control) != 0)
         {
             editor.Text = editor.Text
                 .Insert(editor.ActiveView.CurrentViewLine.EndOffset, Environment.NewLine);

             editor.ActiveView.Selection.CaretOffset 
                 = editor.ActiveView.CurrentViewLine.NextLine.StartOffset;

             e.Handled = true;
         }
     }
 }

As shown in the code, when pressing Ctrl+Enter, a new line should be inserted immediately below the current one, and the caret should move to the beginning of that new line.

However, I noticed that the value returned by editor.ActiveView.CurrentViewLine.EndOffset does not match the actual editor.Offset.
Because of this mismatch, the custom line break doesn’t behave as expected.

debug img

From what I can tell, the offset calculation seems to ignore characters like \r\n, which might be causing the inconsistency.

It seems that the offset calculation removes or ignores the \r\n characters, which makes it difficult to insert a new line at a specific character index.
I don’t quite understand why this happens, and it causes some trouble when trying to calculate the correct position for inserting a newline.

Could you please advise whether there’s a more convenient or recommended way to insert a new line programmatically in SyntaxEditor?

Thank you!

Attached is the complete project address

[Modified 2 days ago]

Add Comment

Please log in to a validated account to post comments.