Sublime Forum

Scopes and extract_scope

#1

I’m having trouble working out what the exact rules are for the region returned from view.extract_scope. I’m working on better scopes for the Lua grammar, but some of the behaviour is puzzling to me.

e.g. Here’s the visual scope for the comment delimiter, plus the text scope description. The cursor is on ‘–’:

source.lua comment.line.double-dash.lua punctuation.definition.comment.lua
http://img.skitch.com/20120225-gd5tc8et7ehahtgaec6ar4g3ps.png

This what I would expect. The innermost scope at the cursor is the comment delimiter. As you move to the right where nothing is scoped, the visual scope moves out to match the entire comment. Again, I expect this because it is the now the innermost scope since I never scoped the comment text.

source.lua comment.line.double-dash.lua
http://img.skitch.com/20120225-nrmuutmgu73tk77sg6g3n14uc4.png

Now here’s the strange one. When I highlight the word ‘function’, it matches the entire function’s scope, not the keyword ‘function’ itself. Note that the keyword is the innermost scope.

source.lua meta.function.lua keyword.control.lua
http://img.skitch.com/20120225-np8euychuun72rbc1c6suyp4j2.png

So why does this match the scope ‘meta.function.lua’ rather than ‘keyword.control.lua’?

On a related note, it would be awesome to be able to choose the scope level in extract_scope. This would allow me to visualize all scopes for a particular token. Needless to say this would be very handy when working on language grammars.

0 Likes

#2

I have the same problem. I found, that extract_scope works correctly for scopes, that have begin-end syntax matches and ignores elements, which are simply matched.
Haven’t you solved it anyhow?
And yes, it would be so cool to have a function like find(pattern, fromPosition), but with selector instead of pattern…

0 Likes

#3

I have found extract_scope to be maybe 40% useful. If you happen to be scanning for a region that is very simple, and doesn’t touch any similar regions, it seems to work okay. If you try and do any thing other than that, it gets confused in a hurry.

I tried to do something basic like find an html tag.
Basic structure is that the main scope is meta.tag and the end caps are punctuation.definition.tag.begin and punctuation.definition.tag.end.

So the idea was simple, start grabbing the extent of the cursor’s scope if it was one of the three. If you are looking left, ensure that the next character to the left was meta.tag -punctuation.definition.tag.end, if so grab that extent and add it to the current.

Do something similar when looking right but exclude the begin part. It worked well enough until you start putting these regions next to each other.

For instance, the opening tags just bleed together in this example (it would group tag.begin and tag.end together for opening tags, but not for closing; it made no sense):

<div><div><div></div></div></div>

I would find it more useful if it only would return contiguous same scopes opposed to trying to be clever and try to group similar/close enough scopes.

0 Likes