Sublime Forum

Avoid single plugin file from beeing loaded in older ST versions

#1

Which is the recommended way/how would you cancel the load of a plugin in a condition.

I would like to use the new features like phantoms or hover listener inside packages, which should also support old Sublime Text versions and even ST2.
This would e.g. be a ViewListener, which adds Phantoms according to some logic. For backward compatibility it would be the easiest to just avoid the interpretation of this file in older Sublime Text versions.
I.e. something like:

if not sublime.version() >= "3118":
    # cancel file parsing
  • Raising an exception would be an opportunity, but this would log an console error and hence look like an error.
  • Wrapping the rest of the file into the if block would just add an indent level, which obviously is not nice either.

Do you know a better option to do so?

1 Like

#2

It seems like a clean solution might be to store the file in question in a location that isn’t automatically loaded and create a file that loads files as needed, e.g., using a directory structure something like this

root/
|__ loader.py
|__ optional/
|| __init__.py
|| v3118.py

Then in loader.py (or whatever), you can just do

if sublime.version() >= '3118':
    from .optional import v3118

This has some downsides if you just insert something like loader.py to load one optional module (since the whole structure requires 3 files and a subfolder), but properly used such a layout could leave you with a single file automatically loaded by ST that loads whatever additional files you need according to whatever conditions (order, version, etc.).

3 Likes

#3

Thanks, that seems to work quite well. However one must also raise the commands and listener to the top level.

Working example (if someone else is interested):

v3118.py:

import sublime
import sublime_plugin


class TestPhantomListener(sublime_plugin.ViewEventListener):
    def __init__(self, view):
        self.view = view
        self.phantom_set = sublime.PhantomSet(view, "ps_test")

    def on_selection_modified(self):
        view = self.view
        x = view.find_all("test")
        phantoms = [
            sublime.Phantom(r, "x", 0)
            for r in x
        ]
        self.phantom_set.update(phantoms)

loader.py

import sublime

if sublime.version() >= "3118":
    from .optional import v3118
    # reload the module instead of restarting ST
    # to propagate changes
    import imp
    imp.reload(v3118)
    # raise all commands and listener to the top level
    TestPhantomListener = v3118.TestPhantomListener
1 Like