Sublime Forum

Problem with EventListener class

#1

I’m trying to write an EventListener that applies relevant syntax to files based on several criteria (I know there’s a package that does this already, I’m doing it for educational purposes).

I have set up a dummy event listener, but I get this kind of errors, and I can’t find a way to make it work:

from sublime import *
from sublime_plugin import *
class ck2IDE_event_listener(EventListener):
	def on_new(view):
		print('test')

Every time I creat a new file, I get this error on the console:

Traceback (most recent call last):
  File "C:\Sublime Text 3\sublime_plugin.py", line 330, in on_new
    callback.on_new(v)
TypeError: on_new() takes 1 positional argument but 2 were given

The same eror happens with other even hooks such as on_loaded or on_pre_save.
Any help would be appreciated - and I hope I’m not being overy obtuse with this.

Edit; having the same problem with ViewEventListeners. Also, having this in the last dev build, if that changes anything.

1 Like

#2

something the ST official API reference doesn’t mention is that all instance methods on a class in Python take a first argument of self - you’ll need to add this to get it to work i.e.

from sublime import *
from sublime_plugin import *
class ck2IDE_event_listener(EventListener):
    def on_new(self, view):
        print('test')

it’d be much easier for copy/paste reasons and Python beginners if those docs included the self parameter

1 Like