Sublime Forum

Loading Settings the Easy Way - Declarative Settings Loader for Sublime Text 2 & 3

#1

Hello all! I am here yet again with another nifty & time-saving utility for use in all your wonderful plugins.

This time I would like to present an expressive and easy way to load settings through the use of declarative structures and class attributes - the SublimeDeclarativeSettings plugin.

With this plugin, all you have to do is have your commands declare the setting keys, attributes to bind to, and the default values, and the loader will take care of loading and updating the values:

from DeclarativeSublimeSettings import DeclarativeSettingsMixin

class WordFinder(DeclarativeSettingsMixin, ApplicationCommand):
    setting_entries = (
         ('enabled', 'word_finder.enabled', True) 
    )

    def __init__(self):
        self.load_settings("word_finder.sublime-settings")

    def run(*args, **kwargs):
        print(self.enabled)

This is a very useful solution if you have a lot of command-specific settings, and the declarative approach means you don’t have to worry about loading and reloading the settings. There are a couple other features, such as embedding/reusing other setting trees, and extending how entries are processed. A full outline can be found at the repository.

I’ve currently submitted a pull request to the main Package-Control repository so that this can be added as a dependency.

1 Like

#2

I’ve wanted to to something similar to this, actually. I did do something similar already, but I did not branch it off into a depencency (because they didn’t exist back then and I have more plans for “useful utilities”, just not the time).

I’ll just leave this here.


I will review your actual code later when I get to it in the package control channel.

0 Likes

#3

@FichteFoll Oh neat! My mixin class isn’t quite as featureful as your settings object - I was aiming for simplicity. Additionally, tuples carry less overhead than dictionaries, and since I didn’t really anticipate any need to change the set of settings watched, I figured that a static structure of tuples would be best (I should probably use an abstract base class to test that the structure is correct).

As with my previous tools, this is actually split off from the plugin I work on, NimLime.

0 Likes

#4

By the way, you might be interested in how I used this structure to override it for the InactivePanes package: https://github.com/SublimeText/InactivePanes/blob/ca417aa6699432ffbbd716ea44e8d431a8d5818f/inactive_panes.py#L66-L106

I needed this because I am overriding the color_scheme setting in the view but still want to be notified when the underlying setting (e.g. from user, project or per-syntax settings) changes.
That might even use a more recent version of it, actually, but I don’t exactly remember.

0 Likes