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();
}
}
请先登录再写评论。
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:
After inserting Live Template (note that there is no "use this\class;" line):
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.
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 =)
Maybe something like that is available?
No.
Okay, thanks for your time.
The only other thing that comes into my mind is to use
classNameComplete()
or maybecomplete()
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
I've tried that kind of functions but I can't make them work at all
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
Thank you very much, I'll try this out!
Thanks for your help again!
Your solution did the trick and almost in the way I wanted!