Sublime Forum

Looking for advice on how to factor plugins

#1

Hi,

I’m trying to publish some of power-typing plugins. There’s a few different elements and I’m trying to publish things separately in order to keep things modular but I’m running up against dependency/precedence problems.

To simplify, consider the following scenario: plugin A does something special on “left arrow” in a certain context; plugin B does something else on “left arrow” regardless of context; and we would like plugin A to take precedence over plugin B if both plugins are installed, when plugin A’s context is met.

If B and A are bundled as a single plugin then of course we can solve this by creating a context for B. (Namely, fire only if A’s context is not met.)

But can one achieve this if A and B are separate plugins, and B should always fire if A is not installed? Can a plugin test if another is installed/active? Is my only way forward to offer bundled and un-bundled versions of the plugins? (Eg., “A”, “AB” and “B” versions.)

Thank you!

0 Likes

#2

You can try to locate a file from plugin B inside your plugin A using sublime.find_resources("plugin_b.py") (supposing a file from plugin_b has the name plubin_b.py)
It will return an empty array if it cannot find it.

0 Likes

#3

Thanks. I have some questions:

  1. can plugin B check if plugin A is actually enabled?
  2. will this work if plugin A hasn’t been extracted, and is still in its .sublime-package file?
0 Likes

#4

If both plugins are maintained by you. You can set a flag for a view like view.settings().set('plugin_loaded_A', True) to mark that plugin A is loaded in the view. Plugin B can get the variable via view.settings().get('plugin_loaded_A', False).

Note that plugin B may be loaded before plugin A so plugin B cannot detect plugin A correctly at first. But it seems not important in your use case.

0 Likes

#5

I suppose the easiest method is to specify a custom context manager that A implements and B references but in a way that makes the context match when it’s not available. I don’t know what happens with contexts whose keys aren’t defined, but this would be something I would try. See on_query_context.

sublime.find_resources honors the ignored_packages settings, so it won’t be able to find any resources in disabled packages.

Also I presume you’re not actually looking to bind “left arrow” in all cases and this is just for illustration purposes.

0 Likes

#6

What I will try to do is set an on_query_context listener for plugin B that uses jfcherng’s suggestion to detect if plugin A is there, and if so to give precedence. Will let you know if I succeed. (Or rather, if I fail.)

@FichteFoll Yes I am only binding “alt+left”, “alt+right” in all cases, for plugin B. And plugin A is very simple, basically described in this post.

0 Likes