Sublime Forum

Init method of sublime_plugin.ViewEventListener


What is the signature for the init method of sublime_plugin.ViewEventListener ? It doesn’t seem to be documented anywhere.



I figured it out. It seems to be the view object.



The view is passed as a single parameter to the constructor. The default implementation makes the view available via self.view.



What advantages does ViewEventListener have over EventListener ?

1 Like


The ViewEventListener class has a class level instance method that Sublime invokes on a view to ask it if it applies to that view. This method is given a view and a settings object to see if it applies to that view, and it gets re-invoked every time the settings change.

So one benefit is that if you have an event handler that is very specific to a certain type of file or only in very specific cases (as determined by settings), your code tends to be somewhat cleaner in that it can assume that if it sees an event, it should take an action. It also makes it easier to to flip the state as needed when the settings change.

In addition, since there is one ViewEventListener subclass instance created for every applicable view, if you need to persist some sort of state between events it’s easier to do with a ViewEventListener (for which you could just use an instance variable) as opposed to an EventListener, which would have to maintain some kind of data structure on it’s own.

I’m sure there are other benefits as well, but these are the big ones that come immediately to mind.



Hmmm you mentioned getting the view in the class method. I only see the settings object. Is there a way to get access to the view? Or to determine what file, syntax etc is bound to the view in that class method?



I am hoping to use a ViewEventListener on an input panel. (I would like context sensitive completions for tags in my org mode extension)

I have a syntax that has a bogus file extension.
I was hoping I could look at settings.get(“extensions”,[])
but that seems to return the file extension of the parent view not the input panel view despite the fact I have explicitly set the syntax on the view right after I made it, I expect this is because this class method is being called as the panel view is being created.

I can do this with a global view, it just becomes much more of a pain to gain access to state information here as the user types.



Ahh sorry, I mis-spoke above. The signature for the method in question is:

class TestListener(sublime_plugin.ViewEventListener):
    def is_applicable(cls, settings):

Every time settings change on a view, all of the is_applicable() class methods are invoked with the new settings to see if that view applies; anything that didn’t previously but now does causes an instance of the listener to be created for it, while if it used to apply and now does not the instance is dropped.

When the actual instance is created, the view is provided there, and the resulting instance of the class can use self.view to know what view it’s associated with as it’s handling events.

So basically in order to test applicability you only get to see the settings for the view and as such you can’t know things like the file extension, etc from inside of the listener unless you have a way to apply them to the settings of a view from outside (e.g. if you had an on_load and on_save event in a non-view listener that applied the information to the settings).

That said, from what I can see, the plugin host doesn’t trigger is_applicable() for views that aren’t related to files (such as the view used in the input_panel()) so regardless there’s not a way to do this using just a ViewEventListener as far as I can see. Even trying to defer the settings change using set_timeout() in case the issue might be related to timing doesn’t work.