【问题标题】:How to set or find the command name of a Sublime Text plugin如何设置或查找 Sublime Text 插件的命令名称
【发布时间】:2021-01-06 16:56:07
【问题描述】:

我正在尝试为我的节点 js 服务器编写 ST3 插件。为了运行它,我调用了命令view.run_command('Node js.nodejs')

我的 Sublime Text Packages 文件夹如下所示:

│   main.py
│   text_example_one.py
│
├───Node js
│       Nodejs.py
│
└───User
    │   main.py
    │   Package Control.last-run
    │   Package Control.sublime-settings
    │   Preferences.sublime-settings
    │
    └───Package Control.cache
            01524fae79697630d0454ba3fabd9414
            01524fae79697630d0454ba3fabd9414.info

../Packages/Node js/Nodejs.py 文件包含以下代码:

import sublime, sublime_plugin

class TryCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        print("It's working")
        self.view.insert(edit, 0, "Hello, World!")

view.run_command('Node js.nodejs') 被调用时,什么都没有发生,参见the image of the window here

未引发错误,但未插入 "Hello, World!" 消息且未在控制台中打印 "It's working"

【问题讨论】:

    标签: sublimetext3 sublimetext sublime-text-plugin


    【解决方案1】:

    view.run_command('Node js.nodejs') 命令不会调用您的插件。

    要运行您的插件,您需要调用try 命令,例如view.run_command("try")。以下是原因的解释:

    Sublime Text 插件的命令名称来源于它们的类名。比如下面这个类...

    class PrintHelloInConsoleCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            print("Hello")
    

    ...可以通过调用print_hello_in_console 命令来运行。例如

    // Run from a plugin or in the console
    view.run_command("print_hello_in_console")
    
    // Run from keys by adding a key binding
    {"keys": ["ctrl+shift+y"], "command": "print_hello_in_console"},
    
    // If instead of a TextCommand the plugin had been a sublime_plugin.WindowCommand
    // then the following line would be needed to run the command in the console.
    window.run_command("print_hello_in_console")
    

    要从类名中获取命令名,首先从类名中删除Command 后缀。其次将类名的剩余部分从CamelCase 转换为snake_case。因此,定义class PrintHelloInConsoleCommand 的插件由print_hello_in_console 命令 调用。

    • 类名是:PrintHelloInConsoleCommand
    • 从类名中删除 Command
      PrintHelloInConsoleCommand --> PrintHelloInConsole
    • CamelCase 转换为 snake_case
      PrintHelloInConsole --> print_hello_in_console
    • 要调用的命令名称是:print_hello_in_console

    您的课程class TryCommand(sublime_plugin.TextCommand) 可以通过调用try 命令运行,即view.run_command("try")


    这里有一些其他的例子:

    • class ClearConsoleCommand(sublime_plugin.WindowCommand)
      "clear_console" 命令
    • class InsertDateTimeCommand(sublime_plugin.TextCommand)
      "insert_date_time" 命令
    • class TestOKCommand(sublime_plugin.TextCommand)
      "" 未创建命令 - 不要使用大写单词,例如"OK" 中的 "TestOK"。请注意,这不会创建 "test_o_k" 命令
    • class MoveSelection(sublime_plugin.TextCommand)
      "move_selection" 命令 - 尽管在类名中省略了 "Command",但它仍然有效。在撰写本文时,ST 并未严格执行该要求(但在未来的版本中可能会有所改变)
    • class AutoSaverEvents(sublime_plugin.EventListener)
      "" 未创建命令 — 未调用事件侦听器,因此未创建命令,ST 不希望类名以 "Command" 结尾

    有关插件的更多信息,请参阅 Sublime Text Unofficial Documentationplugins section,其中包含的信息比官方文档要多得多。

    【讨论】:

      猜你喜欢
      • 2013-09-23
      • 1970-01-01
      • 2018-12-07
      • 2014-10-23
      • 2014-04-14
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多