Sublime Forum

Is there a way to clear all saved regions? (command)

#1

I’m adding regions using:

view.add_regions(key, [new_region], "comment", "", sublime.PERSISTENT)

and I was wondering if there is any way to remove all added regions in one fell swoop from the current view. I understand I can iterate over all keys and call ‘erase_regions’ but that would require me keeping the list of keys around somewhere, which is a challenge (with the current plugin I’m developing).

Thoughts?

0 Likes

Erase Region Without Key?
#2

I don’t think it’s possible - you have to track the keys you use in your plugin

0 Likes

#3

Yep… that actually makes sense when you think about it.

Is there a ‘best practice’ for persisting regions in a view? The ‘regions’ class does not serialize to json. Just wondering what folks do. Right now, I’m just writing them out to a ‘settings’ file (begin and end) but the problem with that is if the file is changed on disk (which it is quite often) the regions are wrong.

0 Likes

#4

With the caveat that I have no actual experience with something like this…

How time consuming/complex is it to calculate the regions to begin with? If it’s not too stressful it might be better to instead persist the data needed to know how to get back to the same regions again instead and just recalculate them whenever the file is reloaded.

Failing that you could do something like also persist the last modification time of the file when you persist the regions and then verify it in on_load to know if it’s safe to re-apply the same regions or not. That’s not 100% foolproof (someone crafty could modify the file but maintain the time stamp) but probably good enough.

1 Like