klesun / deep-assoc-completion

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

Autcomplete in global scope #167

Closed andrew-svirin closed 3 years ago

andrew-svirin commented 3 years ago

First of all, thanks for your plugin.

I trying to make autocomplete for Pimple with you plugin, but i do not know how to make autocomplete

For example i have generated file _ide_helper.php with such records:

$app['twig.form.renderer']=new Symfony\Bridge\Twig\Form\TwigRenderer();

and using in other files like this

$app['twig.form.renderer']->render(); //autocompletes good.

function($app){
$app['twig.form.renderer']->render(); //do not autocompletes.
}

function() use ($app){
$app['twig.form.renderer']->render(); //do not autocompletes.
}

Question: Is it possible to force autocomplete for some variable be autocompleted global?

klesun commented 3 years ago

Hi. I believe you need to bind with global $app; to use a global variable in a function

Like:

function someName() {
    global $app;
    $app['twig.form.renderer']->render(); // completion should work if you bound the var with `global`
}

See: https://www.php.net/manual/en/language.variables.scope.php

andrew-svirin commented 3 years ago
$app['twig.form.renderer']=new Symfony\Bridge\Twig\Form\TwigRenderer();
$app['twig.form.renderer']->render(); //here works
function() use ($app){
$app['twig.form.renderer']->render(); //here not works
}
andrew-svirin commented 3 years ago

With global initialization do not working also (

image

klesun commented 3 years ago

hm... could it be that you are using a "Scratch File" for this code?

klesun commented 3 years ago

I had this issue reproducing in a Scratch File, but when I created a real index.php file, it worked ok

klesun commented 3 years ago

image

andrew-svirin commented 3 years ago

I understood what the difference. Your case is working on my code as well. Difference is in that a have declared in another file _ide_autocomplete.php

$app['twig.form.renderer']=new Symfony\Bridge\Twig\Form\TwigRenderer();

So then autocomplete works only in global scope, but inside functions don`t.

klesun commented 3 years ago

Hm...

image

andrew-svirin commented 3 years ago

Yes, you again right. Then difference in another. I using framework that is very old (Silex) And here $app also initialized like $app = Application(); so i have construction like on screen image

andrew-svirin commented 3 years ago

image image image

klesun commented 3 years ago

I wonder what causes plugin to go bananas here... Not use() per chance?

klesun commented 3 years ago

Yeah, I think it's the use(). Can you remove $app from the use() list? You closure on it with global anyway, so your app should still work...

Supporting use() is tricky and supporting global vars is tricky - supporting use() of global vars would be tricky^2, so better use just global instead of use() if possible...

andrew-svirin commented 3 years ago

image This way works. Thanks a lot for assist.