Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expression function runaway indentation #1421

Open
andrewparmet opened this issue Nov 27, 2022 · 2 comments
Open

Expression function runaway indentation #1421

andrewparmet opened this issue Nov 27, 2022 · 2 comments
Labels

Comments

@andrewparmet
Copy link

Describe the bug

When using withIndent in an expression function, the emitted code does not unindent properly. Automatic wrapping then causes compilation to fail as deeply nested code gets squeezed against the margin.

To Reproduce

Unfortunately I don't have a simple example, but I do have a branch that reproduces the problem: https://github.com/andrewparmet/protokt/tree/demo-broken-indentation

To reproduce, run ./gradlew protokt-core-lite:build. The issue manifests in the generated code file descriptor.kt.

Errors are like: e: /Users/.../protokt/protokt-core-lite/build/generated-sources/main/protokt/com/toasttab/protokt/descriptor.kt: (8132, 176): Expecting a top level declaration

Expected behavior

My overall goal is to generate expression functions whose bodies begin on the next line, like so:

fun foo() =
    "some very long expression " +
        "that stretches on for many lines"

And the source of this bug is in MessageGenerator.kt:

buildCodeBlock {
    add(
        "return \nother is %T &&\n".bindSpaces(),
        msg.className
    )
    withIndent {
        equalsLines(properties).forEach(::add)
        add("other.unknownFields == unknownFields".bindSpaces())
    }
}

If I remove withIndent() then the code generates ok, although it's not pretty.

Thanks!

Additional Context

Note that the code generator has a formatting pass that is disabled; it is found in GeneratedCodeCleanup.kt.

@andrewparmet
Copy link
Author

andrewparmet commented Nov 27, 2022

Here's a small, self-contained example:

class Foo {
    @Test
    fun foo() {
        FileSpec.builder("foo", "Foo.kt").apply {
            (1..10).forEach {
                addFunction(
                    FunSpec.builder("foo$it")
                        .addCode(
                            buildCodeBlock {
                                add("return \n\"foo\"\n")
                                withIndent {
                                    add(".toString()")
                                }
                            }
                        )
                        .build()
                ).build()
            }
        }.let { println(it.build()) }
    }
}

Generates:

public fun foo1() = 
"foo"
  .toString()

  public fun foo2() = 
  "foo"
    .toString()

    public fun foo3() = 
    "foo"
      .toString()

      public fun foo4() = 
      "foo"
        .toString()

        public fun foo5() = 
        "foo"
          .toString()

          public fun foo6() = 
          "foo"
            .toString()

            public fun foo7() = 
            "foo"
              .toString()

              public fun foo8() = 
              "foo"
                .toString()

                public fun foo9() = 
                "foo"
                  .toString()

                  public fun foo10() = 
                  "foo"
                    .toString()

@andrewparmet andrewparmet changed the title Runaway indentation Expression function runaway indentation Nov 27, 2022
@JakeWharton
Copy link
Member

Related to #1300

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants