Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

sublimetext3 - How to set or find the command name of a Sublime Text plugin

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"},

// 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")

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" command
  • class InsertDateTimeCommand(sublime_plugin.TextCommand)
    ? "insert_date_time" command
  • class 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" command
  • class 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...