Code Monkey home page Code Monkey logo

Comments (7)

bmeier-pros avatar bmeier-pros commented on June 2, 2024 2

@Stephan202 - not completely, I saw that issue as well, but these are just bare static final string constants.

For example:

    public static final String JSON_1 =
        """
        [
          "FUNCTION1",
          "FUNCTION2",
          "// ... 98 other records ..."
          "FUNCTION101"
        ]""";

    public static final String JSON_2 =
        """
{
  "errorID": "error id",
  "errorMessage": "message",
  "resourceType": "metadata",
  "requestID": "request"
}""";

I'd say it was something about the JSON curly brackets, but it's not consistent. I'm trying to find a truly minimal reproduction.

from google-java-format.

bmeier-pros avatar bmeier-pros commented on June 2, 2024 2

My preferred formatting for text blocks would be something like:

public static final String STRING = """
    ... Embedded code and scripts possibly with long lines...
    """; // the position of this element defines the rest of the block's indentation

from google-java-format.

bmeier-pros avatar bmeier-pros commented on June 2, 2024 1

Ok, looked at it a bit more.

I suspect the behaviour you're seeing is that there's some logic to start the text block contents at the left margin if indenting the lines would cause the column limit to be exceeded...

This is the issue, generally. The text blocks in question all have lines exceeding 100 characters when the file is consistently indented. There's one block that seems to be an outlier, but this behavior explains the move of every other block. The gutter in the editor is configured at 120 characters, so that threw me for a bit.

IMHO, the left justification of long lines is unexpected, and I would not do it for text blocks. It makes the left margin of files look very strange if they have text blocks with long lines, and there's lots of situations where long lines are somewhat required, like embedded scripts. In the case of these files, about a third of the blocks are left justified, and the rest are justified with the code. Something to discuss, for sure.

from google-java-format.

Stephan202 avatar Stephan202 commented on June 2, 2024

Hey @bmeier-pros! Does your issue match what's described in #1081?

from google-java-format.

cushon avatar cushon commented on June 2, 2024

I suspect the behaviour you're seeing is that there's some logic to start the text block contents at the left margin if indenting the lines would cause the column limit to be exceeded, and to preserve the existing formatting choice if a text block is aligned with the left margin.

In that example, adding a least one space of indent to the second text block will cause the formatter to indent it the same way as the first text block.

The formatting of text blocks hasn't been finalized yet, we might revisit some of those details, in particular whether or not aligning with the left margin is a choice that should be preserved.

String prefix =
(deindent == 0 || lines.stream().anyMatch(x -> x.length() + startColumn > columnLimit))
? ""
: " ".repeat(startColumn - 1);

Example:

$ google-java-format T.java
class T {
  String S =
      """
      hello
      world
      """;
}
$ google-java-format T.java
class T {
  String S =
      """
hello
world
""";
}

from google-java-format.

bmeier-pros avatar bmeier-pros commented on June 2, 2024

Hi @cushon,

That makes some sense, however the original formatting of the two text blocks before applying google-java-format was consistent at 8 spaces indentation for both blocks. This also happened in several other places throughout the file where some blocks were indented and others were justified. It's also consistent - if I modify the indentation of the second block, it gets revised back to left justified. I will try again to make a minimal case :-) I've had a busy couple of days.

from google-java-format.

zaneduffield avatar zaneduffield commented on June 2, 2024

The formatting of text blocks hasn't been finalized yet, we might revisit some of those details, in particular whether or not aligning with the left margin is a choice that should be preserved.

I'm also not a huge fan of pushing the text blocks all the way to the left margin. I would prefer if the line length limit was violated. GJF can't promise that its output will never violate the line length limit anyway (all it takes is an identifier of length 101).

Another option would be to wrap the inner contexts of the text block, like

foo(
  """
  aaaaaaaaaaaaaaaaaaaaaaa\
  a
  """
)

from google-java-format.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.