What is the difference between sublime_plugin.TextCommand and sublime_plugin.WindowComand?

0

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

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 what the difference is between TextCommand and WindowCommand . What do each one look like?

    
asked by anonymous 04.04.2017 / 02:09

1 answer

1

WindowCommands do not require a view to run, although an associated view may exist.

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 mandatory because it is a TextCommand.

In the example below of the WindowCommand a view is not used but only access to the sublime window to create a new document.

Ref .: Extract from the plugin side_bar ( link )

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 does not mean that you   can not manipulate views from window commands, but rather that you do not   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   would not make sense.

     

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

     

The .run () method of a command window does not 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:

link

link

    
04.04.2017 / 11:03