I added a right click context menu to sublime text 3 by creating a Context.sublime-menu file in Packages/user folder with the following configuration:
[
{
"caption": "wrap with try",
"mnemonic": "t",
"id": "try",
"command": "insert_snippet",
"args": { "name": "Packages/User/js-try.sublime-snippet" }
}
]
This works fine. But I want it to appear only when the current file is a javascript file, just like you can assign a scope to a snippet by configuring <scope>source.js</scope>. I did not find any documentation on this on the one hand, on the other hand I see that there are context menu's that are behaving this way, so I know its possible.
Does anyone know how to achieve this?
Menu item visibility is controlled by the command being referenced. See the is_visible method at https://www.sublimetext.com/docs/3/api_reference.html#sublime_plugin.TextCommand.
As the built in insert_snippet command always returns True for this method, to achieve this, you will need to write a (small) custom plugin which will act as a wrapper for your desired command:
import sublime
import sublime_plugin
class ProxyCommand(sublime_plugin.TextCommand):
def run(self, edit, command_name, scope_selector, **kwargs):
self.view.run_command(command_name, kwargs)
def is_visible(self, command_name, scope_selector, **kwargs):
return self.view.match_selector(self.view.sel()[0].begin(), scope_selector)
proxy_command.py - the base filename doesn't matter, only the extension mattersproxy command we just created, and passing in arguments to tell it which scope (selector) it should be active for, and which real command to execute and with what arguments.)[
{
"caption": "wrap with try",
"mnemonic": "t",
"id": "try",
"command": "proxy",
"args": {
"command_name": "insert_snippet",
"name": "Packages/User/js-try.sublime-snippet",
"scope_selector": "source.js",
},
}
]
You could take this a step further and build a general purpose snippet wrapper command, which could read the snippet file (using the sublime.load_resource API and then parse the XML) to see if the <scope> specified there matches, rather than requiring it to be entered(/potentially duplicated) in the context menu entry.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With