Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shortcut for inserting PHP's object operator upon code completion in PhpStorm?

Let's say I want to type the following in PhpStorm:

$longObjectName->propertyName = 'some value';

Seems innocent enough, no? If I start typing longOb, code completion kicks in and helpfully provides $longObjectName as a suggestion, since it exists elsewhere in my project. I think to myself, "Ah perfect, that's exactly what I wanted", so I hit Enter or Tab to accept the suggestion. At this point, I'm feeling pretty happy.

But now I want to insert ->, PHP's awkward but familiar object operator. If I type it manually, that's three whole keystrokes (including Shift), which makes me feel just a bit sad. A distant, nagging doubt begins to enter my mind. "Three keystrokes? What kind of evil IDE is this? Who are these ruthless dictators at JetBrains??"

The emotional roller coaster continues when I see the following in PhpStorm's Tip of the Day dialog, bringing a minuscule but insistent glimmer of hope to my dark, Monokai-schemed world:

When using Code Completion, you can accept the currently highlighted selection in the popup list with the period character (.), comma (,), semicolon (;), space and other characters.

The selected name is automatically entered in the editor followed by the entered character.

In JavaScript, this means I can type longOb and hit . to both accept the first code completion suggestion and insert the JS object operator, resulting in longObjectName., at which point I can keep typing a property name and go on autocompleting all day long without ever hitting Enter. Amazing. Revolutionary even.

Now for some devastating news: it doesn't seem to work in PHP. (Fret not children—this harrowing tale is almost at its end.)

If I type longOb and then hit -, I get this:

longOb-      // :(

I'm pretty sure the PHP interpreter wouldn't like me very much if I tried to execute that.

(Side note: ., ,, and ; exhibit pretty much the same behavior, contrary to the quoted Tip of the Day above.)

So here's what I would get if I were to make my fantasy world a reality:

$longObjectName->[handy dandy code completion list, primed and ready for action]

Wouldn't that be flipping awesome?

So finally, we arrive at the ultimate question, with some redundant stuff added for those who didn't bother to read my action-packed, heartwrenching story:

Is there a single keyboard shortcut in PhpStorm for "Accept the currently highlighted code completion suggestion and insert the PHP object operator (->)"?

Or is this just a bug?

like image 362
peterjmag Avatar asked Mar 19 '14 17:03

peterjmag


2 Answers

Well, I solved this problem by recording a Macro and then binding it with a keyboard shortcut:

  1. Go to Edit | Macros | Start Macro Recording
  2. Type '->'
  3. Stop Macro Recording using the button in the lower right corner, then name it whatever you want.
  4. Assign Shortcut to it:
    • Go to File | Settings | Keymap | Macros |
    • Right click and choose 'Add Keyboard Shortcut'

I chose Ctrl+. as the shortcut and now I am more than happy. :)

like image 64
Naveed Avatar answered Oct 19 '22 23:10

Naveed


You can use autohotkey (http://www.autohotkey.com/) to create new keystrokes and replace PHP Object Operator for period or anything else.

For example, with a script like this:

^.::
Send ->
return

Will replace (ctrl + .) with (->), in anywhere in Windows.

Or

#IfWinActive ahk_class SunAwtFrame
.::
Send ->
return

Will replace . (period) with (->) only in PhpStorm Window (Or others with ahk_class SunAwtFrame). Use Window Spy to see the ahk_class class of any Windows Window.

You can use CTRL + . as . (period)

#IfWinActive ahk_class SunAwtFrame
^.::
Send .
return
like image 37
Daniel Maia Avatar answered Oct 19 '22 23:10

Daniel Maia