Sublime Forum

A problem with setViewPosition API?

#1

I’m trying to write a command to rearrange tabs with ctrl+alt+pageup/pagedown. Now I think that there is a small off-by-one bug in the new setViewPosition call. You can test this by using the console, but I also created the following command to help. With this you can move a view by typing ctrl+m followed by the new index.

[code]

class MoveTab(sublimeplugin.TextCommand):
def run(self, view, args):
win = view.window()
group, tab = win.getViewPosition(view)
win.setViewPosition(view, group, int(args[0]))
print “MoveTab from %d to %s: end position is %d” % (tab, args[0], win.getViewPosition(view)[1])
[/code]
I opened a new empty editor window, created four new tabs and named them 1, 2, 3 and 4. Then I selected tab 4 and typed ctrl+m,2,ctrl+m,1,ctrl+m,0. Now all is good – the tab is moved as expected (one step left each time) and console shows:

MoveTab from 3 to 2: end position is 2 MoveTab from 2 to 1: end position is 1 MoveTab from 1 to 0: end position is 0
Here comes the problem: continue moving the tab with ctrl+m,1,ctrl+m,2,ctrl+m,3. With the first command, tab does not move and after that the tab is always one position ‘behind’. You can also see this from the console:

MoveTab from 0 to 1: end position is 0 MoveTab from 0 to 2: end position is 1 MoveTab from 1 to 3: end position is 2

0 Likes

#2

Here is a working script to move a tab with ctrl+alt+pagedown/pageup. I had to add 2 to the index to move the tab right, but it looks strange…

[code]

class MoveTab(sublimeplugin.TextCommand):
def run(self, view, args):
groupIndex, tabIndex = view.window().getViewPosition(view)
view.window().setViewPosition(view, groupIndex, tabIndex + int(args[0]))[/code]

0 Likes

#3

Very nice, thank you.
I missed this command just a few hours ago.

0 Likes