Javadoc: line breaks without HTML-tags?
Sorry for a probable FAQ kind of question, but I just can't find the answer.
As far as I remember Eclipse, a blank line in a Javadoc comment is displayed (in in-source Javadoc popups) as a line break (with extra vertical spacing).
In Netbeans, however, this is not the case.
Can I configure Javadoc to interpret a blank line as a line break?
Additional question: Can I override default Netbeans behavior (related to this) for in-source Javadoc popups?
What I'm talking about is:
Source
/**
* Paragraph One
*
* Paragraph Two
*/
void someMethod() { }
Eclipse interpretation
Paragraph One
Paragraph Two
Netbeans interpretation
Paragraph One Paragraph Two
Solution 1:
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>
.
Solution 2:
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.
Solution 3:
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, choseOptions
- Go to
Editor
tab, thenFormatting
tab - In the
Language
menu choseJava
, and inCategory
menu choseComments
- Check the
Preserve New Lines
checkbox in theGeneral
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 theJavadoc
section if you also want to add<p>
tag.
Solution 4:
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
Solution 5:
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;
}