Sublime Forum

Restrict EventListener callback to specific file type

#1

I created a script to perform an action on Cucumber Feature files (*.feature) in the on_pre_save callback, and I’m currently just checking the extension:

	def on_pre_save(self, view):
		file_name = view.file_name()
		if not file_name: # buffer has never been saved
			return

		base_name, ext = os.path.splitext(file_name)

		if ext != '.feature': # not a cucumber feature file
			return

Is there a better way to restrict the callback to a particular type of file?

Thanks.

0 Likes

#2

Test the scope at one point in the beginning of your method:

if view.score_selector(0, 'source.js') == 0: return
Replace ‘source.js’ by your scope name.

Look at the doc: http://www.sublimetext.com/docs/2/api_reference.html

0 Likes

#3

Thanks, bizoo. That works well.

Heh. I had read the documentation, but wasn’t able to grok it I guess. At any rate, between being new to ST2 and Python, I’m slowly getting my feet underneath me. The callback is working as I want it to now.

Peace.

0 Likes