vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Sort and clean imports

Open ghostoy opened this issue 7 years ago • 13 comments

Usually after code refactor, some imports are not necessary and should be removed and re-sorted. Hope Intelephense can support this feature.

BTW, this plugin is the best PHP plugin on VSCode. Fast and stable.

ghostoy avatar Jul 25 '18 03:07 ghostoy

I use phpfmt for that demands. It can sort use statements by the length of line, clear unused imports and transform long array() into short [].

banqhsia avatar Aug 01 '18 02:08 banqhsia

i use PHP Namespace Resolver for auto import and auto sort , also it cleans unnecessary imports

and thank @bmewburn your nice plugin 😄

oommgg avatar Feb 28 '19 09:02 oommgg

+1 for having this added to Intelephense

chrislentz avatar Apr 09 '20 20:04 chrislentz

+1

andrefelipe avatar Apr 17 '20 14:04 andrefelipe

+1 for sorting/cleaning use statements, and a short array conversion option would be so handy

richmilns avatar Oct 27 '20 16:10 richmilns

Would be really useful to do this "on-save"

HomelessCoder avatar Nov 10 '20 20:11 HomelessCoder

+1, PHPStorm has it, hope vscode-intelephense also offer this functionality

slyang-git avatar Sep 15 '21 07:09 slyang-git

Just a question here, when you talk about sorting. Are there multiple different approaches to sorting imports? I saw "length of line" above, other methods could be "alphabetically".

For e.g. Java, I saw the sorting puts builtin imports first, then a newline, then user created classes etc.

What is the common approach for PHP?

PythooonUser avatar Oct 07 '21 06:10 PythooonUser

I think alphabetical makes most sense to me.

richmilns avatar Oct 07 '21 07:10 richmilns

+1 to alphabetical order.

I think there should be at least three options in the settings:

  • sort strategy
  • sort on save
  • clean on save

And on top of that, it worth to have the corresponding command in the command palette, so we can bind it to some keyboard shortcut.

Thanks in advance!

HomelessCoder avatar Oct 07 '21 07:10 HomelessCoder

Have you tried running CS PHP Fixer on save with config 'ordered_imports' => ['sort_algorithm' => 'alpha'],? I think that could be a simple solution as well.

PythooonUser avatar Oct 07 '21 07:10 PythooonUser

Yes, I know we can achieve it with phpcs, of course. But for some reasons it is not always convenient... For some projects it is quite enough to simply use intelephense and this is really great, but in this case it lacks the kind of features this topic suggests.

HomelessCoder avatar Oct 07 '21 08:10 HomelessCoder

I think there should be at least three options in the settings:

  • sort strategy
  • sort on save
  • clean on save

And on top of that, it is worth having the corresponding command in the command palette, so we can bind it to some keyboard shortcut.

Thanks in advance!

Note: This is just food for thought. Please, DO NOT try to implement such behaviour, if it will delay the arrival of the Clean&Sort update.

Another one, which I definitely won't use, but probably some would like to, is an option to group imports.

Ie. converting

use App\Services\BbisWrapper\BbisWrapperClient;
use App\Services\BbisWrapper\CustomerAddress;
use App\Services\Dokumenti\DocumentTree;
use App\Services\Pim\PimCachingClient;
use App\Services\BbisWrapper\Customer;
use App\Events\OrderDocumentReceived;
use App\Enums\FinancialDocumentType;
use App\Jobs\StartShopOrderProcess;
use App\Models\FinancialDocument;
use App\Enums\Bbis\DeliveryType;
use App\Events\ShipmentUpdated;
use App\Services\Nop\NopClient;
use Illuminate\Support\Carbon;
use App\Services\Pim\Product;
use App\Models\OrderShipment;
use App\Dtos\ReceiptDocument;
use App\Enums\ShippingMethod;
use App\Enums\PaymentMethod;
use App\Enums\OrderLineType;
use Psr\Log\LoggerInterface;
use App\Dtos\OrderDocument;
use App\Models\OrderAction;
use App\Enums\OrderSource;
use App\Models\OrderLine;
use App\Dtos\Document;
use App\Models\Order;
use App\Dtos\Line;

to

use App\Services\Dokumenti\DocumentTree;
use App\Jobs\StartShopOrderProcess;
use App\Enums\Bbis\DeliveryType;
use App\Services\Nop\NopClient;
use Illuminate\Support\Carbon;
use App\Services\BbisWrapper\{
    BbisWrapperClient,
    CustomerAddress,
    Customer,
};
use Psr\Log\LoggerInterface;
use App\Services\Pim\{
    PimCachingClient,
    Product,
};
use App\Events\{
    OrderDocumentReceived,
    ShipmentUpdated,
};
use App\Models\{
    FinancialDocument,
    OrderShipment,
    OrderAction,
    OrderLine,
    Order,
};
use App\Enums\{
    FinancialDocumentType,
    ShippingMethod,
    PaymentMethod,
    OrderLineType,
    OrderSource,
};
use App\Dtos\{
    ReceiptDocument,
    OrderDocument,
    Document,
    Line,
};

mathmul avatar May 10 '22 08:05 mathmul

+1

gabrieleolmi avatar Apr 23 '23 23:04 gabrieleolmi

+1

+1

3elDU avatar Oct 17 '23 07:10 3elDU

That would be so cool to have. Support as well.

Tamas-hi avatar Feb 23 '24 14:02 Tamas-hi