Sublime Forum

Is there a good way to run find_by_selector() over a region instead of the entire view?

#1

Of course, I could run find_by_selector() over the entire view and then filter out regions I don’t care about, but in my application, that’s very slow. I’m dealing with potentially very large files, but I may only care about a smallish region at a time.

If I have a region that’s rather small, it’s much faster for me to iterate through it calling match_selector().

My current solution is to have some heuristic for what defines a “small” region, and if it’s sufficiently small, I iterate through it with match_selector(), and if it’s not, I call find_by_selector(). It works, but it’s inelegant to say the least, and I’m wondering if there’s a better way.

One of the parts that’s particularly inelegant is that extract_scope() doesn’t seem to be particularly useful, because it seems to have a mind of its own about what “scope” means. What would be useful would be something that given a point and a selector that matches that point, find the extent around the point that matches the selector. I don’t see anything like that, so it’s more iterating over points with match_selector().

I see an API suggestion from three years ago about this, but I don’t know if there’s a better workaround than what I’m doing.

0 Likes

#2

You can iterate over the undocumented view.extract_tokens_with_scopes(region).

1 Like

#3

Sounds like that will help a lot. Thanks!

0 Likes