IntelliJ IDEA 中类似 Eclipse 的注释格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6813662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:41:51  来源:igfitidea点击:

Eclipse-like comment formatting in IntelliJ IDEA?

eclipseintellij-idea

提问by python dude

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

在 Eclipse 中,我可以通过选择注释并按 来格式化注释Shift + Ctrl + F。例如,当我选择这样的方法注释时:

/**
 * This method
 * does some stuff.
 */

and press Shift + Ctrl + F, Eclipse automatically wraps it:

并按下Shift + Ctrl + F,Eclipse 会自动包装它:

/**
 * This method does some stuff.
 */

Is there anything comparable to this in IDEA?

IDEA中有什么可以与此相媲美的吗?

EDIT: To clarify, I'm looking for comment formatting that also breaks lines that are too long into multiple lines.

编辑:为了澄清,我正在寻找注释格式,该格式也将太长的行分成多行。

采纳答案by CrazyCoder

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.

你能得到的最接近的东西是Edit| Join Lines( Ctrl+ Shift+ J)。您必须先选择要加入的行。

To wrap long comments enable Settings| Code Style| JavaDoc| Wrap at right margin.

包装长评论启用Settings| Code Style| JavaDoc| Wrap at right margin.

回答by Ted M. Young

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.

对于 Javadoc 注释,您需要确保选中“Wrap at right margin”设置。请参阅“其他”下的“代码样式”>“JavaDoc”。但是,此设置似乎仅在您重新格式化整个文件时才生效,因为仅重新格式化超出右边距的 Javadoc(即,选择 Javadoc,然后执行 aCode (menu) > Reformat Code...CtrlAltL)并不会强制其换行。如果我重新格式化整个文件,那么它会按预期在边距处换行。

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".

这似乎是一个错误(尽管似乎没有报告过),因为如果您必须设置“确保不超过右边距”,那么选择 Javadoc 文本并重新格式化代码确实会包装线。此设置在Settings > Code Style > Wrapping and Braces. 您还可以在“设置”对话框中搜索“确保右边距”。

You'll still have to manually join the lines using CtrlShiftJ

您仍然必须使用手动加入行 CtrlShiftJ

This might be worthy of an improvement request to JetBrains.

这可能值得向 JetBrains 提出改进要求。

回答by gerrytan

enter image description here

在此处输入图片说明

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

当您执行“重新格式化代码”(Mac 中的⌥⌘L)时,现有评论将被重新格式化。

回答by Jerry101

@kghastie uncovered the key.

@kghastie 发现了钥匙。

Steps:

脚步:

  1. Set the Code Style > Java > JavaDoc > Wrap at right marginsetting.
  2. Select the full lines of the entire JavaDoc comment.
  3. Reformat Code (Ctrl-Alt-L or ??L).
  1. 设置Code Style > Java > JavaDoc > Wrap at right margin设置。
  2. 选择整个 JavaDoc 注释的完整行
  3. 重新格式化代码(Ctrl-Alt-L 或 ??L)。

Lesser alternative:

较小的选择:

  1. Set the Code Style > Java > JavaDoc > Wrap at right marginsetting and the Code Style > Java > Wrapping and Braces > Ensure right margin is not exceededsetting.
  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).
  1. 设置Code Style > Java > JavaDoc > Wrap at right margin设置和Code Style > Java > Wrapping and Braces > Ensure right margin is not exceeded设置。
  2. 在 JavaDoc 注释中选择一些文本。
  3. 加入行 (Ctrl-Shift-J),然后重新格式化代码(Ctrl-Alt-L 或 ??L)。
    • 当心:这将使所有选定的行保持连接,即使在您有段落分隔符(<p/>\n\n)的地方也是如此。

回答by Matthew Cornell

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!

我在 Mac 上使用 IntelliJ 14,它有一个填充段落命令。通过令人敬畏的通用 Command-Shift-A 操作搜索功能访问它。奇迹般有效!

回答by Joshua Michael Waggoner

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...

这是一个 hack,不是一个很好的解决方案,但是如果您有一个代码块想要像这样格式化并且它非常需要自动格式化,因为它最多超过 80 行,或者它只是不可读......

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+TABto move it back 4 spaces and remove the dummy if statement

你可以把if ("foo" == "bar") {任何你想要格式化的东西放在上面,然后}在 if 语句的底部,关闭它,瞧,你的代码应该自动缩进,自动格式化等......然后把它拿出来,突出显示您刚刚格式化的所有内容,然后按SHIFT+TAB将其移回 4 个空格并删除虚拟if 语句