Making Eclipse's Java code formatter ignore block comments

后端 未结 11 525
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 13:20

Is there a way to make Eclipse\'s built-in Java code formatter ignore comments? Whenever I run it, it turns this:

    /*
     * PSEUDOCODE
     * Read in us         


        
相关标签:
11条回答
  • 2020-12-04 13:57

    There is another solution that you can use to suppress the formatting of specific block comments. Use /*- (note the hyphen) at the beginning of the block comment, and the formatting won't be affected if you format the rest of the file.

    /*-
     * Here is a block comment with some very special
     * formatting that I want indent(1) to ignore.
     *
     *    one
     *        two
     *            three
     */
    

    Source: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141999.html#350

    0 讨论(0)
  • 2020-12-04 13:57

    You can change this in Windows - Preferences - Java - Code Style - Formatter, than click the Edit.. button, find Comments, choose the Never Join Lines.

    Then, it should be OK.

    0 讨论(0)
  • 2020-12-04 14:00

    It is language-dependent.

    For example, if working with javascript, you would go to "Window -> Preferences -> Javascript -> Code Style -> Formatter" and then edit the formatter options.

    Edit (reflecting changesin OP Questions

    For editing java code formatting, go to "Window -> Preferences -> Java -> Code Style -> Formatter"

    At the top of the panel you will see

    Active Profile:
    Eclipse [built-in]

    From there you have one button to the right, "Edit", and two below, "New..." and "Import...". I would recommend Editing the existing profile.

    In the edit profile dialog, there are a series of tabs along the top. The last tab is "Comments". To completely disable comment formatting, uncheck "Enable Javadoc comment formatting", "Enable block comment formatting", "Enable line comment formatting", and "Enable header comment formatting".

    0 讨论(0)
  • 2020-12-04 14:02

    Another possibility is to use HTML's <pre> in Javadoc:

    /**
     * <pre>
     *    this
     *   is
     *      kept
     *  as
     *    is
     * </pre>
     */
    

    At least this is how I tend to embed my ASCII-art in source code comments :)

    0 讨论(0)
  • 2020-12-04 14:03

    I just learned from a co-worker that Eclipse offers special formatting tags that can be used for this:

    // @formatter:off
    /*
     * ╔════════╦═══════╦══════════╗
     * ║ Month  ║ Sales ║ Position ║
     * ╠════════╬═══════╬══════════╣
     * ║ June   ║ 44k   ║ 2nd      ║
     * ║ July   ║ 39k   ║ 2nd      ║
     * ║ August ║ 49k   ║ 4th      ║
     * ╚════════╩═══════╩══════════╝
     *
     * This comment shouldn't be formatted, and will now be ignored by the formatter.
     */
    // @formatter:on
    

    Note that you may need to manually enable this feature through the Preferences menu → Java > Code Style > Formatter, clicking on Edit, selecting the Off/On Tags tab and checking Enable Off/On tags (source).

    A quick Google for the string @formatter:off brought me to this other SO answer, which mentioned this feature in the context of disabling the formatter for code blocks. I've confirmed that it works for line comments, "normal" block comments and Javadoc block comments as well.

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