Sublime Forum

Plugin extension target/scope

#1

Hi all. Short-term reader, first-time poster.

I’ve written a simple plugin that is designed to create/manage todo lists. I’ve gotten the python code working, more or less, so I can transform lines(s) of text into checklist items with a checkbox at the start that can be toggled with a keypress. So far I’ve ony worked/tested in Sublime Text 3, if that’s relevant.

I created a simple language definition so I have syntax-highlighting, and this all works fine. The problem I’m having is that I can’t seem to get my plugin to only operate when the user is editing files with a “.checklist” extension. I’ve looked at other plugins and it seems that the “extensions”: “checklist”] part of the plugin config file (in this case, Checklist (OSX).sublime-settings) should make this work, but my commands still run on files of any type.

Any help gratefully received.

0 Likes

#2

If you are using a custom language definition you could add context arguments to the key mapping where custom_lang is your new syntax.

{
      "keys": "f1"], "command": "my_command",  "context":
            
                {"key": "selector", "operator": "equal", "operand": "source.custom_lang"}
            ]
    }
0 Likes

#3

That’s really helpful, thanks so much!

Loking at this, and examining other plugin keymap, I can see I was missing this now. It makes me wonder - is it possible to have the context at a global (plugin) level for all shortcuts rather than having to specifiy for each individual shortcut? I only have 2 keyboard shortcuts so it’s no big deal, but I can imagine that if you had many shortcuts it seems like a lot of repetition and bloat in the keymap file.

Thanks again!

0 Likes

#4

I think you could do a check in your plugin for the scope and limit it that way. I have not tested this, but something like this may work:

def checkScope(self):
    myscopes = "scope1", "scope2"]
    cursor = view.sel()[0].begin()
    curr_scope = view.scope_name(cursor)

    return (curr_scope in my_scopes)

This way your plugin always executes but can exit if the scope is not valid. This may cause problems if you are overwriting an existing key map or if another plugin uses a similar key mapping. Having the context entry on the key mapping makes your entry more specific so there is less of a chance of your breaking another plugin.

0 Likes

#5

[quote=“huot25”]
This way your plugin always executes but can exit if the scope is not valid. This may cause problems if you are overwriting an existing key map or if another plugin uses a similar key mapping. Having the context entry on the key mapping makes your entry more specific so there is less of a chance of your breaking another plugin.[/quote]

Exactly. I highly discourage you from doing this since, if another plugin did define the same keybindings, they will likely be overridden. Unfortunately there is no way to specify a general context for all of a package’s or file’s key bindings, but you can use the multi-cursor or search-and-replace features of ST to aid you here.

You might also want to check out docs.sublimetext.info/en/latest/ … dings.html and the reference (which is linked on the page).

0 Likes