Best way to work with ITextChange?

SyntaxEditor for WPF Forum

Posted 5 years ago by Simon Sprott
Version: 14.2.0610
Avatar

Hi

I am looking to group a set of changes together so they can be undone atomically, reading though other posts this is acomplished via the ITextChange.

Using this mechanisum I have managed to group changes, but the behaviour of the interface makes it somewhat tricky to use.

From what I can gather ITextChange groups the changes and applies them in the order they were added.

So if the document is

01234

and you intend to perform 2 changes, replacing '1' for 'aa' and '3' for 'bb'

you would expect to do something like

myTextChange.Replace(start:1, length:1, insertString:"aa");

myTextChange.Replace(start:3, length:1, insertString:"bb");

but these get applied producing (which makes sense, but makes producing the second replace rather tricky in more general cases)

01234 -> 0aa234 ->0aabb34

I've tried translating offsets to the ITextChange.Snapshot assuming the snapshot would contain the changes (but snapshot does not change).

 

In my case I have solved the problem by coping all the changes out, ordering them by offset and building a new ITextChange, then applying that. This works for us as our changes do not overlap, but its difficult to see how to use this interface effectivley?

Comments (1)

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

Hi Simon,

A text change is what applies text replace operations to move from one snapshot to a new one.  Think of the snapshot text as a long string.  When you first start a text change, the snapshot text is retrieved and the first operation works directly on that original text.  Now you end up with a variable of the revised text.  All offsets now need to be relative to what is currently in that variable.  There are several ways to do this.

1) Do your operations in reverse order.  Meaning since the second replace starts later in the chain than the first, execute it first then do the first operation last.  This requires minimal tracking but only works if you have non-overalpping text change operations.

2) We have a built-in feature that can track and apply offset deltas so that you don't have to think about them.  In your case, this would work.  Create your text change like:

options.OffsetDelta = TextChangeOffsetDelta.SequentialOnly;
ITextChange textChange = snapshot.CreateTextChange(TextChangeTypes.Indent, options);

Then run the same lines you had before.  It will be tracking that the first operation had a net offset delta of 1 and will then change the start offset of your second call automatically to 4 (your specified 3 + delta 1).  This option only works if the operations are always in sequential order.  Please see the documentation for that offset delta option.  We use that for things like Indent, etc.

3) Track the character delta yourself.  You can always fall back to this but it's more work than either of the two cases above.  However you can use it in scenarios when the other two don't work.

Overall I would recommend using options 1 or 2 whenever possible.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.