Sublime Forum

Setting view index after creation

#1

Hi,

I’ve already asked some questions about my “moving tabs to the right” plugin (and have received some wonderful answers, thanks for that.)

I was wondering if there is a way to set the view_index of a newly created view. I’m using on_new(self,view) (and have tried on_activated) to get a reference to the newly created view, but its view index at that point in time is always set to -1. Changing it with window.set_view_index(view, group, rightIndex) does nothing. It appears the view’s index is set by SublimeText after these events fire. There may be a good reason for this, but in my case it makes things difficult.

Is there a way to change the index of a newly created view? If there isn’t, is there a polite way to ask Jon to add this to the API?

I’ll include some samplecode from my plugin so you can understand what I mean.

class TabrightEvent(sublime_plugin.EventListener):

	def move_tab_right(self,view):
		if not view.settings().get("Tabright_processed"):
			window = sublime.active_window()
  			view.settings().set("Tabright_processed", True)
			group,index = window.get_view_index(view)
			rightIndex = len(window.views_in_group(group)) - 1
			window.set_view_index(view, group, rightIndex)
			window.focus_group(group)
			window.focus_view(view)

	def on_new(self,view):
		self.move_tab_right(view)

Thanks in advance!

1 Like

#2

I discovered I could use this trick to delay setting the index until after the new view was made visible. It seems a bit hacky. Any comments or insights are still welcome.

	def on_new(self,view):
	    def callback(view=view):
			return self.move_tab_right(view)
	    sublime.set_timeout(callback, 10)
0 Likes

#3

I think you’ll find that set_timeout is indeed the generally accepted way to work around issues like these. A lot of times it works even if the timeout is 0.

0 Likes

#4

Thanks for the comment! I’m using this now.
Do you reckon this is “expected” or simply hacking around a problem with the API? I’m sure Jon has a lot on his plate as it is, but adding some “post” events would make for much more future-proof plugins, I’d say.

0 Likes

#5

This is probably just a “missing feature” out of many. Related: sublimetext.userecho.com/topic/1 … -no-window

0 Likes