QDox
  1. QDox
  2. QDOX-146

Retaining whitespace in comments

    Details

    • Type: New Feature New Feature
    • Status: Resolved Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.6.3
    • Fix Version/s: 1.10
    • Component/s: Java API, Parser
    • Labels:
      None
    • Number of attachments :
      0

      Description

      As per the dev-list, it might be useful, in some cases, to preserve whitespace in javadoc comments:
      http://www.nabble.com/Retaining-whitespace-in-comments-td18738908.html

      Since it is not always desired, I'd suggest adding a method and/or parameter so that users can get one or the other as they see fit.

      I've tried looking into the source to see where this was happening, but haven't been able to locate it precisely.

        Activity

        Hide
        Grégory Joseph added a comment -

        Here's a testcase that shows the potentially desired behavior:

            public void testWhitespaceCanBeRetainedInJavadoc() {
                String sourceCode = ""
                        + "package com.acme.thing;\n"
                        + "\n"
                        + "/**\n"
                        + " * This class does something.\n"
                        + " *     chalala\n"
                        + " *         cha  **  lala\n"
                        + " **/\n"
                        + "public class AClassName {\n"
                        + "}";
                JavaDocBuilder builder = new JavaDocBuilder();
                builder.addSource(new StringReader(sourceCode));
                JavaClass aClass = builder.getClassByName("com.acme.thing.AClassName");
                assertEquals("This class does something.\n"
                        + "    chalala\n"
                        + "    cha  **  lala", aClass.getComment());
            }
        

        — as you can see, I think the concern is mainly about leading whitespace on each line.

        Show
        Grégory Joseph added a comment - Here's a testcase that shows the potentially desired behavior: public void testWhitespaceCanBeRetainedInJavadoc() { String sourceCode = "" + " package com.acme.thing;\n" + "\n" + "/**\n" + " * This class does something.\n" + " * chalala\n" + " * cha ** lala\n" + " **/\n" + " public class AClassName {\n" + "}" ; JavaDocBuilder builder = new JavaDocBuilder(); builder.addSource( new StringReader(sourceCode)); JavaClass aClass = builder.getClassByName( "com.acme.thing.AClassName" ); assertEquals( "This class does something.\n" + " chalala\n" + " cha ** lala" , aClass.getComment()); } — as you can see, I think the concern is mainly about leading whitespace on each line.
        Hide
        Paul Hammant added a comment -

        Gregory, look at line 211- 226 in lexer.flex.

        I think the stripping of spaces is baked into QDox at the parser stage. It would most likely need to be a change for QDox rather than a switch.

        In AbstractJavaEntity.commentHeader(..) there is an attempt to format again in /** Javadoc Style */ but it does not add back the spaces.

        Show
        Paul Hammant added a comment - Gregory, look at line 211- 226 in lexer.flex. I think the stripping of spaces is baked into QDox at the parser stage. It would most likely need to be a change for QDox rather than a switch. In AbstractJavaEntity.commentHeader(..) there is an attempt to format again in /** Javadoc Style */ but it does not add back the spaces.
        Paul Hammant made changes -
        Field Original Value New Value
        Priority Major [ 3 ] Minor [ 4 ]
        Hide
        Robert Scholte added a comment -

        I've had a look at the lexer and I think it's a bit too complecated right now.
        Why not read per line instead of per word? Everything after a javaDocComment-linestart can be copied as it is, remaining spaces, asterisks, etc.
        The only reason I can think of is detecting doclet-tags.
        For the doclet case you could use javaDocComment-linestart followed by an '@', push state and read until the space and pop back.

        Are there cases which won't fit this strategy?

        If this seems solid enough, then most lexer-tests specific for javadocs have to be rewritten. Any ideas?

        Show
        Robert Scholte added a comment - I've had a look at the lexer and I think it's a bit too complecated right now. Why not read per line instead of per word? Everything after a javaDocComment-linestart can be copied as it is, remaining spaces, asterisks, etc. The only reason I can think of is detecting doclet-tags. For the doclet case you could use javaDocComment-linestart followed by an '@', push state and read until the space and pop back. Are there cases which won't fit this strategy? If this seems solid enough, then most lexer-tests specific for javadocs have to be rewritten. Any ideas?
        Hide
        Robert Scholte added a comment -

        Take a look at the test below. I think I found a way to parse javadoccomments, but it looks like both tests collide with each other.
        Unless we say: JavadocTags should remove leading spaces, comments not.
        But then there´s another thing: in the chalala-example I think the last line should contain the extra 4 spaces in front of 'cha'.

        Any ideas?

        JavaDocBuilderTest.java
            public void testTagValuesCanSpanMultipleLines() {
                String source = "" +
                        "/**\n" +
                        " * @bar.baz foo=\"this is\\\n" + 
                        " *       multilined\"\n" +
                        " */\n" +
                        "class x{}";
                builder.addSource(new StringReader(source));
                JavaClass x = builder.getClassByName("x");
                DocletTag tag = x.getTagByName("bar.baz");
                assertEquals("foo=\"this is\\\nmultilined\"", tag.getValue());
                assertEquals("this is\nmultilined", tag.getNamedParameter("foo"));
            }
        
        Show
        Robert Scholte added a comment - Take a look at the test below. I think I found a way to parse javadoccomments, but it looks like both tests collide with each other. Unless we say: JavadocTags should remove leading spaces, comments not. But then there´s another thing: in the chalala-example I think the last line should contain the extra 4 spaces in front of 'cha'. Any ideas? JavaDocBuilderTest.java public void testTagValuesCanSpanMultipleLines() { String source = "" + "/**\n" + " * @bar.baz foo=\" this is\\\n" + " * multilined\" \n" + " */\n" + "class x{}" ; builder.addSource( new StringReader(source)); JavaClass x = builder.getClassByName( "x" ); DocletTag tag = x.getTagByName( "bar.baz" ); assertEquals( "foo=\" this is\\\nmultilined\"", tag.getValue()); assertEquals( " this is\nmultilined" , tag.getNamedParameter( "foo" )); }
        Hide
        Robert Scholte added a comment -

        Issue resolved like mentioned: add those 4 extra spaces so the comment stays as it is.

        Show
        Robert Scholte added a comment - Issue resolved like mentioned: add those 4 extra spaces so the comment stays as it is.
        Robert Scholte made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 1.10 [ 15020 ]
        Resolution Fixed [ 1 ]
        Assignee Robert Scholte [ rfscholte ]

          People

          • Assignee:
            Robert Scholte
            Reporter:
            Grégory Joseph
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: