Search code examples
phpstormshortcut

PhpStorm shortcut to wrap text with function call


I'm in PhpStorm and I need to select some text, press some shortcut and have that text wrapped in a function call (that I would have defined somewhere in the settings beforehand).

For example:

  • "Hello World" would become input("Hello World").
  • $_GET["foo"] would become input($_GET["foo"]).

I don't know if this is even possible, but it could help me save so much time if so. Applying regex to solve this problem is unfortunately not possible. Manually selecting what I need to wrap isn't an issue.


Solution

  • Will the function name be the same every time or different?

    In any case: it can be done this way:

    • Make a Live Template of "surround template" type with the following content:
      $FUN$($SELECTION$)$END$
      
      The $SELECTION$ variable here tells that it's a surround template.
    • Apply correct Context (where this template can be used)
    • Give it an abbreviation (name used to locate & invoke it) and brief description.

    Here is mine: enter image description here

    NOTE: replace $FUN$ by a fixed function name if the function will always be the same. You can have additional templates with different abbreviations (that will have different hardcoded function names).

    To use it:

    Make a selection and invoke Code | Surround with... action where you select the right template. On Windows keymap it's Ctrl + Alt + T

    enter image description here

    In action (NOTE: it's without hardcoding the function name hence me typing the myFunc part):

    enter image description here
    (HINT: you can select the desired entry in a few keypresses if the name is unique -- just start typing the name in the popup -- the standard Speed Search work here)

    P.S. Code | Surround with... can have other (possibly irrelevant for you in this case) entries. To list Live Templates only, use the shortcut for Surround with Live Template... action (Ctrl + Alt + J here on my Windows keymap). You can check the shortcut or change it in the Settings/Preferences | Keymap:

    enter image description here

    This way the popup menu will be a bit shorter:

    enter image description here

    Less keypresses:

    You may be able to use Macros functionality to record the invoking the popup and selecting the right entry. You can then assign a custom shortcut to that Macros: select the text, hit the shortcut and it will playback the recorded sequence.

    Sadly I cannot 100% guarantee that Macros will always work nicely (sometimes/on some setups it can "eat" keypresses).

    P.S. It would be much easier if the IDE would support assigning keyboard shortcuts to specific Live Templates .. but it's in the backlog and no ideas on when this might be implemented. Anyway: https://youtrack.jetbrains.com/issue/IDEA-67811 -- watch this ticket (star/vote/comment) to get notified on any progress.


    P.S. You can also try Postfix completion. It's good for writing the code and not really suitable for your case (editing small parts of it), but who knows. You will have to make a custom postfix for this -- should not be an issue though.

    https://www.jetbrains.com/help/phpstorm/2021.3/auto-completing-code.html#postfix_completion