Sublime Forum

Settings.add_on_change won't work?

#1

Are there some undocumented conditions under which Settings.add_on_change will/won’t work? I can’t seem to get any callback submitted to run, despite putting in a range of existing and non-existing keys.

0 Likes

#2

code?

0 Likes

#3

Ack! Sorry, thought I had pasted it in.
The following is a base class that nearly all my commands inherit from.
Each command defines a ‘settings selector’, which is merely a prefix for all the settings relating to command.

class NimLimeMixin(object):
    self.settings = sublime.load_settings('NimLime.sublime-settings')

    def __init__(self):
        if hasattr(self, 'load_settings'):
            self.load_settings()

            key = '{0}.enabled'.format(self.settings_selector)
            print("Adding on_change callback for key '{0}'".format(key))
            self.settings.add_on_change(
                key,
                self.load_settings
            )

    def get_setting(self, key):
        formatted_key = key.format(self.settings_selector)
        if not self.settings.has(formatted_key):
            print("Warning, '{0}' not found.".format(formatted_key))
        return self.settings.get(formatted_key)

    def load_settings(self):
        print("Loading settings.")
        self.enabled = self.get_setting('{0}.enabled')
0 Likes

#4

you instantiate the class after plugin_loaded, … right? why don’t move the load_settins call to init

0 Likes

#5

I wanted to be able to share the same settings object between all class instances. Anyway, I’ve tried moving the call to init, and it didn’t solve the problem, unfortunately.

Some extra information - I’m targeting Sublime Text 2 and 3, but can only confirm that I’m getting this problem on 2 (My plugin is behaving oddly in 3, enough so that I need to fix those problems first).

1 Like