Hi,

There are times when code (without namespaces) is copied from another source. So this won't work until the namespaces are also selectively copied over or more likely, manually inserted. Think of code from stackoverflow etc.

Can I suggest to have an option like PHP Namespace Resolver to manually scan and import the selected class?

Here is how it works:

  1. Move the cursor to the class that needs to import its namespace
  2. Press ctrl + alt + i which triggers namespace resolving
  3. If there is only 1 match, it is inserted immediately, otherwise show a selection of the matching namespaces for the user to select

Thank you.

    Hello,

    Do you mean when using such a class in your code?

    • editor automatically adds use when completing (auto-import)
    • you can resolve namespace using quick actions (ctrl+.)

    Details are listed on https://docs.devsense.com/en/vscode/editor/auto-import

    Or when you copy a class, and you need to prepend a namespace declaration such as:

    namespace MyNamespace;

    ?

      JakubMisek

      Thanks! I tried ctrl + ., I did not know as it wasn't documented.

      Perhaps add in the word "Quick Fix" at https://docs.devsense.com/en/vscode/editor/auto-import and it's default shortcut?

      My issue was the first case:

      1. Let's say I copied some code that uses Artisan facade
      2. But use Illuminate\Support\Facades\Artisan; has not been added yet
      3. If I trigger PHP Namespace Resolver, it will automatically add in use Illuminate\Support\Facades\Artisan; without any dialog/selection as there is only 1 Artisan.php in the whole project
      4. If there are more than 1 Artisan.php, only then will a selection dialog pops up

      Small thing, but makes quite a different when there are several classes to add. Thank you.

        I understand; so you'd like to import all the namespaces that need to be imported within a file? Otherwise, we have the import as a quickfix providing autoimport with all the possible namespaces within your workspace and syntaxes:

          Yes, spot on: add all namespaces that needs to be imported. :)

            alright, I get it. Something like "apply all recommended fixes", or "import all namespaces"

              7 months later

              Hi,

              I realized what isn't so good about ctrl+.

              Even though there is only 1 class with the name, I have to activate the quick actions, read the options, and select the one that I want.

              In contrast, PHP Namespace Resolver just adds it without asking. It only asks when there are multiple classes with the same name.

              Thank you.

              Yea, but the automatic adding doesn't always work, especially when there are typing mistakes or editing of the names etc.

                8 months later

                JakubMisek

                is it possible to expose a command that I can call to resolve the namespace of the class that the cursor is on?
                Today, I'm using the shortcut key ctrl + alt + i to "force" it.

                The issue is that there are frequently classes of the same name, I need to select the correct one.
                This plugin does it quite well https://open-vsx.org/extension/MehediDracula/php-namespace-resolver

                I suspect this feature requires a language server, so I prefer to just rely on yours. Thank you.

                  JakubMisek

                  I did and tried for quite a whlie already. For instance in my project, Router class has 3 possibilities:

                  use Illuminate\Routing\Router;
                  use Google\Service\Compute\Router;
                  use Symfony\Component\Routing\Router;

                  But Quick Fix... showed No code actions available

                  See the difference in the 2 videos: https://imgur.com/a/Ggh0yqs

                    phptools I see, there is no Quick Fix, because there is nothing to fix. The Router is a perfectly valid name in this context.

                    So you'd like to use a different Router. We don't have a "quick fix" like that, but I'll note it in our to-do to add some refactorings for this. It's a good idea!

                    JakubMisek

                    Thanks! If you could, perhaps make selection the same style like finding files/workspaces instead of a pop up :)

                      11 days later

                      JakubMisek I'm "patiently" waiting for this feature. Thank you! :)

                        a month later
                        24 days later

                        phptools implemented 😄

                        will be available in the next pre-release.

                          thanks can share which version so I can test? Thank you.