Sublime Forum

Simple syntax/file type query function

#1

Is there a function that just returns the current syntax highlighting/detected file type for the file open in the active window?

Edit: Mmm, using view.syntaxName(0) would seem to work, but it’s not that obvious from the documentation at least. Not to mention needlessly wordy by passing an parameter where none should be needed for this use case IMHO.

Edit2: This function seems to return a flat Unicode string consisting of two parts. The first part being specific for the point you call syntaxName() on, the second part identifying the type of file/syntax highlighting applied, e.g.:

keyword.control.import.from.python source.python meta.preprocessor.c source.c++

Example use:

[code]import sublime, sublimeplugin

class MyPlugin(sublime.TextCommand):
def run(self, view, args):
syntax = view.syntaxName(0).split()[1]

source.python
[/code]

Edit3: jps, any chance of getting a simpler function that just returns ‘python’ or ‘c++’?

0 Likes

#2

A better option is to use the value returned by view.options().get(‘syntax’), which will give you the path to the tmLanguage used by the current buffer - this is what’s used by the status bar to show the current syntax. You’ll still have to trim off the path components though to get a friendly name.

0 Likes

#3

Just so others know what to expect (as the documentation is not that fleshed out yet):

>>> view.options().get('syntax') Packages/Python/Python.tmLanguage

[code]>>> s = view.options().get(‘syntax’)

s = s.split()[0].rsplit(’/’)[1].lower()
python[/code]

0 Likes