Sublime Forum

Getting view text diff between on_modified events

#1

Hello there.

I’m making a plugin interfacing with a compiler, and i need a way to get text diffs between on_modified events.

The diff could take any form, that’s not a problem, just a synthetic way to get all the changes that happened between now and the last time on_modified was called.

I’m pretty sure there is a way to do that, but i can’t find it … Anybody has an idea ?

0 Likes

#2

Python has a difflib module but I haven’t checked whether it is part of the ST Python-version. Test import difflib and check the Console for any error. Otherwise you would include it (or some alternative) as part of your plug-in.

on_modified happens *constantly *within a view so you would need to keep an eye on how much processing you do on each occasion. Python is very efficient, however, so this may not be too much of an issue.

Handling Undo and Redo correctly is likely to be a challenge.

0 Likes

#3

I thought about that, but i’d rather not reprocess the diff every time. Anyway, for the record, it is part of the ST python version.

Well yes from what i gathered it seems to happen on every event. But that’s not likely a big problem because i can always aggregate such events and do processing only every X events, or in an helper thread.

Why ? From what i’ve seen, on_modified events are also sent when an undo or a redo are done. I don’t need to track wether they are undos/redos or not, just keep a consistent view of the file on the compiler’s part.

Thanks for the input !

0 Likes

#4

I, personally, cannot fully explain why Undo/Redo cause concerns, or precisely how they interact with on_modified. Perhaps someone else might offer some insight.

0 Likes

#5

Undo/redo are a concern if an on_modified callback changes the buffer. If you change the buffer after an undo, it breaks the redo stack (and can also make undoing impossible, because the next undo undoes the callback modifications).

0 Likes

#6

I was using on_modified to add_regions (and modify them): I didn’t believe this changed the buffer(?). Regardless, I had to add a separate on_selection_modified to counter-balance the impact of Undo/Redo (or, at least, try to).

PS I am not trying to be discouraging :smile:

0 Likes

#7

Ok so this is clearly not a problem for my use case, since i’m not planning to modify the document in those callbacks.

So i guess the answer to my question is a “no there is no way of doing that” ?

That would be a definitive lack for an “on_modified” event IMO .

0 Likes

#8

The limit of my knowledge on this subject is to suggest that you could use **on_selection_modified **to store the current line-contents, having checked that they have moved to a different line, and then compare this to the line following on_modified. There is also the command_history() to explore, but I do not know how useful this may prove. Good luck.

0 Likes

#9

Ok. Thank you very much for your help !

0 Likes