In Eclipse, I can format comments by selecting them and pressing Shift + Ctrl + F
. For example, when I select a method comment like this:
/**
* This method
* does some stuff.
*/
and press Shift + Ctrl + F
, Eclipse automatically wraps it:
/**
* This method does some stuff.
*/
Is there anything comparable to this in IDEA?
EDIT: To clarify, I'm looking for comment formatting that also breaks lines that are too long into multiple lines.
The closest thing that you can get is Edit
| Join Lines
(Ctrl+Shift+J). You have to select the lines you want to join first.
To wrap long comments enable Settings
| Code Style
| JavaDoc
| Wrap at right margin
.
For Javadoc comments, you want to make sure the "Wrap at right margin" setting is checked. See Code Style > JavaDoc, under "Other". However, this setting only seems to take effect when you reformat the whole file, since a reformat of just the Javadoc (i.e., select the Javadoc, then do a Code (menu) > Reformat Code...
or CtrlAltL) that exceeds the right margin doesn't force it to wrap. If I reformat the entire file, then it wraps at the margin as expected.
This seems like a bug (though one that doesn't seem to have been reported), since if you have to set the "Ensure right margin is not exceeded" checked, then selecting the Javadoc text and doing a reformat code does indeed wrap the lines. This setting is in Settings > Code Style > Wrapping and Braces
. You can also do a search in the Settings dialog for "ensure right margin".
You'll still have to manually join the lines using CtrlShiftJ
This might be worthy of an improvement request to JetBrains.
Existing comment will be reformatted when you do "Reformat Code" (⌥⌘L in Mac).
@kghastie uncovered the key.
Steps:
- Set the
Code Style > Java > JavaDoc > Wrap at right margin
setting.
- Select the full lines of the entire JavaDoc comment.
- Reformat Code (Ctrl-Alt-L or ⌥⌘L).
Lesser alternative:
- Set the
Code Style > Java > JavaDoc > Wrap at right margin
setting and the Code Style > Java > Wrapping and Braces > Ensure right margin is not exceeded
setting.
- Select some text within a JavaDoc comment.
- Join Lines (Ctrl-Shift-J) followed by reformat Code (Ctrl-Alt-L or ⌥⌘L).
- Beware: This will leave all the selected lines joined even where you had paragraph breaks (
<p/>
or \n\n
).
I'm using IntelliJ 14 on a Mac, which has a Fill Paragraph command. Access it via the awesome universal Command-Shift-A action search feature. Works like a charm!
This is a hack, not a really good solution, but if you have a block of code that you want formatted like this and it's in serious need of auto format, because it's going over the 80 line max, or it's just unreadable...
You can just put if ("foo" == "bar") {
on top of whatever you want formatted, and then and the}
at the bottom of the if statement, to close it, and voila, your code should auto-indent, auto format, etc... Then take it out, highlight all of what you just formatted and press SHIFT+TAB to move it back 4 spaces and remove the dummy if statement