Smart indentation on closing curly braces

2.1k views Asked by At

I come from MS Visual Studio background, there on entering a closing curly brace the IDE automatically indents all the code within the block which the curly brace closed. In eclipse I know that it is done by pressing Ctrl+Ikeys and I know how to change these shortcut keys. What I want to know is how to auto indent a block of code when closing the block by entering a closing curly brace.

2

There are 2 answers

0
Bananeweizen On BEST ANSWER

Eclipse JDT does auto indentation on typing (i.e. it increases indentation after an opening brace and decreases after a closing brace), so there is normally no need to explicitly run auto indentation or auto formatting if you are just writing new code from top to bottom.

You should verify however, that Project -> Context menu -> Properties -> Java Code Style -> Formatter does have an active Formatter profile.

Besides that, the most common used semi automated tool for cleaning up your source in Eclipse is Project -> Context menu -> Properties -> Java Editor -> Save Actions, which is used to trigger formatting and minor code rewritings. I recommend getting used to that one and basically activate all of its possible options.

0
mrjmh On

The accepted answer does not actually answer the question.

The original author describes a very nice feature in Visual Studio that I also miss when in Eclipse.

The use case is when you have some existing code you are refactoring, and in similar scenarios when writing new code. Say you have several lines in a method that you realize require conditional around them. You begin by adding the conditional and the opening curly brace, then arrow-key down to the end of the last statement, hit return, then enter the closing brace.

At this point Visual Studio indents all of the lines between your opening and curly braces. the Eclipse java editor does not do this, although you could remember to hold "shift" while using arrow-down (to select the lines) then press Ctrl-I before you pressed return and added your closing brace. That requires more key strokes, and is error prone too, since you could easily finger slip and delete all the selected lines.

It's just one of the nice features in VS that would be nice in Eclipse.