bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.6k stars 94 forks source link

Auto import missing packages on editor.formatOnSave? #2689

Open vuon9 opened 11 months ago

vuon9 commented 11 months ago

I'm not sure if this is a feature or a thing that I don't know, but how can I have auto import and additionally remove unused imports with "editor.formatOnSave"?

When using Github Copilot, it would be helpful if auto-import could assist me after I choose a suggestion and save it.

netbrothers-tr commented 11 months ago

Have you seen this feature somewhere? It would be very helpful, but IMHO very difficult to solve. It is not uncommon for imports to be ambiguous, which makes it difficult to automate this. Either the AI can do it itself (apparently it can't) or the dev must decide which import is the correct one. Just an example: It doesn't help if auto-import imports App\Http\Controllers\Controller but Illuminate\Routing\Controller would be the right choice. A lot of introspection would be necessary, to determine the correct import.

vuon9 commented 11 months ago

I think maybe the problem just only about format on save. The AI generation just about the thing in my context.

I don't have an example for another PHP LSP. But Go with gopls is working with auto imports when saving Go file https://github.com/golang/tools/tree/master/gopls. It works really well on VSCode.

I think for the case that would have over 1 suggestion like Controller you said, I'm fine with one of below options:

  1. Continue to check if the class would be used for importing also contain the method. For example:

    $httpClient = new HttpClient(); // it could be A\HttpClient, B\HttpClient
    $httpClient->get(); // it could be a HttpClient contains the method.

    This is how it is showing today

    image
  2. Just skip auto-import for that missing package if there is a potential confusion.

I agree that would be a lot of scenarios that need to think about.

razvanphp commented 7 months ago

Marginally related, it would be nice to be able to import/use all missing classes from the current file, instead of clicking on Quick Fix for each error.

Here is how it looks on DART language:

0_iRZIzPrwKl7jEYNY 0_p2Pf06L6A4JSX7IS