arnaud-lb / vim-php-namespace

PHP namespace support for VIM. Types "use" statements for you
256 stars 29 forks source link

Sort use by length #22

Open suth opened 8 years ago

suth commented 8 years ago

The new sort function is great (and much easier than other solutions I've found that require selecting the lines manually), but an option to sort by line length would be another awesome addition.

arnaud-lb commented 7 years ago

Out of curiosity, why sorting by length ?

Personally I like to sort alphabetically because this reduces the chances of merge conflicts when many people work on the same file: If everyone added use statements at the end, this would always create conflicts because modifications always happen at the same place. You might say that sorting by length may help too.

Also, sorting alphabetically naturally groups use statements with common namespaces together.

suth commented 7 years ago

In the Laravel community it's a common convention. It's mainly aesthetic, but as you noted it still reduces merge conflicts. When you're mostly working with small classes with just a few use statements there isn't much of a benefit to grouping statements with relevant namespaces, so I don't mind going with the more aesthetic option.

I also think when visually examining use statements I typically am only looking at the actual class name, and sorting by length makes it easier to do that.

Take for example:

use App\Apples\Foo\Bar\Baz;
use App\Bar;
use App\Charlie\Delta\Foo\Bar\Baz\Product;
use App\Foo\Green;
use App\Foo\Yellow\Foo\Bar\Order;

Not only does this look ugly, but if you're just looking at the class names your eyes are constantly moving left to right to find the end of the line. Now look at this:

use App\Bar;
use App\Foo\Green;
use App\Apples\Foo\Bar\Baz;
use App\Foo\Yellow\Foo\Bar\Order;
use App\Charlie\Delta\Foo\Bar\Baz\Product;

Your eyes only have to gradually move right to find the class name.

arnaud-lb commented 7 years ago

Makes sense, thanks.

Replacing sort by !awk '{ print length, $0 }'|sort -n -s|cut -d' ' -f2- near the end of phpns.vim should allow to sort by length. I would welcome a PR to add this as an option :)

davidsierradz commented 7 years ago

you can set:

let g:php_namespace_sort = "'{,'}-1!awk '{print length, $0}' | sort -n -s | cut -d' ' -f2-"