Sublime Forum

Plugin for only a specific file type

#1

Is there a way to restrict a plugin to only a specifc file type. All the menu items for the plugin only appear if a certain file type is being edited.

0 Likes

#2

You can’t restrict a plugin, no; plugins are either loaded or not loaded (based on whether the package that they’re contained in is in the list of ignored_packages or not), so they are either available everywhere or available nowhere.

That said, plugins implement commands to be triggered by keys or menu items and event listeners that react to things happening; it’s certainly possible for a command or an event listener to not take actions based on the type of the file.

In particular, a view represents a file, and the method view.match_selector() can be used to determine the type of the file that is contained in it.

In the case of event handlers, they can use that method on the view that dispatched the event to see if it’s the right kind of file and then do nothing if it’s not.

Commands have two methods that control when they are available; is_visible and is_enabled.

is_visible is called every time a menu or the command palette is about to appear and the command is contained inside. If the method returns False, the command won’t appear; so it would be hidden from a menu or the command palette, but still be executable by a key binding or by some other plugin running it.

is_enabled is called every time a command is about to be called to see if the command should be enabled or not. If the method returns False, the command is disabled and won’t run. Key bindings and plugins can’t run the command, menu entries will appear grayed out (unless the command also hides itself) and the command won’t appear in the command palette because it only displayed commands that you can execute right now.

The default for both methods is to return True, meaning the command is always visible and enabled.

1 Like