…then the regions are not deleted, but rather fall through (e.g. if I had a file with 10 lines and a region on the 5th line, then after deleting the 5th line, the region is now associated with the former 6th line). Is this a bug or a feature?
When text that has associated regions is deleted
I’m dealing with a related issue, and here’s how I understand it: this is neither a bug nor a feature, but a misunderstanding of the metaphor. There’s no such thing as “text that has associated regions.” You can use view.add_regions (which is what I assume you’re talking about) to add a set of regions to a view under a particular key, but that has nothing to do with text. Regions are locations in the file.
Let’s say I have a buffer that just contains the text “foobar”. I’m doing the following in the console:
>>> a = sublime.Region(0, 3)
>>> a
(0, 3)
# Note that this isn't text at all. Just locations.
>>> view.substr(a)
u'foo'
# I can use those locations to get the text occupying that space *from the view.*
# Now, if I delete "foo", so that the buffer just contains "bar":
>>> view.substr(a)
u'bar'
>>> a
(0, 3)
# a (the Region) hasn't changed, but the contents of that Region have.
It’s like the difference between addresses and people. view.add_regions allows you to set up a list of addresses, just like if you had a mailing list. But if a family moves, their old house stays at the same address, and so sending a letter to 123 Whatever Blvd will still go to the same place, whether they live there or not.
So: Regions are supposed to be independent of text, and so shouldn’t be changed if the text is.
I think what we’re both wanting, though, is to have something like mail forwarding (metaphorically, not programmatically), such that we can identify a span of text in a buffer with which we can associate data that will persist even if the location of that text changes. I can see, though, how this could be a programming nightmare, and I’m not sure at all how I’d go about implementing it myself. I’m hoping there’s something in place that can simulate that, though. Maybe?
Yep it seems so. Great metaphor by the way!
I decided to live with it. In my use case regions denote breakpoints (to provide an indicator that certain line has a breakpoint associated with it), so I don’t trust add_regions and keep all the info in an external settings file. Then in an on_modified event of a sublime.EventListener I check whether recent edits skewed the regions and fix them up if necessary.