Sublime Forum

How to reliably check for ST2 vs ST3

#1

Quick question: the porting documentation says that the sublime.* API is “dormant” during initialization. If so, how can one reliably check for ST2 vs. ST3 in a plugin, before the plugin is called? For instance, this is needed to handle include statements in a way that works for both python2 and python3.

Right now I use sys.version_info, but this only tells me which python I’m running. If an user on Mac/Linux had python3 set as his/her main python, then under ST2 this trick would not work.

User phyllissten suggested using sublime.version (? not sure it’s the correct api), but is that guaranteed to work “deterministically”?
Thanks!

Marciano (author of the LaTeXTools plugin)

0 Likes

#2

In my CursorRuler plugin I use the following at the top to determine the ST build version before anything else happens:

[code]import sublime

It’s important for us to know if we’re running in ST3 or ST2.

Build 3010 and older of the Sublime Text 3 builds do not have the API

available during program startup. As of build 3011 sublime.version()

is available during startup. If we’re unable to get the version build

number we make the assumption that it’s 3000.

We’re also assuming that if we’re not in ST3 then we’re in ST2.

st = 3000 if sublime.version() == ‘’ else int(sublime.version())[/code]
This helps us keep the behavior of the special plugin_loaded() function consistent between ST2 and ST3:

[code]# In ST3 this will get called automatically once the full API becomes available.
def plugin_loaded():
CursorRuler.init()

if st < 3000:
plugin_loaded()
[/code]
Using the build number allows us to properly handle build-specific issues. In my plugin I dealt with inconsistent support for the add_on_change() method in this way:

[code]
@classmethod
def init(cls):
plugin_name = os.path.basename(file):-3]

    cls.editor_settings = sublime.load_settings('Preferences.sublime-settings')
    cls.settings        = sublime.load_settings(plugin_name + '.sublime-settings')

    # In Sublime Text 3 the `add_on_change()` method
    # was not implemented until build 3013.
    if st < 3000 or st >= 3013:
        cls.editor_settings.add_on_change(plugin_name.lower() + '-reload', cls.__setup)
        cls.settings.add_on_change('reload', cls.__setup)

    cls.__setup()[/code]

Perhaps nowadays checking for add_on_change() support is unnecessary but you know what I mean :smile:

0 Likes

#3

Thanks icylace. This is what phyllisstein also suggested. However, my understanding from the ST3 porting docs was that the entire sublime API was basically unavailable until the plugin was actually called: from the ST3 Porting Guide,

“Due to the plugin_host loading asynchronously, it is not possible to use the Sublime Text API at import time. This means that all top-level statements in your module must not call any functions from the sublime module. During startup, the API is in a dormant state, and will silently ignore any requests made.”

Maybe there is an exception for sublime.version(), but I’d like to see that confirmed by jps…

0 Likes

#4

If you look at the release notes for build 3012 it says:

So, those four functions may be called anytime and anywhere.

On a boring technicality note sublime.version() was made available in build 3011 but I believe 3012 was a hotfix that came out immediately after it so that’s why the release notes for 3012 include the notes for the “missing” 3011.

0 Likes

#5

Yes! Yes! Yes! Awesome, thanks! And, I guess I should have done my homework better :smile:

0 Likes

#6

Glad to be of help :smile:

0 Likes