PhpStorm shortcut to wrap text with function call - phpstorm

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.

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:
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
In action (NOTE: it's without hardcoding the function name hence me typing the myFunc part):
(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:
This way the popup menu will be a bit shorter:
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

Related

How to make USE for class quickly in PhpStorm?

How i can quickly make
use \DateTime;
for new \DateTime();
in PhpStorm ? I saw on some videos guys do that with some hotkey or something like that.
You can find the most relevant contextual options with secondary mouse click. The one you want is Show Context Actions (you'll see your current keyboard shortcut beside, mine is Alt+Enter):
Additionally, clicking on the code and waiting for a second will often display a standalone light bulb that points to the same command:
Those are called intentions. There're two that apply here:
Replace with alias (to add use foo as bar;)
Simplify FQN (to just add use foo or remove it altogether if already in root namespace)
Your particular example is a strange use case (you only save one character worth of typing) but the feature rocks when you have nested namespaces.

When I create a new class="" the cursor starts outside the quotes in PhpStorm. How can I get it to go between the quotes so I can continue typing?

When I type in class="" it autofills and puts the cursor after the closing quote.
This means I need to delete a quote to enter the class name or click between them. Both of which ruin my workflow.
Is there a way to put the cursor inbetween the quotes in settings? And if there is, is there a way to jump out of the quotes and keep adding to my markup?
In Sublime Text it puts the cursor in the class and then tab takes you outside of it. I'm sure PhpStorm can do the same.
Instead of typing the whole class="" thing by yourself -- just let IDE autocompletion to do the job for you.
Just two characters (cl) is enough to make class entry first in the list (unless you have used some other similarly named attributes/properties recently that would temporarily bring them higher than class):
Completing with Enter will have the text inserted into current position with caret located in the right place class="[CARET_HERE]".
Completing with Tab does the same but replaces the text that is currently under caret (useful when changing class/image name/function/etc completely).
If standard code completion is not good enough for some reason or you do not like automatic completion popop (and prefer invoking it manually only when needed) -- you will be interested in Live Templates functionality that allows creating some abbreviation and expanding it into the final snippet with minimal key presses (e.g. cl[Tab] into class="|")
IDE also has options to:
insert quotes ("" or '') after typing = in XML/HTML attributes.
Settings/Preferences | Editor | General | Smart Keys --> Add quotes for attribute value on typing '=' and attribute completion
insert pair quotes (closing one) when entering opening one -- works in different contexts/languages. It will "eat" the closing quote preventing you from typing too many.
Settings/Preferences | Editor | General | Smart Keys --> Insert pair quote
In Sublime Text it puts the cursor in the class and then tab takes you outside of it. I'm sure PhpStorm can do the same.
If I understood you correctly (sorry, never used Sublime myself) -- No... and may not have it for quite some time (devs say that the way how IDE works somehow conflicts with proposed Tab or Esc behaviour).
Better explanation/arguments from both sides can be found in actual ticket: https://youtrack.jetbrains.com/issue/IDEABKL-6984

PhpStorm combine multiple Predefined Live Template functions

I have a project where my files are in "lisp-case" (hyphen delimited) and I would like to use the filename as a variable in a live template, but it must be converted to CamelCase first.
I found out that you can set the expression fileNameWithoutExtension() under "Edit Template Variables" and there is also a function called camelCase() which should be able to turn my filenames into CamelCase. But I cannot figure out how to combine those two. I tried doing camelCase(fileNameWithoutExtension()) but that does not work, unfortunately.
Is it possible to achieve this some other way?
camelCase(fileNameWithoutExtension())
It's a correct syntax.
Is it possible to achieve this some other way?
Please ensure that Skip if defined checkbox is checked for this variable.
If I try to wrap fileNameWithoutExtension() inside camelCase() and press enter to save the entry the window is closed but the change is not saved
It's an IDE bug (https://youtrack.jetbrains.com/issue/IDEA-132965 -- supposed to be fixed in current 2017.1.x version).
In any case: either press Enter before leaving the field ... or click on OK button straight away (you may then reopen this window to do further changes).
Also, when editing the field it's rendered like a select box which is kind of weird. Maybe it's a bug in this specific version. I'm on PhpStorm 10.0.4 for Mac.
Not a Mac user here .. but it is an editable drop-down box indeed (it lists all possible functions).

Is there a way to remove search result from the find tab in PhpStorm?

In PhpStorm 9.0.2 (I do not know about the newer versions, but I am afraid it will be the same) when you search for something and get the results in the Find tab, you can mark certain results as irrelevat by selecting them and pressing the del key and this will strike them out.
When you search for something very common, which can have 5000+ occurences, even by striking some results out, the Find tab becomes very bloated.
Is there a way to completely remove a result from the Find tab, instead of just striking it out?
When using Find/Replace in Path functionality and you know that you will get a lot of results it's better to limit your search scope by either specific path, specific file extensions or some custom scope (using Scopes functionality).
If such "limiting" cannot not help (e.g. you really need to go trough each occurrence) then use Alt + Delete (the Remove action).
Please note that this action is most likely available since PhpStorm v10 only (it was added only recently).
You can check if you have it and what the shortcut is via content menu:
You can use this keymap shortcut : ALT + SUPPR (this remove the result out of the find tab)
instead of
keymap shortcut : SUPPR (this JUST exclude the result of the find tab with underline this not remove)
EDIT :
This is the exact response for your issue. Look at this screenshoot which say all :)

Is there any way to switch multiple cursors with tab?

The situation is: in my html-file I have a lot of "href" attributes to be filled with specific links. And I can get multiple cursors on every needed place.(via Ctrl+D, or Alt+Enter).
Now it would be very helpful if I could switch between this cursors with tab(like emmet plugin does with self-generated html-content, if you know what I'm talking about).
You can have multi cursor, and switch between them in using :
Ctrl + F3
or
Ctrl + Shift + F3
You can get the behavior I believe you want by selecting all the "href" attributes using ctrl+d. If you are selecting all of them, you cna run the find_all_under command (alt + f3 in windows, unsure of other platforms but you can search for the command). Then, bookmark the cursor positions (ctrl+f2 in windows). You can then use f2 to cycle through the bookmarks. You could bind the appropriate command to tab if you want also, though you would have to do some work do you don't break normal tab behavior.
As a side note, I believe emmet simply inserts a snippet, so it defines various locations for the cursor to jump to through the built in snippet behavior.
#Jahnux solution may be more ideal since you wouldn't have to select initially, but I believe you would have to move the cursor back to the "href" attribute for continuing to the next token.
You may also want to investigate simply using the find functionality, though perhaps you have run into some limitations with that.