Skip to content

Regression with method chaining and text block #918

Closed

Description

@murdos

Prettier-Java 2.9.7

# Options (if any):
--print-width 140

Input:

  public String foo() {
    return "SOME TEXT"
      .replace("SOME", "OTHER")
      .replace("TEXT", "OTHER")
      .replace("OTHER", "SOME")
      .replace("OTHER", "TEXT")
      .replace(
        "SOME",
        """
        FOO"""
      );
  }

Output:

    return "SOME TEXT".replace("SOME", "OTHER").replace("TEXT", "OTHER").replace("OTHER", "SOME").replace("OTHER", "TEXT").replace(
      "SOME",
      """
      FOO"""
    );

Expected behavior:

    return "SOME TEXT"
      .replace("SOME", "OTHER")
      .replace("TEXT", "OTHER")
      .replace("OTHER", "SOME")
      .replace("OTHER", "TEXT")
      .replace(
        "SOME",
        """
        FOO"""
      );
  }

What is really bothering here, is that if you change the last parameter (FOO) from text block to a simple String, the method chaining is back to one line per method call:

    return "SOME TEXT"
      .replace("SOME", "OTHER")
      .replace("TEXT", "OTHER")
      .replace("OTHER", "SOME")
      .replace("OTHER", "TEXT")
      .replace("SOME", "FOO");

So changing the way to write the last parameter change the whole method chaining formatting, which is awkward.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions