Closing braces don't unindent it automatically

Answered

I am using Java and closing braces don't unindent it automatically as expected.

 

For example, when I try the following:

 

try {

  foo();

 

Then, when I type } I expect that closing brace will be unindented automatically so that it's aligned with try {.

Instead, it ends up as follows:

 

try {

  foo();

  }

 

I spent some time trying to find answers using Google but with no luck.  So many unrelated questions are returned and I can't find this exact question.

 

I suspect the answer should be really easy.  Maybe the code styling for this is somehow disabled somewhere.

0
3 comments

What result you see when you use Code | Reformat Code?

Do you have Settings (Preferences on macOS) | Editor | General | Smart Keys | Reformat block on typing '}' enabled?

Check also wit Settings (Preferences on macOS) | Editor | Code Style | Enable EditorConfig support and Detect and use existing file indents for editing options disabled.

0
Avatar
Permanently deleted user

Thank you, Andrew.

Code | Reformat Code did the right thing if I put the catch block too.

My (Preferences on macOS) | Editor | General | Smart Keys | Reformat block on typing '}' is enabled.

(Preferences on macOS) | Editor | Code Style | Enable EditorConfig support and Detect and use existing file indents for editing options was enabled but it was the same if I disabled it.  The .editorconfig file only contains indent_size and indent_style.

Maybe this is specific to only try and catch. The exact use case is I have a line that I wrap in a try-catch block.  For example, I have:

foo();

Then, I add a new line before it and type 'try {'.  Then, I indent the line foo();  Finally, I add another new line after 'foo()' and then type }.  It's not unindented at this point.  Even Code | Reformat Code doesn't work.

However, if I enter 'catch (Exception e) {}' after it and then run Code | Reformat Code, then it's correctly indented.

The reason this was annoying is when I tried to manually unindent it by hitting the backspace key before typing '}', it deletes the new line and the cursor goes back to the end of the 'foo();' line instead of reducing the indent by one indent size.

Seems like there is an easy workaround.  If I just type 'try {' in front of 'foo();' then hit the enter key, foo(); is indented automatically and the closing brace is put after that line.

0

I have a line that I wrap in a try-catch block.  For example, I have:

foo();

Then, I add a new line before it and type 'try {'.  Then, I indent the line foo();  Finally, I add another new line after 'foo()' and then type }.  It's not unindented at this point.  

Cannot reproduce it with 2019.2.1. What version do you use?

 

Anyway there is special action for wrapping code into a pre-defined try/catch block.

0

Please sign in to leave a comment.