Sublime Forum

open_file in on_activated crashing ST2

#1

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:

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.

0 Likes

#2

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):

gist.github.com/1904456

The code that is crashing is essentially:

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.

0 Likes

#3

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

0 Likes

#4

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.

0 Likes