Eclipse-like comment formatting in IntelliJ IDEA?

前端 未结 7 1579
南方客
南方客 2020-12-11 00:38

In Eclipse, I can format comments by selecting them and pressing Shift + Ctrl + F. For example, when I select a method comment like this:

/**
 *         


        
相关标签:
7条回答
  • 2020-12-11 00:42

    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.

    0 讨论(0)
  • 2020-12-11 00:45

    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.

    0 讨论(0)
  • 2020-12-11 00:45

    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

    0 讨论(0)
  • 2020-12-11 00:47

    @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).
    0 讨论(0)
  • 2020-12-11 00:47

    The JetBrains plugin Wrap to Column is made for this:

    From the overview:

    Wraps text to the specified column width. Similar to the Emacs command 'Fill Paragraph' and Vim's gq (format lines) command. This is a replacement for the native Intellij Fill Paragraph command, which doesn't work quite how I need it to.

    This plugin provies two IDE actions:

    • Wrap Line to Column: Wraps selected text or the current line if no text is selected. This is useful for IdeaVim users who wish to pair the command with motions like vip (select current paragraph).
    • Wrap Paragraph to Column: Wraps the paragraph (multiple lines) in which the cursor appears. No selection is needed, and will be ignored.
    0 讨论(0)
  • 2020-12-11 00:50

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

    0 讨论(0)
提交回复
热议问题