Sublime Forum

TextCommand not seen by EventListener when called from the Command Palette

#1

I just noticed that an EventListener listening for TextCommands does not pick up a TextCommand when it gets called from the Command Palette.

Consider this code:

import sublime
import sublime_plugin

class TempTestCodeCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        file_path = self.view.file_name()
        if file_path:
            sublime.status_message(file_path)
        else:
            sublime.status_message("no file path available")

class TempTestCodeEventListener(sublime_plugin.EventListener):

    def on_text_command(self, view, command_name, args):
        print("on_text_command:  {} - {}".format(command_name, args))

    def on_post_text_command(self, view, command_name, args):
        print("on_post_text_command: {} - {}".format(command_name, args))

# { "keys": ["ctrl+k", "ctrl+z"], "command": "temp_test_code" },
# { "caption": "Temp Test Code", "command": "temp_test_code" },

When called by the key binding the file path is shown as a status message and the EventListener's methods print as expected to the console. But when called by the Command Palette the EventListener does nothing although the file path status message is shown successfully.

I tried a workaround like this:

class TempTestCodeCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        active_view = sublime.active_window().active_view()
        active_view.run_command("test_work_around")

class TestWorkAroundCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        file_path = self.view.file_name()
        if file_path:
            sublime.status_message(file_path)
        else:
            sublime.status_message("no file path in this buffer")

Unfortunately it made no difference at all - the exact same thing happened.

Is this problem a bug? Is there an effective workaround?

Thanks.

0 Likes

#2

there is a similar known regression for WindowCommands:

1 Like