Home Download Buy Blog Forum Support

Restrict EventListener callback to specific file type

Restrict EventListener callback to specific file type

Postby phillip.koebbe on Fri Dec 23, 2011 2:22 am

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:

Code: Select all
   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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Restrict EventListener callback to specific file type

Postby bizoo on Fri Dec 23, 2011 7:46 am

Test the scope at one point in the beginning of your method:
Code: Select all
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
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Restrict EventListener callback to specific file type

Postby phillip.koebbe on Sat Dec 24, 2011 8:50 pm

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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm


Return to Technical Support

Who is online

Users browsing this forum: No registered users and 23 guests