Home Download Buy Blog Forum Support

Sorting Tabs

Sorting Tabs

Postby agibsonsw on Thu Mar 08, 2012 7:11 pm

Hello. I thought I would re-post the following TextCommand here. Assign it a key-binding and it will sort all open tabs alphabetically (case-insensitive). It sorts within groups as well.

Code: Select all
import sublime, sublime_plugin, os
from operator import itemgetter

class SortTabsCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      file_views = []
      win = self.view.window()
      curr_view = win.active_view()
      for vw in win.views():
         _, tail = os.path.split(vw.file_name())
         group, _ = win.get_view_index(vw)
         file_views.append((tail.lower(), vw, group))
      file_views.sort(key = itemgetter(2, 0))
      for index, (_, vw, group) in enumerate(file_views):
         if not index or group > prev_group:
            moving_index = 0
            prev_group = group
         else:
            moving_index += 1
         win.set_view_index(vw, group, moving_index)
      win.focus_view(curr_view)

It's available as a Gist as well https://gist.github.com/1995854

Someone may have already created this in the past - I haven't checked :?: . Andy.
Last edited by agibsonsw on Fri Mar 09, 2012 6:18 pm, edited 1 time in total.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sorting Tabs

Postby facelessuser on Thu Mar 08, 2012 7:20 pm

Thanks for this. I am currently using it.
facelessuser
 
Posts: 1460
Joined: Tue Apr 05, 2011 7:38 pm

Re: Sorting Tabs

Postby quodlibet on Fri Mar 09, 2012 1:18 pm

I just tested this on Win XP, ST build 2183, and get the following error:

Code: Select all
Traceback (most recent call last):
  File ".\sublime_plugin.py", line 350, in run_
  File ".\SortTabs.py", line 10, in run
    head, tail = os.path.split(vw.file_name())
  File ".\ntpath.py", line 170, in split
  File ".\ntpath.py", line 125, in splitdrive
TypeError: 'NoneType' object is unsubscriptable


Any ideas?
quodlibet
 
Posts: 460
Joined: Sat Jun 18, 2011 5:53 pm

Re: Sorting Tabs

Postby sublimator on Fri Mar 09, 2012 1:51 pm

An untitled View has `None` for its file_name()
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 648
Joined: Thu Mar 20, 2008 5:41 am

Re: Sorting Tabs

Postby agibsonsw on Fri Mar 09, 2012 1:55 pm

quodlibet wrote:I just tested this on Win XP, ST build 2183, and get the following error:

Code: Select all
Traceback (most recent call last):
  File ".\sublime_plugin.py", line 350, in run_
  File ".\SortTabs.py", line 10, in run
    head, tail = os.path.split(vw.file_name())
  File ".\ntpath.py", line 170, in split
  File ".\ntpath.py", line 125, in splitdrive
TypeError: 'NoneType' object is unsubscriptable


Any ideas?


My first guess would be that you have an unsaved file, in which case their is no file_name() to split. If that's the case I'll need to re-visit the code to handle this scenario.

I also haven't tested it with a Cloned file. I'll incorporate these concerns when I revisit the code - perhaps tonight. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sorting Tabs

Postby quodlibet on Fri Mar 09, 2012 2:07 pm

castles_made_of_sand wrote:An untitled View has `None` for its file_name()


Yup. After saving all my untitled files, the plugin works.

It would be nice if untitled files could be accounted for. I often find myself using ST as a scratchpad :)

(Maybe using the tab's label instead of the filename?)
quodlibet
 
Posts: 460
Joined: Sat Jun 18, 2011 5:53 pm

Re: Sorting Tabs

Postby agibsonsw on Fri Mar 09, 2012 3:44 pm

quodlibet wrote:
castles_made_of_sand wrote:An untitled View has `None` for its file_name()


Yup. After saving all my untitled files, the plugin works.

It would be nice if untitled files could be accounted for. I often find myself using ST as a scratchpad :)

(Maybe using the tab's label instead of the filename?)


Hi. Yes, I'll "fix" this later :)
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sorting Tabs

Postby agibsonsw on Fri Mar 09, 2012 5:52 pm

This should "do it" for unsaved files and/or cloned files.

Code: Select all
import sublime, sublime_plugin, os
from operator import itemgetter

class SortTabsCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      file_views = []
      win = self.view.window()
      curr_view = win.active_view()
      for vw in win.views():
         _, tail = os.path.split(vw.file_name() or os.path.sep)
         group, _ = win.get_view_index(vw)
         file_views.append((tail.lower(), vw, group))
      file_views.sort(key = itemgetter(2, 0))
      for index, (_, vw, group) in enumerate(file_views):
         if not index or group > prev_group:
            moving_index = 0
            prev_group = group
         else:
            moving_index += 1
         win.set_view_index(vw, group, moving_index)
      win.focus_view(curr_view)

but let me know.. Andy. I'll update my Gist in a wee while.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sorting Tabs

Postby agibsonsw on Fri Mar 09, 2012 6:46 pm

As indicated in another thread, this should really be a WindowCommand rather than a TextCommand. It required a small change to the code, but you can just replace the code with the following - your key-binding will work the same.

Code: Select all
import sublime, sublime_plugin
from os import path
from operator import itemgetter

class SortTabsCommand(sublime_plugin.WindowCommand):
   def run(self):
      file_views = []
      win = self.window
      curr_view = win.active_view()
      for vw in win.views():
         _, tail = path.split(vw.file_name() or path.sep)
         group, _ = win.get_view_index(vw)
         file_views.append((tail.lower(), vw, group))
      file_views.sort(key = itemgetter(2, 0))
      for index, (_, vw, group) in enumerate(file_views):
         if index == 0 or group > prev_group:
            moving_index = 0
            prev_group = group
         else:
            moving_index += 1
         win.set_view_index(vw, group, moving_index)
      win.focus_view(curr_view)

Basically, sorting tabs applies to the whole Window, rather than a particular view. https://gist.github.com/2007980

I'm also only importing os.path, rather than the whole library, as only 'path' is needed. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Sorting Tabs

Postby facelessuser on Fri Mar 09, 2012 7:17 pm

You don't need to import sublime since you aren't using anything from that namespace.
Code: Select all
import sublime, sublime_plugin


Also, prev_group is not intialized before you use it.
Code: Select all
if index == 0 or group > prev_group:


Edit
I guess technically index will always equal 0 first so you won't try and compare prev_group until after it gets set where index = 0...but it still kind of bugs me, oh well, it is technically sound unless index doesn't equal 0 first.
facelessuser
 
Posts: 1460
Joined: Tue Apr 05, 2011 7:38 pm

Next

Return to Plugin Development

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests

cron