Is there a shortcut for inserting PHP's object

2019-01-22 21:45发布

问题:

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?

回答1:

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. :)



回答2:

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


回答3:

1) As far as I remember this kind of completion was asked to be removed for PHP code (too unusual) .. but I cannot find such ticket right now (already spend over 20 mins on this) .. so I could be wrong (it was quite some time ago .. so possibly I'm confused with another code-completion improvement which was hard-configured to behave differently for PHP code only).

Kind of related to "why it was disabled/what problems where there in the past":

  • http://youtrack.jetbrains.com/issue/WI-7013
  • http://youtrack.jetbrains.com/issue/IDEA-88179
  • http://youtrack.jetbrains.com/issue/IDEA-59718

In any case: there is an open ticket to actually having it working: http://youtrack.jetbrains.com/issue/WI-21481 (only 1 vote so far).

2) There is no "complete with ->" action available. Related ticket: http://youtrack.jetbrains.com/issue/WI-17658 (star/vote/comment to get notified on progress)