openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.02k stars 299 forks source link

TabsAndIndents: continuationIndentSize fails by `!DOCTYPE` #4234

Open ofaucheux opened 1 month ago

ofaucheux commented 1 month ago

I am using OpenRewrite Version 8.27.0 and the org.openrewrite.xml.format.TabsAndIndents recept.

The issue: a new line in the <!DOCTYPE> declaration element of an XML file is not indented with the continuationIndentSize as it should be.

Following test reproduces the issue:

    @DocumentExample
    @Test
    void continuationIndentsWithinDOCTYPE() {
        rewriteRun(
          spec -> spec.recipe(toRecipe(() -> new TabsAndIndentsVisitor<>(
            TabsAndIndentsStyle.DEFAULT.withIndentSize(2).withContinuationIndentSize(5)
          ))),
          xml(
            """
              <!DOCTYPE module PUBLIC
                "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
                "https://checkstyle.org/dtds/configuration_1_3.dtd">
              <project>
              </project>
              """,
            """
              <!DOCTYPE module PUBLIC
                   "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
                   "https://checkstyle.org/dtds/configuration_1_3.dtd">
              <project>
              </project>
              """
          )
        );
    }

The result:

    expected: 
      "<!DOCTYPE module PUBLIC
           "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
           "https://checkstyle.org/dtds/configuration_1_3.dtd">
      <project>
      </project>"
    but was: 
      "<!DOCTYPE module PUBLIC
      "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
      "https://checkstyle.org/dtds/configuration_1_3.dtd">
      <project>
      </project>

as you see, the continuation line into the doctype has not be indented.

timtebeek commented 1 month ago

Thanks for the report @ofaucheux ! Always appreciate a runnable example along with an already clear bug report. Did you already explore what the reason might be? Without having gone in yet it seems we might visit the doctype differently from other elements.

ofaucheux commented 1 month ago

You're welcome, @timtebeek , and no, I didn't investigate the cause of the problem and will not be in the position to do it.