如何列出 Sublime Text 3 中的所有命令 [英] How to list all commands in Sublime Text 3

查看:52
本文介绍了如何列出 Sublime Text 3 中的所有命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 sublime text 3 中所有可用命令的列表(内置和来自包)

I'd like to get a list of all available commands in sublime text 3 (built-in and from packages)

我想做什么:

我正在尝试为包命令创建快捷方式,但我不知道该命令的名称.我可以找到命令并使用 alt + shift + p 使用它,但是当我尝试将快捷方式添加到我的 .sublime-keymap 文件时,我不确定穿上 "command": "?" 位.如果我能列出所有命令和 grep 来查找我要查找的内容,然后将正式命令名称复制粘贴到键盘映射文件中,那我就太好了.

I'm trying to create a shortcut for a package command but I don't know the name of the command. I can find the command and use it using alt + shift + p but then when trying to add the shortcut to my .sublime-keymap file, I'm not sure that to put on the "command": "?" bit. I'd be great if I could just list all commands and grep for what I'm looking for then just copy paste the formal command name into the keymap file.

我想探索所有可用的命令(内置和来自包)以了解 Sublime Text 功能.与其在 sublime 中搜索命令或在线阅读教程,不如问我的

I'd like to explore all the commands that are available (built-in and from packages) to understand Sublime Text capabilities. Rather than searching for commands within sublime or reading tutorials online, I'd like to ask my editor:

你能做什么?

而不是

你能做到吗?

推荐答案

Sublime 中有一个相当完整的核心命令列表,可通过 社区文档,特别是在命令列表部分.然而,这并不能帮助您了解第三方软件包和插件可能已添加的命令.

There is a fairly complete list of the core commands in Sublime available via the Community Documentation, in particular in the command list section. This however doesn't help you to learn about commands that third party packages and plugins may have added, however.

在您的问题中,您提到知道如何获取命令但不知道为了在其他地方使用它而可能是什么.如果您知道调用命令的某种方式(键、命令面板、菜单)并想知道命令是什么,Sublime 可以满足您的需求.

In your question you mention knowing how to get at a command but not knowing what it might be for the purposes of using it elsewhere. If you're in the situation of knowing some way to invoke a command (key, command palette, menu) and wondering what the command is, Sublime has you covered.

如果您使用 Ctrl+`View > 打开 Sublime 控制台;Show Console,可以输入如下命令:

If you open the Sublime console with Ctrl+` or View > Show Console, you can enter the following command:

sublime.log_commands(True)

现在无论你做什么,Sublime 都会记录它正在执行控制台的命令,以及它可能需要的任何参数.例如,如果您打开日志记录并依次按下每个箭头键,控制台将显示:

Now whenever you do anything, Sublime logs the command that it's executing the console, along with any arguments that it might take. For example, if you turn on logging and press each of the arrow keys in turn, the console will display this:

command: move {"by": "lines", "forward": false}
command: move {"by": "lines", "forward": true}
command: move {"by": "characters", "forward": false}
command: move {"by": "characters", "forward": true}

使用此工具,您可以确定各种操作执行的命令,以便您可以在其他地方使用它们.例如,这也是诊断诸如键盘快捷键之类的东西似乎没有做您认为它们应该做的事情的方便技术.使用 False 而不是 True 运行相同的命令(或重新启动 Sublime)以关闭日志记录.

Using this facility you can figure out what commands various actions take, so that you can use them elsewhere. This is also a handy technique for diagnosing things like keyboard shortcuts that don't seem to do what you think they should do, for example. Run the same command with False instead of True (or restart Sublime) to turn the logging off.

如果您真的对每个可能命令的内部细节很感兴趣,那么可能会出现以下情况.这实现了一个标记为 list_all_commands 的命令,当你运行它时,它会将所有类型的所有可用命令列出到一个新的暂存缓冲区中.

If you're really interested in the gritty internal details of every possible command, something like the following is possible. This implements a command labelled list_all_commands which, when you run it, will list all of the available commands of all types into a new scratch buffer.

请注意,并非所有已实现的命令都必须供外部使用;插件有时会定义帮助命令供自己使用.这意味着虽然这会告诉您所有存在的命令,但这并不意味着所有命令都适合您使用.

Note that not all implemented commands are necessarily meant for external use; plugins sometimes define helper commands for their own use. This means that although this tells you all of the commands that exist, it doesn't mean that all of them are meant for you to play with.

此外,虽然这里粗略地列出了命令类上的 run 方法采用的参数(这是 Sublime 为运行命令而执行的),但某些命令可能具有模糊的参数列表.

Additionally, although this lists roughly the arguments that the run method on the command class takes (which is what Sublime executes to run the command), some commands may have obscure argument lists.

import sublime
import sublime_plugin

import inspect

from sublime_plugin import application_command_classes
from sublime_plugin import window_command_classes
from sublime_plugin import text_command_classes


class ListAllCommandsCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.view = self.window.new_file()
        self.view.set_scratch(True)
        self.view.set_name("Command List")

        self.list_category("Application Commands", application_command_classes)
        self.list_category("Window Commands", window_command_classes)
        self.list_category("Text Commands", text_command_classes)

    def append(self, line):
        self.view.run_command("append", {"characters": line + "\n"})

    def list_category(self, title, command_list):
        self.append(title)
        self.append(len(title)*"=")

        for command in command_list:
            self.append("{cmd} {args}".format(
                cmd=self.get_name(command),
                args=str(inspect.signature(command.run))))

        self.append("")

    def get_name(self, cls):
        clsname = cls.__name__
        name = clsname[0].lower()
        last_upper = False
        for c in clsname[1:]:
            if c.isupper() and not last_upper:
                name += '_'
                name += c.lower()
            else:
                name += c
            last_upper = c.isupper()
        if name.endswith("_command"):
            name = name[0:-8]
        return name

这篇关于如何列出 Sublime Text 3 中的所有命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆