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

IndexOutofBoundsException in argument-list-wrapping-rule formatting file with many corrections #1081

Closed
shashachu opened this issue Feb 12, 2021 · 0 comments · Fixed by #1083

Comments

@shashachu
Copy link
Contributor

Steps to Reproduce

This test will throw an exception:

ArgumentListWrappingRule().format(
    """
        package com.foo
    
        class MyClass() {
            private fun doSomething() {
                if (d == 0 || e == 0f) {
                    c.ee(hh, d, d, yy)
                } else {
                    foo.blah()
                    val dr = t - u
                    val xx = -gg(dr) * rr(2f * d, dr.hh)
                    foo.bar(
                        dd,
                        g - d
                    )
                    foo.baz(
                        a.b, a.c - d
                    )
                    foo.biz(
                        a.b, a.c - d,
                        a.b, a.c,
                        a.b + d, a.c
                    )
                    foo.baz(
                        a.x - d, a.c
                    )
                    foo.biz(
                        a.x - d, a.c,
                        a.x, a.c,
                        a.x, a.c - d
                    )
                    foo.baz(
                        a.x, a.j + d
                    )
                }
            }
        }
    """.trimIndent()
Caused by: java.lang.IndexOutOfBoundsException: Wrong offset: 839. Should be in range: [0, 773]
	at org.jetbrains.kotlin.com.intellij.openapi.editor.impl.LineSet.findLineIndex(LineSet.java:178)
	at org.jetbrains.kotlin.com.intellij.openapi.editor.impl.DocumentImpl.getLineNumber(DocumentImpl.java:991)
	at com.pinterest.ktlint.core.ast.PackageKt.lineNumber(package.kt:236)
	at com.pinterest.ktlint.ruleset.experimental.ArgumentListWrappingRule.isOnSameLineAsIfKeyword(ArgumentListWrappingRule.kt:225)
	at com.pinterest.ktlint.ruleset.experimental.ArgumentListWrappingRule.visit(ArgumentListWrappingRule.kt:96)
	at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:300)

When you make it much shorter, it does not crash. Perhaps each node's startOffset is not being updated as corrections are made?

Your Environment

  • Version of ktlint used: 0.40.0
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): CLI
  • Version of Gradle used (if applicable): 6.6
  • Operating System and version: Mac OS 10.15.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant