So here is my editor text changed event:
private void FormulaEditor_TextChanged(object sender, TextChangedEventArgs e)
{
if(!changedByControls)
{
int oldTextLength = e.OldTextValue?.Length ?? 0;
int newTextLength = e.NewTextValue?.Length ?? 0;
bool textInserted = newTextLength > oldTextLength;
TextAction action = textInserted ? TextAction.Insert : TextAction.Remove;
int textLength = Math.Abs(newTextLength - oldTextLength);
int startIndex = textInserted ? FormulaEditor.CursorPosition : FormulaEditor.CursorPosition - textLength;
string textForOperation = textInserted ? e.NewTextValue: e.OldTextValue;
string text = textForOperation.Substring(startIndex, textLength);
textCommandManager.ChangeText(action, text, startIndex);
UndoButton.IsEnabled = textCommandManager.CanUndo;
}
changedByControls = false;
}
I will explain what happens here. Every time when I insert or remove a symbol or some part of text, this text is added to the commands list with appropriate action. When I insert a symbol or text, everything works correctly. When I delete one character or text completely everything also works fine. But when I select a part of a word and delete it, for example, I encounter very strange behavior. Let me explain with an example. Let's say I copied and pasted the word "wasabi" into my editor. At the moment, the list of commands contains one element and looks like this:
indexOfElement - 0, action - Insert, Position - 0, Text - "wasabi".
Now, for example, I have selected a part of the word "sabi" and clicked delete. I noticed that in this case, the TextChangedEvent is always triggered 3 times and then contains 4 elements (given that the first element was the word "wasabi" inserted at the beginning). After deleting this part of the word, the list looks like this:
indexOfElement - 0, action - Insert, Position - 0, Text - "wasabi";
indexOfElement - 1, action - Remove, Position - 2, Text - "sabi" (this is the value I expected, but unfortunately it is followed by 2 more elements);
indexOfElement - 2, action - Remove, Position - 0, Text - "wa";
indexOfElement - 3, action - Insert, Position - 0, Text - "wa";
Can anyone explain what this behavior is due to and how to fix it?
For some reason, the visual studio update just worked for me. I read about this bug with the inability to debug earlier on github, maybe now it's just fixed. Or maybe it was just that some library was damaged or something else. In short, everything works fine now, the method behavior is as expected, the text changed event can now also be debugged (I couldn't do it before, I got a message that incompatible code was running in the thread). Thanks to everyone who tried to solve my problem