def on_new(self, view) has no window
I'm trying to create a plug-in which intercepts the new file command and opens the new tab to the right of all existing tabs. My code looks something like:
import sublime, sublime_plugin
class EventDump(sublime_plugin.EventListener):
def on_new(self, view):
print ("new file")
print (view.window().__class__)
w = view.window()
w.set_view_index(view, w.active_group(), len(w.views_in_group (w.active_group())) - 1)
The problem appears to be that the view doesn't have a window associated with it at the point at which the event fires, making it (as far as I can tell) impossible to move the new file's tab to the rightmost of the rest. Perhaps this event should fire after the view has a window or another event created, like on_after_new_complete.
Служба підтримки клієнтів працює на UserEcho
view.window() is unsafe in a few situations. As a workaround you can use sublime.active_window() in most cases.
Well, that does not solve the fact that view.window() *is* unsafe.
Thank-you FichteFoll, however it would appear that doesn't solve my problem as the new tab still appears to the immediate right of the currently focused tab. I guess this is due to the tab (view) not having being attached to a window at the point at which the event fires.
Well, for the time being you can use this workaround with sublime.set_timeout:
import sublime
import sublime_plugin
class EventDump(sublime_plugin.EventListener):
def on_new(self, view):
self.view = view
sublime.set_timeout(self.on_post_new, 200)
def on_post_new(self):
view = self.view
w = view.window() or sublime.active_window()
print view.window().__class__, w.__class__
grp = w.active_group()
print w.get_view_index(view)
print grp.__class__, w.views_in_group(grp)
w.set_view_index(view, grp, len(w.views_in_group(grp)) - 1)
Brilliant, thank-you for that!
You can also try to define on_load as well and use that to assign the group index after defining a value or something in on_new so this does not trigger more often than it should.
Full instructions on how to set up an 'Open Tab Rightmost' plug-in within Sublime can now be found here:
http://www.sublimetext.com/forum/viewtopic.php?f=5&t=11821