klesun / deep-assoc-completion

A phpstorm plugin for associative array key typing and completion
Other
267 stars 17 forks source link

Removes falsely used usages! Even while option is not active! #175

Open andimg93 opened 3 years ago

andimg93 commented 3 years ago

The plugin is causing much trouble right now, in some of my projects. I have not activated the feature "Remove unused use-s on save" even though it removes some usages while saving! image

Trouble code e.g. (heavily abridged):

<?php

use Application\Model\Mandator;
use Application\Model\Supplier;
use Monitoring\Monitor\Supplier\AbstractConnection;
use Wrapper\Service\Cycle;

return [
    'displayName'        => 'Whatever',
    'displayForced'      => true,
    'configuredWebsites' => [
        Mandator::WHATEVER => [
            AbstractConnection::createIdentifier(Supplier::WHATEVER) => [
                'type' => Cycle::TYPE_MONITOR,
            ],
        ]
    ]
];

the usage use Application\Model\Supplier; is always deleted, while saving.

This issue is critical as the plugin is not usable anymore, right now.

klesun commented 3 years ago

Hm, thanks for the report, that would be pretty nasty, I'll look later today

klesun commented 3 years ago

Did it start recently? Did you ever tick "Remove unused use-s on save" or did plugin start behave this way on it's own? And what version of phpstorm are you using?

I tried your minimal example with 2021.01.05.001 version of plugin and 2020.3.2 (Ultimate Edition), but I could not reproduce the issue: ticking the setting makes unused imports to disappear on save, unticking makes them to stay. It is possible that plugin config got messed on your machine and shows the setting as unticked, even though it is actually ticked. if that is the case, I wonder if ticking it, then pressing "apply", then unticking and pressing "apply" again would fix the settings state...

Of course, perfectly would be to nail down the exact reason why you get this problems, I'll need your feedback on my questions to do that.

klesun commented 3 years ago

Just in case, you did mention you have this problem in multiple projects, is this flag unchecked in each of them? That could be a source of confusion, as it's a project wide setting, not IDE wide...

Also, does it, per chance, reproduce only when several projects are open at once? There is probably a possibility IDEA keeps one project window focused, and takes setting from this focused project, even though the file in which save handler runs belongs to a different project.

Also, do these "falsely used usages" get removed when there are some syntax errors in the file? It's not as bad as behaviour not matching the setting, but that could be fixed as well.

klesun commented 3 years ago

Also, to be 100% sure, did you try disabling the plugin and restarting the IDEA, does issue not reproduce then?

andimg93 commented 3 years ago

Since it says in the upper right corner that it is only a project related configuration, I would have thought that it is not applied across all projects.

In any case, I have actually activated the functionality in one project. There were no problems with it yet. Have that now actually only noticed after I have removed a use that has then caused trouble.

I use version 2020.3.2 of PHPStorm: image

The problem must exist based on several open projects. I closed all IDE instances and then opened only one problem project. Activating the plugin again now, there are no problems. Even if you tick the function to remove the unnecessary usages, the problem does not occur again 🧐.

andimg93 commented 3 years ago

Seems to be rather an unfortunate coincidence of several IDE instances/open projects and thus more an exceptional case. The priority is therefore not so high, even if the user should pay attention to it. Since it is a potential source of trouble/errors.

klesun commented 3 years ago

Hm, thank you very much, will try to play around to reproduce it. Based on your feedback and the code, my main theory is that plugin considers setting on for all open projects if at least one of them has it on.

PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);

If my theory is right, the problem is that I assume that IDEA would not allow me to access a document from context of a project that it does not belong to, but probably my assumption was wrong and it would after all.