Leonardo
Leonardo

Reputation: 4228

Run a sync sequence of commands in sublime plugin

I've been trying to write a very simple plugin for Sublime Text (3) but being new to the topic either I am missing something or something is not working as expected.

I want to create a command that move the current tab to a specified group (for example group 0 for simplification). Right after that I want to focus on that group:

import sublime
import sublime_plugin

class MoveAndFocusToGroupCommand(sublime_plugin.WindowCommand):

    def run(self):
        self.window.run_command("move_to_group",{"group": 0})
        self.window.run_command("focus_group", {"group": 0})

The previous snippet does not work. The first command will be executed but not the second one.
If I comment the first command, then the second one is executed.

Maybe the commands are executed on different threads or something async is going on.

I've also tried to run the second command in an event handler:

class MoveAndFocusToGroupCommand(sublime_plugin.WindowCommand):

    def run(self):
        self.window.run_command("move_to_group",{"group": 0})

class MovedToGroupEventListener(sublime_plugin.EventListener):
    def on_post_window_command(self, window, name, args):
        if name == 'move_to_group' and args is not None:
            window.run_command("focus_group", args)

but nothing changes, the focus will never set on group 0.

What am I missing here?

Upvotes: 2

Views: 280

Answers (1)

Enteleform
Enteleform

Reputation: 3833

Your initial code worked fine for me, until I tried it on the last remaining view in the second group.

As r-stein mentioned in the comments, it seems that the blank view created by SublimeText in the empty group is interfering with the timing of your command sequence.

The following code uses set_timeout_async to re-sequence the order of commands. I omitted the optional delay parameter, as it seems to work fine without it.


import sublime
import sublime_plugin

class MoveAndFocusToGroupCommand( sublime_plugin.WindowCommand ):

    def run( self ):
        self.window.run_command( "move_to_group", { "group": 0 } )
        sublime.set_timeout_async( lambda: self.window.focus_group( 0 ) )

Upvotes: 3

Related Questions