Sublime Forum

Inter-Plugin communication - How To?

#1

Is there an inbuilt method to let plugins talk to each other?

(Rather than using heavy weight solutions like zmq)

I need one plugin to see a set of integers generated in another plugin whenever needed.
See my discussion with jisaacks the author of the popular GitGutter on GitHub: here

Sublime’s API doc are not helpful and the only thread on the forum I was able to dig up did not provide any instructions.

0 Likes

#2

I’ve used settings files for this before. Basically you load a settings file that doesn’t exist, but don’t save it. It’s created in memory. You can set data to it and from the other plugin load the settings file that’s stored in memory and read the data.

Or you can create an API in your plugin that other plugins can call. You just import the plugin and call it’s API to share the data.

There are lots of ways you could share data.

4 Likes

#3

Obviously you should give your settings file in the first example a very unique name. But I wanted to say it just in case :slight_smile:.

0 Likes

#4

@facelessuser Thanks four your in-depth reply!

I like to test both methods you describe to see which one is most suitable to my purposes:

1) The idea with in-memory settings files sounds very practical to me. With the help of the API doc I should be able to figure out how to do it.

2) Just importing the other module would be the most simple solution.

I am not sure whether I understand this method correctly. I am expecting that the import would just generate an additional instance of the imported plugin, whose state is not shared with that loaded by Sublime itself. Would it work like in the example below?

Plugin 1:


class ShareThis:
share_list = []

ShareThis.share_list.append(“test”)

Plugin 2:

from path_to_plugin1.plugin1 import ShareThis

print(ShareThis.share_list)

output: [“test”]

Related question to the thread’s topic: How can I define another plugin as a dependency.
Will PackageControl installe those dependend plugins automatically?

0 Likes

#5

I don’t really have an understanding of what kind of data you are sharing and for what purpose. I’m just throwing things out.

Ideally you could avoid importing the other plugin and just create a sublime command that the other plugin calls. On receiving the call, it could save away the data in global variable or something like that.

I personally like the settings method as you could technically obtain return info as well by having the receiver write return data to the settings on completion. Sublime commands don’t really offer a return, but you could write return data in the settings file. Its kind of like using a named pipe to send data. The data essentially gets serialized to a buffer that is sharable. You can even add a callback for on_change events when settings change.

In the ColorHelper plugin, I do this to retrieve the color from the ColorPicker plugin:

            s = sublime.load_settings('color_helper_share.sublime-settings')
            s.set('color_pick_return', None)
            self.view.window().run_command(
                'color_pick_api_get_color',
                {'settings': 'color_helper_share.sublime-settings', "default_color": color[1:]}
            )
            new_color = s.get('color_pick_return', None)

Its possible you could pull off the same thing by sending a dictionary as a variable to the other plugin’s command as the variable is mutable reference. I’ve never tried it that way, but it might work.

How can I define another plugin as a dependency.
Will PackageControl installe those dependend plugins automatically?

You can only add modules as dependencies, not Sublime plugins. Usually I just add a note in the plugin that mentions prerequisites. In that sense I feel it is no different that plugins that require external binaries, like plugins that call node.js, send files to external programs. The user has a little setup, but then they are good to go.

2 Likes

#6

Thank you for the detailed code @facelessuser!

I have created a Gist from this, extended to a more elaborate example with a producer plugin sending data to its consumer pendant.

The Gist may serve those users among us interested in inter-plugin communication as a starting point:

Gist

2 Likes