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

Organize Imports

Open tarik02 opened this issue 4 years ago • 10 comments

Feature description or problem with existing feature There should be a function to organize imports within a file.

Describe the solution you'd like For example we have such a block of imports:

use App\Exceptions\Customers\CustomerNotApproved;
use App\Exceptions\Customers\CustomerWasBanned;
use App\Exceptions\Customers\CustomerNotRegistered;
use Auth;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\{ServiceProvider};
use libphonenumber\PhoneNumberFormat;
use App\Providers\Auth\EloquentUserByResolverProvider;
use Lord\Laroute\Generators\GeneratorInterface as LarouteGeneratorInterface;
use function array_filter;

I'd want to make convert it to this:

use App\Providers\Auth\EloquentUserByResolverProvider;
use Auth;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\ServiceProvider;
use libphonenumber\PhoneNumberFormat;
use Lord\Laroute\Generators\GeneratorInterface as LarouteGeneratorInterface;

use function array_filter;

use App\Exceptions\Customers\{
	CustomerNotApproved,
	CustomerNotRegistered,
	CustomerWasBanned
};

So, it should be organized in such way:

  • imports from the same namespace should be grouped within single use statement (only if there is 3 (configurable) or more imports from single namespace). Also, it should be discovered about nested imports e.g.:
    use App\Http\{
        Controllers\HomeController,
        Middlewares\MyMiddleware
    };
    
  • that groups go after single imports, that are going after functions (order of groups should be configurable);
  • all imports and groups are alphabetically sorted;
  • unused imports should be removed.

Also would be great to see:

  • a keybinding and command palette shortcut to organize imports;
  • automatically organize imports every time an import is added;
  • grouping exclude list (e.g. disallow to make a group starting with use App\...), maybe minimum nesting level to create a group;
  • grouping force include list (like exclude one, but opposite) - namespaces specified here will always be imported like use Path\To\Namespace {;
  • would be great to make these lists work with wildcards. Very useful for projects that are built in modular structure, e.g. we may add something like Packages\*\Events to the list.

Additional context I've seen this feature in other editors like IntelliJ IDEA (PhpStorm), Eclipse. This feature allows to maintain a readable list of imports.

tarik02 avatar Jan 27 '21 08:01 tarik02

Thing to consider: PSR-12 section 3 (https://www.php-fig.org/psr/psr-12/).

tarik02 avatar Feb 01 '21 19:02 tarik02

And the important thing is Remove unused import.

xpader avatar Jul 13 '21 08:07 xpader

any progress?

shushenghong avatar Jul 22 '21 11:07 shushenghong

I would love to see this feature!

schorschebob avatar Apr 06 '22 07:04 schorschebob

Perhaps you could use Code Actions? editor.codeActionsOnSave:

"editor.codeActionsOnSave": [
    "source.organizeImports",
    "source.fixAll"
]

cyraid avatar Jan 16 '24 11:01 cyraid

@cyraid I tried this, it doesn't work.

RishabhTayal avatar Apr 04 '24 16:04 RishabhTayal

@cyraid I tried this, it doesn't work.

Oh I know, I was suggesting to the author they could use the vscode code actions API. Sorry if I wasn't clear. (If the developer decides to add it and follow th API)

cyraid avatar Apr 04 '24 16:04 cyraid

If someone is still looking for solution, I tried php cs fixer and it works in removing unused imports.

RishabhTayal avatar Apr 04 '24 16:04 RishabhTayal