Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort use by length #22

Open
suth opened this issue Mar 20, 2016 · 4 comments
Open

Sort use by length #22

suth opened this issue Mar 20, 2016 · 4 comments
Labels

Comments

@suth
Copy link

suth commented Mar 20, 2016

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
Copy link
Owner

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
Copy link
Author

suth commented Feb 4, 2017

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
Copy link
Owner

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
Copy link

you can set:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants