What is the difference between sublime_plugin.Textcommand and sublime_plugin.Windowcomand?

Asked

Viewed 45 times

0

When we create a plugin, Sublime Text usually creates a sample using the class sublime_plugin.TextCommand.

import sublime
import sublime_plugin


class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, "Hello, World!")

But I also saw that we have the class sublime_plugin.WindowCommand.

I’d like to know the difference between TextCommand and WindowCommand. What each one of them is for?

1 answer

1


Windowcommands do not require a view to run, although there may be an associated view.

On the contrary, Textcommands require a view to be available.

By way of example, in the example you provided:

self.view.insert(edit, 0, "Hello, World!")

The view is a mandatory component because it is a Textcommand.

In the example below Windowcommand no view is used but only access to the sublime window to create a new document.

Ref.: Extract from the side_bar plugin (http://folk.uib.no/hab001/Info100/Sublime%20Text%202.0.2/Data/Packages/Default/side_bar.py)

import sublime, sublime_plugin
import os

class NewFileAtCommand(sublime_plugin.WindowCommand):

def run(self, dirs):
    v = self.window.new_file()
    if len(dirs) == 1:
        v.settings().set('default_dir', dirs[0])

def is_visible(self, dirs):
    return len(dirs) == 1

Window Commands

Window Commands Operate at the window level. This doesn’t Mean that you can’t Manipulate views from window Commands, but rather that you don’t need views in order for window Commands to be available. For instance, the built-in command new_file is defined as a Windowcommand so it Works Even when no view is open. Requiring a view to exist in that case wouldn’t make sense.

Window command instances have a . window attribute to point to the window instance that created them.

The . run() method of a window command doesn’t require any positional Parameter.

Window Commands are Able to route text Commands to their window’s active view.

Text Commands

Text Commands Operate at the view level, so they require a view to exist in order to be available.

Text command instances have a . view attribute Pointing to the view instance that created them.

The . run() method of text Commands requires an Edit instance as its first positional argument.

References:

http://docs.sublimetext.info/en/latest/extensibility/plugins.html#window-Commands

http://docs.sublimetext.info/en/latest/extensibility/plugins.html#text-Commands

Browser other questions tagged

You are not signed in. Login or sign up in order to post.