没有 HTML 标记的换行符?

对于一个可能的常见问题,我很抱歉,但我就是找不到答案。

就我对 Eclipse 的记忆而言,Javadoc 注释中的空行(在内部 Javadoc 弹出窗口中)显示为换行符(带有额外的垂直间距)。

但在 Netbeans,情况并非如此。

我可以配置 Javadoc 将空行解释为换行符吗?

附加问题: 我可以为内部 Javadoc 弹出窗口覆盖默认的 Netbeans 行为(与此相关)吗?

我想说的是:

来源

/**
* Paragraph One
*
* Paragraph Two
*/
void someMethod() { }

日食解释

 Paragraph One


Paragraph Two

Netbeans 解释

 Paragraph One Paragraph Two
122289 次浏览

JavaDoc displays the way the CSS styles have been defined. You could edit the CSS styles associated with paragraph tags to do this:

p {
line-height: 25px;
}

It has nothing to do with Netbeans. I suspect you are looking at the source code in one case and the output of Javadoc in the other case. Newlines are not significant in HTML: ergo the output will not show them. If you want a newline use a <p> or a <br>.

This is a pseudo-solution
(which sadly affects only generated javadoc, but does not affect Netbeans' in-source javadoc display).

Specify a stylesheet which contain the following:

div.block {
white-space: pre;
}

I have no idea what Eclipse is doing here, but if you want this behavior in general (not only an IDE), you may have to create a new Doclet (which may be based on the default HTML doclet) instead, there inserting a <p> at every empty line or such.

I agree with you, HTML doesn't belong in source-code. Sadly, I didn't find much help Googling around for this. It's actually quite easy to implement.

Here's the custom Doclet that you can compile and use:

import com.sun.javadoc.*;
import com.sun.tools.doclets.standard.*;


/**
* Formats text-only comments with HTML.
*/
@SuppressWarnings("restriction")
public final class TextDoclet {
private static final Pattern NEWLINE_REGEX = Pattern.compile("\\n");
private static final String BR = "<br/>\n";


public static boolean start(RootDoc rootDoc) {
for ( ClassDoc classdoc : rootDoc.classes())
classdoc.setRawCommentText(formatText(classdoc.getRawCommentText()));


return Standard.start(rootDoc);
}


private static String formatText(String text) {
return NEWLINE_REGEX.matcher(text).replaceAll(BR);
}
}

An example of how to invoke it using javadoc:

javadoc -docletpath ~/project/text-doclet/target/text-doclet-1.0.0-SNAPSHOT.jar -doclet com.myorg.textdoclet.TextDoclet -sourcepath ~/project/myapp/src/main/java -subpackages com.myorg.myapp

I'm not sure if this helps for OP's case, however I put <pre></pre> around my document so netbean does not mess up my formatting. So it will look like

/**
* <pre>
* Paragraph One
*
* Paragraph Two
* </pre>
*/

This is closest I get to showing new lines in text format. I'm using NetBeans 7.1.2. This way using code format option will not reformat the document. Showing doc in hints is still formatted.

Update: in Netbeans 8.x there is an option in code formatting to disable formatting comments.

There is already an option in NetBeans - tested on version 8.2 - that allows you to preserve new lines in your comments, and/or add a <p> tag to your Javadoc if needed

  • Just from the Tools menu, chose Options
  • Go to Editor tab, then Formatting tab
  • In the Language menu chose Java, and in Category menu chose Comments
  • Check the Preserve New Lines checkbox in the General section if you want to preserve new lines in your comments. This will preserve new lines without adding <p> tag
  • Check Generate "<p>" on Blank Lines checkbox in the Javadoc section if you also want to add <p> tag.

enter image description here