Search code examples
phpstormjetbrains-ide

PhpStorm generate template from code selection


I frequently use PhpStorm's Extract variable & method refactorings. Is there a way to add/extend functionality that could create a new template file from the selected code, prompt for desired template path, and create an include/require statement for that template?

I'm asking either for an entry point into coding this functionality, or extending existing functionality. Or maybe it's already available and I missed it.


Solution

  • As @Ástþór mentioned, there is no such way to change the refactoring templates.

    You can use surround with live templates to emulate this behavior. This will not find duplicates and will not replace them as well, but may be it's close enough what you want.

    1. Add a surround live template like this one. Open the editor with Ctrl+Alt+S:

    add live template

    1. Edit the variables in order to get a nicer UX:

    edit template variables

    1. Select the variable you want to extract and select Code > Surround with Live Templates from the menu or press Ctrl+Alt+J.

    2. Adjust the templates to your needs.

    HTH