在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}
通过这个工具,您可以了解各种操作所采用的命令,以便在其他地方使用它们。这也是一个有用的技巧,用于诊断像键盘快捷键之类似乎不起作用的东西。例如,运行相同的命令,但将True
替换为False
(或重新启动Sublime)以关闭日志记录。
如果您真的对所有可能的命令的细节感兴趣,可以尝试以下操作。这将实现一个带有标签list_all_commands
的命令,当您运行它时,它会将所有可用的命令的所有类型列入新的scratch缓冲区。
请注意,并非所有已实施的命令都必须供外部使用;插件有时会为自己的使用定义辅助命令。这意味着尽管这告诉您存在的所有命令,但并不意味着所有命令都适合您使用。
此外,虽然这大致列出了命令类的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