live templates - import class (like pressing alt+enter)

Hi,

I have live template like this

use \Ekv\System\Traits\DbTrait;

function __construct()
{
    $this->DB = $this->initDb();$END$
}

Can I get DbTrait imported like that?

use Ekv\System\Traits\DbTrait;

class Fake
{
use DbTrait;

function __construct()
{
$this->DB = $this->initDb();
}
}
0

Hi there,

Well you have a solid Live Template (except first "use Ekv\System...." line) -- just use it to insert that code snipped into the current file.

Once done -- just place caret on "DbTrait" in "use DbTrait;" and invoke Quick Fix menu (Alt+Enter or using mouse on light bulb icon) -- it should offer you import missing class. Just use it and selected import statement will be added in the right place.

Initial file:

<?php
class Fake
{
}

After inserting Live Template (note that there is no "use this\class;" line):

<?php
class Fake
{
    use DbTrait;

    function __construct()
    {
        $this->DB = $this->initDb();
    }
}

Now use that "import class" intention/quick fix as suggested earlier (the "DbTrait" should be highlighted as unknown).

 

Alternatively use your current Live Template as is and invoke Alt+Enter menu on "use \Ekv\System\Traits\DbTrait;" line (which will be inside the class) and then choose "Simplify FQN" option.

0

Thanks for your reply, but the question is can I avoid that manual work with importing by alt+enter. It's great hot-key, I use it quite often but still ... no possibility to make automatic import on template running? Regarding your answers I guess not =)

0

Maybe something like that is available? 

<?php
class Fake
{
    use DbTrait$CURSOR_POSITION1$; //do import manually, press tab and go to $CURSOR_POSITION2$

    function __construct()
    {
        $this->initDb();$CURSOR_POSITION2$
    }
}
0

Okay, thanks for your time.

0

The only other thing that comes into my mind is to use classNameComplete() or maybe complete() Live Templates functions to invoke Code Completion popup at CURSOR_POSITION1 while having "DbTrait" as default value ... but I'm not sure if it will work that way at all (most likely not -- either popup or default value).

https://www.jetbrains.com/help/phpstorm/2017.2/live-template-variables.html

0

I've tried that kind of functions but I can't make them work at all

 

0

You have placed them in a wrong place -- it should be inside "Edit Variables" screen (the button on the right side (not shown on your screenshot))

https://www.jetbrains.com/help/phpstorm/2017.2/edit-template-variables-dialog.html

0

Thank you very much, I'll try this out!

0

Thanks for your help again!

Your solution did the trick and almost in the way I wanted!

 

0

请先登录再写评论。