getack
getack

Reputation: 272

Sublime Text 3 - Set syntax for filetype in package/plugin

I am busy making a sublime text plugin/package that will ease development of lua scripts in my workplace.

We have several lua files with different extensions depending on their purpose. I want ST3 to give the proper lua syntax to these files.

I know you can set ST3 to remember syntax for a specific file extension and this is saved as a (in my case) Lua.sublime-settings file in AppData\Roaming\Sublime Text 3\Packages\User

However, if I put this file in my new plugin's folder, it's ignored.

Am I doing something wrong or is what I want not possible?

Upvotes: 0

Views: 789

Answers (1)

OdatNurd
OdatNurd

Reputation: 22791

This doesn't work because syntax specific settings are only loaded from the package that defines the syntax and from the User package (so the user can customize them).

The following is a quote from the official documentation on settings:

Settings files are consulted in this order:

1. Packages/Default/Preferences.sublime-settings
2. Packages/Default/Preferences (<platform>).sublime-settings
3. Packages/User/Preferences.sublime-settings
4. <Project Settings>
5. Packages/<syntax>/<syntax>.sublime-settings
6. Packages/User/<syntax>.sublime-settings
7. <Buffer Specific Settings>

The only places where <syntax> is referenced is from the package itself and from the user package.

If I had to guess, I would say that this is because outside of the original package author that defined the syntax, all other settings would be considered user customizations, and those settings need to be in the User package (specifically in the root of it) to ensure that they're loaded last.

A simple (but undesirable) solution would be to document for the user that they have to take this step manually.

Another approach would be to add some plugin code that extends the settings when your plugin is loaded:

def plugin_loaded():
    settings = sublime.load_settings("Lua.sublime-settings")
    extensions = settings.get("extensions", [])

    if "blarb" not in extensions:
        extensions.append("blarb")
        settings.set("extensions", extensions)
        sublime.save_settings("Lua.sublime-settings")

If you go this route you may want to include an extra sentinel setting somewhere (in settings specific to your package or some such) that says if you did this or not instead of just forcing the setting in as the example above does.

In practice you would then check if you've set that sentinel or not instead of forcing the extension in, so that if the user decides to use some other syntax for your files you're not forcing them into the Lua syntax.

It's also possible to define your own syntax that just embeds the standard Lua syntax, which allows this to Just Work™ without having to write any code or have the user do anything:

%YAML 1.2
---
name: Blarb
scope: source.lua
file_extensions:
  - blarb

contexts:
  main:
    - include: scope:source.lua

When you do this, the scope in the file will still be source.lua because that's what the scope in the syntax file says. and the status line will set the syntax name to Blarb. You could modify either of those to change the top level scope or displayed name, if desired.

An example would be to change the scope to source.blarb so that you could create key bindings/snippets that only apply to Lua files of your specific variant.

A potential downside/feature of this is that since the name of the syntax specific settings comes from the name of the file that provides the syntax, if the user has any Lua specific settings, they won't apply to your Blarb files by default.

Similarly anything that's specific to Lua by checking for a scope of source.lua won't work in Blarb files for same reasons, which may or may not be an issue.

Upvotes: 2

Related Questions