我正试图为我的节点js服务器编写一个ST3插件。为了运行它,我调用命令view.run_command('Node js.nodejs')
。
我的Sublime TextPackages
文件夹如下所示:
│ 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')
时不会发生任何事情,请参阅此处的窗口图像。
没有出现任何错误,但未插入"Hello, World!"
消息,也未在控制台中打印"It's working"
。
您的插件不会被view.run_command('Node js.nodejs')
命令调用。
要运行插件,您需要调用try
命令,例如view.run_command("try")
。以下是原因的解释:
Sublime Text插件的命令名源自其类名。例如下面的类。。。
class PrintHelloInConsoleCommand(sublime_plugin.TextCommand):
def run(self, edit):
print("Hello")
可以通过调用CCD_ 10命令来运行。例如
// 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
- 从类名中删除命令:
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)
⟶""
未创建命令--不要使用大写单词,例如"TestOK"
中的"OK"
。请注意,这不会创建"test_o_k"
命令class MoveSelection(sublime_plugin.TextCommand)
⟶"move_selection"
命令——尽管类名中省略了"Command"
,但它仍然有效。在撰写本文时,ST并未严格执行该要求(但在未来版本中可能会发生变化(class AutoSaverEvents(sublime_plugin.EventListener)
⟶""
未创建命令--不会调用事件侦听器,因此未创建命令,ST也不希望类名以"Command"
结尾
有关插件的更多信息,请参阅Sublime Text非官方文档的插件部分,该部分的信息比官方文档多得多。