Eclipse-like comment formatting in IntelliJ IDEA?

2019-01-26 03:06发布

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.

6条回答
Fickle 薄情
2楼-- · 2019-01-26 03:14

enter image description here

Existing comment will be reformatted when you do "Reformat Code" (⌥⌘L in Mac).

查看更多
一夜七次
3楼-- · 2019-01-26 03:17

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!

查看更多
放我归山
4楼-- · 2019-01-26 03:24

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

查看更多
聊天终结者
5楼-- · 2019-01-26 03:28

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.

查看更多
来,给爷笑一个
6楼-- · 2019-01-26 03:35

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.

查看更多
Viruses.
7楼-- · 2019-01-26 03:36

@kghastie uncovered the key.

Steps:

  1. Set the Code Style > Java > JavaDoc > Wrap at right margin setting.
  2. Select the full lines of the entire JavaDoc comment.
  3. Reformat Code (Ctrl-Alt-L or ⌥⌘L).

Lesser alternative:

  1. 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.
  2. Select some text within a JavaDoc comment.
  3. 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).
查看更多
登录 后发表回答