Sublime Text 插件 class 命名约定

Sublime Text plugin class naming convention

为了在 Sublime Text 中为简单任务创建一个极简插件,我这样做:

  1. C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\Default (Windows).sublime-keymap" 中添加此行(可从菜单 首选项 > 键绑定 - 用户 获得):

    { "keys": ["ctrl+alt+enter"], "command": "myplugin123_blah" },
    
  2. 我用同名创建了一个Python文件,即myplugin123_blah.pyC:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\

  3. 我创建插件代码:

    import sublime
    import sublime_plugin
    import subprocess
    
    class Myplugin123BlahCommand(sublime_plugin.WindowCommand):
        def run(self):
            command = 'doanything'
            subprocess.Popen(command)
    

看来 "class name" 应该是:

因此:myplugin123_blah => class Myplugin123BlahCommand

这是否正确(只是通过反复试验和查看其他示例发现的)?这是 sublime_plugin.WindowCommand 的子 class 的一般命名规则吗?

通常这是从另一个方向来实现的——用一些 Text/Window 命令编写一个 Python 插件,然后 "translating" 这个名字变成一个你可以从 ST 调用的名字——但是在任何一个在这种情况下,当您不想在 Python 代码或键绑定等中使用不必要的不​​可读名称时,了解这些规则会很有帮助。

非官方文档(针对 ST3,与已弃用的 ST2 具有相同的规则)是这样解释的: http://docs.sublimetext.info/en/latest/reference/plugins.html#conventions-for-command-names

By convention, Sublime Text command class names are suffixed with Command and written as NamesLikeThisCommand. However, command names are automatically transformed from NamesLikeThisCommand to name_like_this. Thus, ExampleCommand would become example, and AnotherExampleCommand would become another_example. In names for classes defining commands, use NameLikeThisCommand. To call a command from the API, use the standardized name_like_this.

这与您的发现相吻合。