Search code examples
pythonsublimetext3sublimetextsublime-text-plugin

Get file syntax selection in sublime text 3 plugin


I have a very small plugin to open a perl file module starting from the use statement. It's really basic and it just replaces '::' with '/' and then if the file exists in one of the paths specified in PERL5LIB, it opens it. I want it to run only when the open file syntax is selected as perl. Is there any API to get that information? This is the code that I have now:

class OpenPerlModule(sublime_plugin.TextCommand):
    def run(self, edit=None, url=None):
        perl_file = url.replace("::", "/")
        perl_dirs = os.environ.get('PERL5LIB')
        for perl_dir in perl_dirs.split(':'):
            if (os.path.exists(perl_dir + '/' + perl_file + '.pm')):
                self.view.window().open_file(perl_dir + '/' + perl_file + '.pm')
                return

(The OS is Ubuntu)


Solution

  • Here is the code snippet you're looking for

    self.view.settings().get("syntax")
    

    You should check whether it's a syntax related to Perl or not. I suggest something like this:

    syntax = self.view.settings().get("syntax")
    syntax.endswith("Perl.tmLanguage") or syntax.endswith("Perl.sublime-syntax")
    

    The second or clause is to cover the new syntax that's introduced in >=3080