Home Download Buy Blog Forum Support

open_file in on_activated crashing ST2

open_file in on_activated crashing ST2

Postby phillip.koebbe on Fri Feb 24, 2012 12:02 pm

I've recently "discovered" using a two-column layout when doing TDD: I put the tests in one group and the associated files in the other. This lets me see both my tests and the code under test at the same time. I like it a lot. So, being the tinkerer that I am, I thought it would be nice to write a little plugin that activated the associated file in the other group when I switched to its counterpart. I've been working with CoffeeScript lately, so if I switch to my_model.js.coffee in group 0, i'd like my_model_test.js.coffee to be activated in group 1. [Over simplifying here ... obviously have the issue of looking to see if the file exists and all that jazz] So to just get my bearings on moving views around group to group, I hard coded some file names and started tinkering.

Given a path to an existing file, if I did window.open_file(path) in the on_activated event, ST2 would crash. Every time. If I open the console and enter the same command, the file would open just fine. Then I tried to move an already open file to a different group using window.set_view_index, which also resulted in a crash. I used a method something like:

Code: Select all
for v in window.views():
   if v.file_name() == <hard coded name of file>:
      the_view = v
      break

window.set_view_index(the_view, 1, 0)


Should these sorts of operations be possible in on_activated? If not, how would one go about making sure an associated file is open in another group when a particular file is activated? If possible, I'd like it to happen automatically and not depend on a key combination. I can bind it to a key combination if necessary, I'd just like to not have to think about it.
-- 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: open_file in on_activated crashing ST2

Postby phillip.koebbe on Fri Feb 24, 2012 11:23 pm

I was working on this again today and I got everything pretty much working until I got to the part of opening the "other" file. Same as before, crash. Here is the Crash Report provided by Apple (in case it helps):

https://gist.github.com/1904456

The code that is crashing is essentially:

Code: Select all
if os.path.exists(new_file_name):
   new_view = self.view.window().open_file(new_file_name)
   the_group, the_index = self.view.window().get_view_index(new_view)
   # group will be either 0 or 1 based on some condition
   if the_group is not group:
      self.view.window().set_view_index(new_view, group, 0)


I did simplify it just a little. There is actually another method involved, but all it does is the get_view_index/set_view_index stuff. I do have two groups active at the time this is running.
-- 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: open_file in on_activated crashing ST2

Postby jps on Sat Feb 25, 2012 10:32 pm

Use sublime.set_timeout(callback, 0) to work around this, but be aware that you can easily get stuck in an infinite loop, as opening a file will trigger a call to on_activated
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: open_file in on_activated crashing ST2

Postby phillip.koebbe on Mon Feb 27, 2012 12:59 am

Thanks for the input, Jon. I decided against doing this in on_activated for this and another reason. I realized that there would be times that I would want to switch to a file without activating its counterpart, so I moved it to a window command.
-- 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 Plugin Development

Who is online

Users browsing this forum: No registered users and 3 guests