How to automatically convert if-else if statement

2019-01-23 23:09发布

Is there a hot key or easy way to replace an if-else if statement with a switch in IntelliJ? I would prefer not to manually convert the if-else if statement to a switch. I am looking to be able to highlight the if-else if statement and press a combination of keys or use a menu button.

5条回答
贼婆χ
2楼-- · 2019-01-23 23:20

I just found out that you have to click on the first if, wait for the light bulb icon to appear and then choose Replace if with Switch.

查看更多
虎瘦雄心在
3楼-- · 2019-01-23 23:21

When selected if/switch in Eclipse, press Ctrl+1 or right-click -> Quick fix and suggestions will pop-up.

查看更多
Luminary・发光体
4楼-- · 2019-01-23 23:23

Update: as @christopheml pointed out in comments on another answer, "Replace If with Switch" seems to have disappeared in IntelliJ 2018.1. A bug has been filed here.


There are IntelliJ intentions named "Replace If with Switch" and "Replace Switch with If". Check your preferences to see if those options are enabled. If so, I would expect that you would see a quickfix option (Option+Enter on Mac, Alt+Enter on Windows) whenever IntelliJ can automatically handle the conversion from one to the other.

查看更多
Melony?
5楼-- · 2019-01-23 23:41

Android studio (intelliJ idea):

Put your cursor on the switch/if statement and:

mac: option + return

win: alt + enter

查看更多
姐就是有狂的资本
6楼-- · 2019-01-23 23:41

If it doesn't offer the replacement anymore, its probably because disabled feature on "Intentions". (Android studio (IntelliJ))

Solution is simple:

  • File > Settings > Editor > Intentions - Search: "replace switch"

or

  • File > Settings - Search: "replace switch" > Editor > Intentions

and check the blank checkbox

查看更多
登录 后发表回答