Javadoc: line breaks without HTML-tags?

110,059

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

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

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;
}
Share:
110,059
java.is.for.desktop
Author by

java.is.for.desktop

Updated on July 08, 2022

Comments

  • java.is.for.desktop
    java.is.for.desktop almost 2 years

    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
    
  • java.is.for.desktop
    java.is.for.desktop about 13 years
    Thank you, but I'm sure that Netbeans doesn't interpret a blank line as a paragraph at all (see "Netbeans interpretation" in my question). I mean: you can only define line spacing if you have lines.
  • adarshr
    adarshr about 13 years
    Hmm. I saw that. But not really sure if Netbeans provides an option to edit this.
  • java.is.for.desktop
    java.is.for.desktop about 13 years
    As I mentioned in the question, Eclipse does consider blank lines as paragraphs. So my assumption is that it has to be possible to achieve this by some javadoc configuration (Netbeans or without).
  • user207421
    user207421 about 13 years
    Eclipse shows you the source code however it is formatted. Netbeans 6.9.1 shows you the source code too, ditto, i.e. it doesn't ignore line breaks either. However if you are looking at the output of Javadoc by any means if would be incorrect for that means not to ignore line breaks.
  • Webel IT Australia - upvoter
    Webel IT Australia - upvoter about 7 years
    Definitely the best answer w.r.t. NetBeans 8.2. Easy. But you should please edit it to add that it only actually kicks in when you perform Format on the code (with existing line-breaks). One has to be careful, if there are already some <p> in there by hand it can sometimes add double <p>``<p> the first time. Thereafter it works as expected.