Sublime Forum

Snippet scope restricted to specific syntax

#1

I created a snippet, and it is getting annoying because it fires with any sort of file.

I want the snippet to run only on a specific syntax (which I apply via first_line_match, because there is no associated file extension).

All the information I find online sets the source based on a file extension, but how can I set it based on a specific syntax?

0 Likes

#2

Scope selectors are based on the syntax that is used for a file, which is generally done by file extension, but as you noted you can also make a syntax apply based on first_line_match in a syntax definition as well. It’s just a convention that source code files have a scope that starts with source and ends with the more common extension of the file type, but this is not always the case. For example, the scope for C# is source.cs for C#, but Python is source.python.

The scope tag in the snippet is matched against the scope at the cursor, so in the general case to constrain a snippet only to a particular type of file you should set the scope tag in the snippet to the same as the scope attribute in your syntax definition. You can also use Tools > Developer > Show Scope Name from the menu to see what the full scope is at the cursor location; the first line in the popup is the scope that describes the file type.

This video has a bunch to say on the particular topic of scopes and how they match.

2 Likes

#3

Thanks for the informative, didactic video pointer, I still have so much to learn!

I will give it another thought to see if I can find an easy solution.

0 Likes