Home Download Buy Blog Forum Support

A problem with setViewPosition API?

A problem with setViewPosition API?

Postby mry on Tue Dec 15, 2009 9:06 am

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: Select all
<binding key="ctrl+m,/(\d)/" command="moveTab $1"/>

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

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:

Code: Select all
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:

Code: Select all
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
mry
 
Posts: 10
Joined: Fri Oct 30, 2009 5:42 am

Re: A problem with setViewPosition API?

Postby mry on Tue Dec 15, 2009 9:21 am

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: Select all
<binding key="ctrl+alt+pagedown" command="moveTab 2"/>
<binding key="ctrl+alt+pageup" command="moveTab -1"/>

class MoveTab(sublimeplugin.TextCommand):
    def run(self, view, args):
        groupIndex, tabIndex = view.window().getViewPosition(view)
        view.window().setViewPosition(view, groupIndex, tabIndex + int(args[0]))
mry
 
Posts: 10
Joined: Fri Oct 30, 2009 5:42 am

Re: A problem with setViewPosition API?

Postby tgkeul on Tue Dec 15, 2009 4:57 pm

Very nice, thank you.
I missed this command just a few hours ago.
tgkeul
 
Posts: 140
Joined: Tue Dec 02, 2008 9:35 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 18 guests