Skip to content

Instantly share code, notes, and snippets.

@asfaltboy
Last active January 28, 2019 21:21
Show Gist options
  • Save asfaltboy/7bd35dad8d1d07ac3ee68ffc0d42e7f2 to your computer and use it in GitHub Desktop.
Save asfaltboy/7bd35dad8d1d07ac3ee68ffc0d42e7f2 to your computer and use it in GitHub Desktop.
A simply SublimeText plugin to run a command if matches a selector
"""
A simply SublimeText plugin to run a command if matches a selector.
Usage example - my `Default (OSX).sublime-mousemap`:
[
// for Python we use Anaconda's goto command, for go we use go_guru,
// for others we use built-in goto command
{ "button": "button1", "modifiers": ["ctrl"], "command": "mousemap_wrap",
"press_command": "drag_select", "args": { "commands": [
{
"command": "anaconda_goto",
"run_for_selector": "source.python"
},
{
"run_for_selector": "source.go",
"command": "go_guru",
"args": {
"mode": "definition",
"output": false
},
},
{
"command": "goto_definition",
}
] } }
]
"""
import sublime_plugin
class MousemapWrapCommand(sublime_plugin.TextCommand):
def run(self, edit, commands, *args, **kwargs):
sel = self.view.sel()
(row, col) = self.view.rowcol(sel[0].begin())
point = self.view.text_point(row, col)
best_match = None
for command in commands:
if 'run_for_selector' in command:
if self.view.scope_name(point).startswith(
command['run_for_selector']
):
best_match = command
elif not best_match:
best_match = command
if not best_match:
raise Exception('No match found')
self.view.window().run_command(
best_match['command'],
args=best_match.get('args'),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment