How to auto-insert curly braces after completing statements such as "if", "else", "switch", etc.?
Answered
Let's say I am typing "if" and click Enter, the following code appears:
if ()
How can I modify the code completion so that when I type "if", the following code appears?:
if () {
}
What I have tried:
1. Modify the coding style so that curly braces are always forced after "if" statements, though this doesn't affect code completion.
2. Add a custom Live Template to achieve what I want (it works), but now I have two options for auto-completing the "if" statement (the original completion and the Live Template), which makes me doubt this is the right way to achieve what I want.
Please sign in to leave a comment.
Please use the statement completion (Ctrl+Shift+Enter): https://www.jetbrains.com/help/idea/auto-completing-code.html#statements_completion .
Thank you Serge Baranov this works.
Small question: So from my understanding, it is not possible to make IntelliJ insert the necessary syntax elements (parentheses, braces, and semicolons) when choosing statements (such as "if", "else", etc.) from the code completion suggestions menu normally (i.e. using Enter, Tab, or the mouse)?
It may be easier to use surround templates for that: https://www.jetbrains.com/help/clion/creating-code-constructs-using-surround-templates.html .
Serge Baranov You didn't quite answer my question. The last suggestion doesn't seem to relate to what I was looking. I was just looking for an easy way to insert all the correct syntax elements when choosing statements constructs as lookup items from the auto-complete suggestion list without the need to press additional keyboard shortcuts. Is it possible? If not, I will just stick with your first suggestion.
There is no way to add curly braces when you select it from the completion popup by default. You could configure a live template for that: https://www.jetbrains.com/help/idea/using-live-templates.html . Just add a plain text template like "if () { }", set up a name for it and you will see it in code completion.
If you want these templates at the top of the completion list, add ide.completion.show.live.templates.on.top=true in Help | Edit Custom Properties and restart the IDE.
Serge Baranov Alright, I understand. Thank you!