Can I create my own command in sublime and how to associate python implementation to that command?

那年仲夏 提交于 2019-12-13 00:38:18

问题


One more depth into my former question Why doesn’t this hotkey configuration for Sublime Text work?. Now I come to the implementation of sublime command, it is really a confusing way to hack what commands sublime has, as the exploration in former thread in order to find the command which is used to open a browser, finally I found it with help of @MattDMo.

Then I find there is one file named open_in_browser.py in the Packages/Default folder, I guess the commands is just the file name of .py files, but in fact I cannot find the corresponding file which could be named find_pre.py to command find_prev, then I copy open_in_browser.py as open_browsers.py, and add { "keys": ["ctrl+b"], "command": "open_browsers"} to sublime keymap, but it doesn't work. Then I realized that there should be some place which registers sublime commands to their implementation, so if there is such a mechanism, what is it? Where can I find it?


回答1:


TL;DR

Create a file with any name in the Packages/User directory. Create a class in the file like MyTestCommand with a run method. Create a keymap using the class name in snake case and without Command suffix. Use named arguments to pass anything to the command.

Full answer

There is no need to register anything to create custom commands. Filename doesn't matter as Sublime Text simply scans it's directories for .py scripts and automatically executes them (registers them).

Here is the example script I use:

import sublime
import sublime_plugin

class ChangeViewCommand(sublime_plugin.WindowCommand):
    def run(self, reverse=False):
        window = self.window
        group, view_index = window.get_view_index(window.active_view())
        if view_index >= 0:
            views = window.views_in_group(group)
            if reverse:
                if view_index == 0:
                    view_index = len(views)

            if reverse:
                new_index = view_index - 1
            else:
                new_index = (view_index + 1) % len(views)

            window.focus_view(views[new_index])

So what it does - switches to the next/previous tab in the current group (the default behavior circles around all tab groups).

So we simply save it as any name in Packages/User directory.

Then we must create the key bindings in our user keymap file:

{ "keys": ["ctrl+tab"], "command": "change_view" },
{ "keys": ["ctrl+shift+tab"], "command": "change_view", "args": {"reverse": true} },

As you may see, the command is snake_case of the class name without the Command suffix. This will run the class's run method with named arguments.

Does this answer your question? For debugging in case of any errors - open the ST console (default shortcut is ctrl + `)



来源:https://stackoverflow.com/questions/24276143/can-i-create-my-own-command-in-sublime-and-how-to-associate-python-implementatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!