Home Download Buy Blog Forum Support

Simple syntax/file type query function

Simple syntax/file type query function

Postby asmodai on Sun Aug 08, 2010 10:03 pm

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.:

Code: Select all
keyword.control.import.from.python source.python
meta.preprocessor.c source.c++


Example use:

Code: Select all
import sublime, sublimeplugin

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

>>> source.python


Edit3: jps, any chance of getting a simpler function that just returns 'python' or 'c++'?
asmodai
 
Posts: 73
Joined: Mon Mar 01, 2010 10:42 am

Re: Simple syntax/file type query function

Postby jps on Sun Aug 08, 2010 11:27 pm

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.
jps
Site Admin
 
Posts: 3058
Joined: Wed Mar 19, 2008 12:33 pm

Re: Simple syntax/file type query function

Postby asmodai on Mon Aug 09, 2010 5:23 am

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

Code: Select all
>>> view.options().get('syntax')
Packages/Python/Python.tmLanguage


Code: Select all
>>> s = view.options().get('syntax')
>>> s = s.split()[0].rsplit('/')[1].lower()
python
asmodai
 
Posts: 73
Joined: Mon Mar 01, 2010 10:42 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 6 guests