Sublime Forum

find_by_selector and nested scopes


It seems that find_by_selector doesn’t return regions for nested scopes.


function foo()
	if true then
		if false then
			print 1

Although there are two if blocks, and the scope name correctly identifies them both when the cursor is inside the inner if, find_by_regions only returns one region (the outer one) for the ‘if’ selector.

Is there a formal way to report issues like this?



Has there been any progress on this? I would likewise like to be able to traverse nested scopes programatically. I can understand view.find_by_selector just returning the outermost ones, but then there should be something you can do with the region list to traverse the scopes within each region.



I stumbled upon this problem and it doesn’t seem to be fixed. find_by_selector doesn’t return nested regions. Is there any way to do this?



can you provide an example of the code in the view, which syntax it is and the view.find_by_selector parameters you are passing?

probably it is related to this suggestion of mine:



Javascript example:

var outer = function () {
    var inner = function () {};

and from python: print(view.find_by_selector('meta.block.js'))
results in only one region: [(23, 59)]

I would expect 2 regions [(23, 59), (54, 57)], or the Region class should also have a find_by_selector so I can search for the inner selector.
I suppose your API suggestion could work, if finding a selector within a region would ignore the outer regions.

1 Like


ah good point, probably my idea would only work if you searched for meta.block.js and meta.block.js meta.block.js etc. and wouldn’t deal with even deeper, arbitrarily nested scopes very well… realistically, you are better off writing your own implementation for this I think, checking the scope at each character of the region returned by the find_by_selector method - using scope selectors is not ideal here imo…
either that or the API could return nested regions like you suggest



Not that I know how find_by_selector works, but I don’t understand why this couldn’t be done with scope selectors. If I check the scope under the inner function body, the scope says
source.js meta.block.js meta.block.js punctuation.definition.block.js
So the scope is correctly finding the meta.block.js selector twice.



at the moment, the find_by_selector API basically returns all continuous regions where match_selector returns true, so there is no way for it to know that you want two separate regions. Any changes to this API would be breaking, hence why I think it’s better to write your own method and compare the scopes manually rather than using scope selector logic, which doesn’t handle unknown-depth nesting very well.



Thanks, I have more selector related questions actually but i’ll move that to a different topic.