How can I sort by line size in Sublime Text?

Asked

Viewed 184 times

4

I wonder if there is any way to sort the line by its size in the Sublime Text.

'Cause I got a code like that:

use ArrayAccess;
use ArrayIterator;
use IteratorAggregate;
use Countable;
use Closure;
use JsonSerializable;
use Laravel\Database\Eloquent\Model;

and would like to leave it like this in "one click" (or shortcut)

use Closure;
use Countable;
use ArrayAccess;
use ArrayIterator;
use JsonSerializable;
use IteratorAggregate;
use Laravel\Database\Eloquent\Model;

2 answers

3


I don’t know if it’s a good idea to do this, it may seem cute but the ideal is to sort by use relevance. But if you really want I found a code in their forum:

import sublime, sublime_plugin
import sort

def line_length_sort(txt):
    txt.sort(lambda a, b: cmp(len(a), len(b)))
    return txt

class SortLinesLengthCommand(sublime_plugin.TextCommand):
    def run(self, edit, reverse=False,
                        remove_duplicates=False):
        view = self.view

        sort.permute_lines(line_length_sort, view, edit)

        if reverse:
            sort.permute_lines(sort.reverse_list, view, edit)

        if remove_duplicates:
            sort.permute_lines(sort.uniquealise_list, view, edit)

I put in the Github for future reference.

Also has plugin ready.

  • Bigwon, I want this to be the coding standard of Laravel 4. You can see in this class code [Illuminate Support Collection])(https://github.com/illuminate/support/blob/master/Collection.php)

  • When you say that "it’s not a good idea... to sort by use relevance," you’re talking specifically about namespaces, right?

  • Yes, of course. In this example not even messed up but there is case that could have gotten worse.

2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.