通过Community Documentation,特别是command list section,有相当完整的Sublime 核心命令列表。然而,这并不能帮助您了解第三方软件包和插件可能已添加的命令。
在您的问题中,您提到知道如何获取命令,但不知道在其他地方使用它可能是什么。如果你知道某种方式来调用命令(键、命令面板、菜单)并且想知道命令是什么,Sublime 可以满足你。
如果你用Ctrl+`或者View > Show Console打开Sublime控制台,可以输入如下命令:
sublime.log_commands(True)
现在,无论您何时执行任何操作,Sublime 都会记录它正在执行控制台的命令,以及它可能采用的任何参数。例如,如果您打开日志记录并依次按下每个箭头键,控制台将显示以下内容:
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)以关闭日志记录。
如果您真的对每个可能的命令的内部细节感兴趣,可以使用以下内容。这实现了一个标记为list_all_commands 的命令,当您运行它时,它将列出所有类型的所有可用命令到一个新的暂存缓冲区中。
请注意,并非所有已实现的命令都必须供外部使用;插件有时会定义自己使用的辅助命令。这意味着虽然这会告诉您所有存在的命令,但这并不意味着所有这些命令都是供您使用的。
此外,虽然这大致列出了命令类上的 run 方法所采用的参数(这是 Sublime 执行命令以运行命令的方法),但某些命令可能具有模糊的参数列表。
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