vscode-intelephense
vscode-intelephense copied to clipboard
Organize Imports
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
usestatement (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\*\Eventsto 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.
Thing to consider: PSR-12 section 3 (https://www.php-fig.org/psr/psr-12/).
And the important thing is Remove unused import.
any progress?
I would love to see this feature!
Perhaps you could use Code Actions? editor.codeActionsOnSave:
"editor.codeActionsOnSave": [
"source.organizeImports",
"source.fixAll"
]
@cyraid I tried this, it doesn't work.
@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)
If someone is still looking for solution, I tried php cs fixer and it works in removing unused imports.