In Intellij, how do I toggle between camel case and underscore spaced?

此生再无相见时 提交于 2019-12-31 07:53:27

问题


At my company we have two different style guides for java vs sql. In java I have a field named historyOfPresentIllness and when i write the sql, I want to name it history_of_present_illness. Is there a keyboard shortcut to switch from one to the other when I have the phrase highlighted? Or perhaps a plugin that can do this?

While I'm asking, I may as well ask if there's a way to turn historyOfPresentIllness to history-of-present-illness. That's from java to clojure style.


回答1:


I use a plugin called String Manipulation which has the capabilities you're looking for (and more).

Select historyOfPresentIllness and press Alt+M to bring up the plugin menu, then press:

  • 5 - To snake_case (or to camelCase) which converts to history_of_present_illness
  • 6 - To hyphen-case (or to snake_case) which converts to history-of-present-illness

To make this easier, you could set up a shortcut at File | Settings | Keymap.


A quick search of the plugin repository for "camel" showed a plugin called CamelCase which does exactly what you're looking for with SHIFT+ALT+U by toggling between various formats:

historyOfPresentIllness --> history_of_present_illness --> HISTORY_OF_PRESENT_ILLNESS --> HistoryOfPresentIllness --> historyOfPresentIllness

However, after trying it out, there seems to be a bug with this plugin which prevents you from undoing any changes. Update: it appears this bug has been fixed for a while now, so the CamelCase plugin is probably the way to go.




回答2:


Very simple press Clr + F to open Find/Replace panel and check [✓] Regex copy past regex

Find: [_]{1,1}([a-z])

Replace: \U$1

Press [Replace all] button, Enjoy


Thanks @piotrek for _some_awe_var to _someAweVar

Use Find: (\w)[_]{1,1}([a-z])
Replace: $1\U$2




回答3:


From snake_case to CamelCase

  • Find: (\w)[_]{1,1}([a-z])
  • Replace: $1\U$2
  • Settings:
    • Match Case
    • Regex

From CamelCase to snake_case:

  • Find: ([A-Z])
  • Replace: \_\L$1
  • Settings:
    • Match Case
    • Regex



回答4:


Answer above is almost perfect, but note that it will change variables like _something or this._something into Something and this.Something. I didn't want that in my case, as leading _ was used to denote "private" variables (old JS project). I slightly modified this approach:

Find: (\w)[_]{1,1}([a-z])

Replace: $1\U$2

This will ensure that only variables with _ is in the middle will be affected.




回答5:


If you are OK with PyCharm also refactoring usages, launch the "Rename" tool (Refactor > Rename). The window pops up with drop down list and you should see the snake_case version of the text in the list (you can open the window and switch to the snake_case with key-strokes so it can be pretty fast).



来源:https://stackoverflow.com/questions/17350359/in-intellij-how-do-i-toggle-between-camel-case-and-underscore-spaced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!