问题
I am trying to write an ST3 Plugin for my node js server. In order to run it I am calling the command view.run_command('Node js.nodejs')
.
My Sublime Text Packages
folder looks like this:
│ 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
The ../Packages/Node js/Nodejs.py
file contains the following code:
import sublime, sublime_plugin
class TryCommand(sublime_plugin.TextCommand):
def run(self, edit):
print("It's working")
self.view.insert(edit, 0, "Hello, World!")
Nothing at all happens when view.run_command('Node js.nodejs')
is called, see the image of the window here.
No errors are raised but the "Hello, World!"
message is not inserted and "It's working"
is not printed in the console.
回答1:
Your plugin does not get called by the view.run_command('Node js.nodejs')
command. In order to run your plugin you should be calling the try
command, e.g. view.run_command("try")
. Here is the explanation of why:
The command names of Sublime Text plugins are derived from their class names. For example the following class...
class PrintHelloInConsoleCommand(sublime_plugin.TextCommand):
def run(self, edit):
print("Hello")
...can be run by calling the print_hello_in_console
command. e.g.
// 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"},
To get the command name from the class name first remove the Command
postfix from the class name. Second convert what remains of the class name from CamelCase
into snake_case
. So a plugin which defines class PrintHelloInConsoleCommand
is called by the print_hello_in_console
command.
- The class name is:
PrintHelloInConsoleCommand
- Remove Command from the class name:
PrintHelloInConsoleCommand --> PrintHelloInConsole
- Convert CamelCase to snake_case:
PrintHelloInConsole --> print_hello_in_console
- The name of the command to call is:
print_hello_in_console
Your class, class TryCommand(sublime_plugin.TextCommand)
can be run by calling the try
command, i.e. view.run_command("try")
.
Here are some other examples:
class ClearConsoleCommand(sublime_plugin.WindowCommand)
⟶"clear_console"
commandclass InsertDateTimeCommand(sublime_plugin.TextCommand)
⟶"insert_date_time"
commandclass TestOKCommand(sublime_plugin.TextCommand)
⟶""
No command created — do not use a word in uppercase, e.g. the"OK"
in"TestOK"
. Note that this does not create a"test_o_k"
commandclass MoveSelection(sublime_plugin.TextCommand)
⟶"move_selection"
command — this works despite the omission of"Command"
in the class name. At the time of writing that requirement is not strictly enforced by ST (but that may change in future versions)class AutoSaverEvents(sublime_plugin.EventListener)
⟶""
No command created — event listeners do not get called so no command is created and ST does not expect the class name to end in"Command"
For further information about plugins, see the plugins section of the Sublime Text Unofficial Documentation which has a lot more information than the official documentation has.
来源:https://stackoverflow.com/questions/63977992/how-to-set-or-find-the-command-name-of-a-sublime-text-plugin