Sublime Forum

Where to put the processing of settings code in a plugin with multiple WindowCommands?

#1

I plan to add two more WindowCommands to my plugin. Currently I load the settings and build my data structure on the __init__ method of my only WindowCommand. If I put multiple WindowCommands in the plugin where should the initialisation be placed?

Is there an method that is called on the first loading of the plugin during startup?

0 Likes

#2

You might want to use plugin_loaded for that. See the API docs.

0 Likes

#3

Thanks @rwols, I will try that approach.

0 Likes

#4

Now, to a similar question:

What’s the recommended approach to parse the settings? I looked at a few packages and most of them parse them during the lauch of the Window/EditCommand. If I understand it right then they don’t parse the settings at the start of ST and the initial loading of the package, instead the parse them on each launch of the command.

Is that a good approach?

0 Likes

#5

Opinions differ on this, but in my humble opinion that is indeed a good approach, because it allows users to change their settings and see the effects immediately without restarting Sublime.

1 Like

#6

Good point. I see it similarly. The processing of a few JSON objects couldn’t take that long each time and in that way you don’t need to catch if the user has updated his settings. :slight_smile:

0 Likes

#7

I think if you write a command you shouldn’t worry about the speed of sublime.load_settings(...).get(..), because it is very fast and ST seems to cache the settings in the background. Also if you put stuff into the plugin_loaded function you slow down the start of Sublime Text.
With an on_modified listener I would try to make it as fast as possible and even try to avoid that little overhead if that’s possible without a disadvantage.

1 Like

#8

This is my experience too. load_settings is super fast and you can waste a couple milliseconds in a command. Usually it’s not even the first thing you need to optimise for eventlisteners either:

 Subsequent calls to load_settings() with the base_name will return the same object, and not load the settings from disk again.

It’s possible to subscribe to settings changes if you do want to optimise this, but the added complexity is usually not worth it and you don’t really want to slow down load/start either (e.g. the user might not even need your commands this session).

0 Likes