Sublime Forum

View.settings().get(?)

#1

I have a plugin for a specific syntax.
I use is_visible to enable or disable it with this:

    def is_visible(self):
        view = sublime.active_window().active_view()
        if view:
            return view.settings().get('syntax') == 'Packages/Mine/Mine.tmLanguage'

I want to add a new syntax to the same package, so moved Packages/Mine/Mine.tmLanguage to Packages/Mine/syntax/Mine/Mine.tmLanguage and added Packages/Mine/syntax/New/New.tmLanguage.

Due to the relocation, I need to update the is_visible method.

Is there a way to use view.settings().get('???') to get the name of the syntax, rather than the path so it is location independent?

I have not found a list of strings that can be used with the get() method.

0 Likes

#2

Iirc, the name attribute (shown in ST’s bottom-right corner) which is defined inside the syntax file is not exposed. At least, last time when I want to get it, I have to parse the syntax file to get it by myself.

Note: I didn’t actually parse the full file but just used a simple regex to find it.


I may suggest just use the syntax path but define those magic strings as constants somewhere for better maintainability.

0 Likes

#3

Yeah, that was what I first thought to check.
Since it is not exposed, I solved it by checking selectors rather the syntax:

def is_visible(self):
    return self.view.match_selector(0, "source.whatever")

Thanks

2 Likes

#4

I think that checking the selector rather than the syntax is the best behavior. This is location-independent and even works if you’re using a different syntax for the same language.

However, if you do really need to get the syntax name, you can do so with sublime_lib, though it isn’t very efficient:

from sublime_lib import list_syntaxes

syntax_name = next(syntax for syntax in list_syntaxes() if syntax.path == 'Packages/Mine/Mine.tmLanguage').name
0 Likes

#5

Or you settle for the file’s basename, which is going to be the same as the syntax’s name field most of the time and much easier to compute. Regardless, self.view.match_selector is the way to go here.

0 Likes