Sublime Forum

Scoping sections of a SQL query -- feedback requested

#1

I’m polishing up an old syntax I wrote for Oracle SQL. A key feature is detailed meta scopes. Consider the following:

with foo as (
    select * from tableA
)
select x, y, z
from foo
where x = 1

The entire snippet would be contained within meta.query. Then, parts of the query would have their own meta scopes.

The first approach I tried was to use new meta scopes for parts of the query. For instance, select x, y, z would be scoped as meta.query meta.select. But this can result in very deeply nested scopes. For instance, the select * on the second line would be meta.query meta.with meta.query meta.select. This can be problematic for very complex queries.

The second approach that I’m using now is to emulate scopes like meta.function and meta.function.parameters. Thus, select x, y, z would be scoped meta.query.select and select * would be scoped meta.query.with meta.query.select. I think that this should work for SQL because a query is unambiguously partitioned into distinct sections. However, I rarely see another syntax use this approach.

What do people think about this? Is there a good reason to prefer one approach or the other?

0 Likes

#2

I personally prefer your second approach for meta scopes as it ties the parts together much more clearly. I guess the reason why you won’t find it too often in custom syntaxes is the required complexity of rules with a lot more push/set statements triggered by look aheads in order to prevent meta scopes from overlapping. It takes a while to understand possible ways and find the best strategy to accomplish that. Another reason might be some of the sublime-syntax files were converted from tmLanguage format which was not able to do something comprehensive like that.

1 Like