Merging undo items

SyntaxEditor for WPF Forum

Posted 10 years ago by Scott Currie
Version: 14.1.0601
Avatar

I am trying to implement a behavior that will automatically change the XML end tag name when the corresponding XML start tag name is changed.  The logic for this is very straightforward.  I just add an event handler for DocumentTextChanged that does some token scanning to find the corresponding end tag, and then invoke SyntaxEditor.Document.ReplaceText(...).  All of this works perfectly.

 

The problem is that the original text change and the automatic change to the matching end tag both get theeir own undo items.  I'd really like them to have the same undo items.  I can't figure out how to resolve this.  Can I merge the two undo items?  Can I remove the undo items and add back a new one?  Can I provide an undo item for the ReplaceText to insert itself into?  Something else?  I don't see any hooks in the API to do any of the above.

 

Thanks,

-Scott

Comments (4)

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

Hi Scott,

Sorry but there isn't a way to merge undo items.  Text changes can contain multiple operations in them though.  The problem here is that the end user types a character and an automated text change (created by our control) occurs.  There isn't a way to add more operations to it once it's been created.

That being said, you might be able to handle the TextChanging event if you detect a change where the end user is modifying the tag name text, cancel it.  Then make a new text change of your own that has the same operation in it along with a second operation to alter the end tag too.  If you apply that text change, then both operations will occur but there will be a single undo item.

If you get this working and want to discuss/share the code, we'd love to.  It's something we've considered looking into as well.


Actipro Software Support

Posted 10 years ago by Scott Currie
Avatar

I have implemented this in 2 ways.  The first way requires that I cancel the DocumentTextChanging event, replace the end tag name with the new name, repeat the original change, apply the change, and then manually set the caret position.  This works fine, but it isn't as clean as I'd like.

The second approach cancels, the DocumentTextChanging event, repeats the original change, and then repeats the original change, but at the offset of the end tag.  This works great until you hit undo - at which point, it undoes the change, but also deletes or adds an additional character that wasn't in the original change.  I suspect this has something to do with the undo operation not updating its offset/snapshot information to match the original context of the operation after each operation is undone.  Are there any known issues with this?

Posted 10 years ago by Scott Currie
Avatar

Actually, I just realized that this was my own fault.  DocumentTextChanging was firing on the Undo/Redo events, and I wasn't filtering out those cases, which resulted in the behavior above.  If you want the code, I'd be happy to share it with you - as I'd rather you include this feature in the product than me maintaining it.

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

Hi Scott,

Sure if you wish to share your implementation ideas, feel free to email them to our support address and we can discuss them with you.  Please reference this thread.  Thanks!


Actipro Software Support

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

Add Comment

Please log in to a validated account to post comments.