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

Multiline primary constructor trips up the indent rule #2251

Closed
vatbub opened this issue Sep 7, 2023 · 2 comments
Closed

Multiline primary constructor trips up the indent rule #2251

vatbub opened this issue Sep 7, 2023 · 2 comments

Comments

@vatbub
Copy link

vatbub commented Sep 7, 2023

Expected Behavior

Consider the following code:

package de.uni_freiburg.inatech.streem.feature_analyzer.view.option_modules

class ClassWithMultilineConstructor
@Deprecated(
    "Constructor must only be called by JavaFX.",
    ReplaceWith("ClassWithMultilineConstructor.newInstance()"),
    DeprecationLevel.ERROR
)
constructor() {
    private val stringMember = "Hello there"

    fun someMethod() {
        println("Hello there")
    }

    companion object {
        fun newInstance(): ClassWithMultilineConstructor {
            println("Method in the companion object called")
            return ClassWithMultilineConstructor::class.java.getDeclaredConstructor().newInstance()
        }
    }
}

The annotation and constructor have to be wrapped this way or else the max line length would be reached.

Observed Behavior

KtLint wants the entire class to be indented by one more tab, which not only looks ugly but also fights IntelliJ:

ClassWithMultilineConstructor.kt:4:1: Unexpected indentation (0) (should be 4) (standard:indent)
ClassWithMultilineConstructor.kt:5:1: Unexpected indentation (4) (should be 8) (standard:indent)
ClassWithMultilineConstructor.kt:6:1: Unexpected indentation (4) (should be 8) (standard:indent)
ClassWithMultilineConstructor.kt:7:1: Unexpected indentation (4) (should be 8) (standard:indent)
ClassWithMultilineConstructor.kt:8:1: Unexpected indentation (0) (should be 4) (standard:indent)
ClassWithMultilineConstructor.kt:9:1: Unexpected indentation (0) (should be 4) (standard:indent)
ClassWithMultilineConstructor.kt:10:1: Unexpected indentation (4) (should be 8) (standard:indent)
ClassWithMultilineConstructor.kt:12:1: Unexpected indentation (4) (should be 8) (standard:indent)
ClassWithMultilineConstructor.kt:13:1: Unexpected indentation (8) (should be 12) (standard:indent)
ClassWithMultilineConstructor.kt:14:1: Unexpected indentation (4) (should be 8) (standard:indent)
ClassWithMultilineConstructor.kt:16:1: Unexpected indentation (4) (should be 8) (standard:indent)
ClassWithMultilineConstructor.kt:17:1: Unexpected indentation (8) (should be 12) (standard:indent)
ClassWithMultilineConstructor.kt:18:1: Unexpected indentation (12) (should be 16) (standard:indent)
ClassWithMultilineConstructor.kt:19:1: Unexpected indentation (12) (should be 16) (standard:indent)
ClassWithMultilineConstructor.kt:20:1: Unexpected indentation (8) (should be 12) (standard:indent)
ClassWithMultilineConstructor.kt:21:1: Unexpected indentation (4) (should be 8) (standard:indent)
ClassWithMultilineConstructor.kt:22:1: Unexpected indentation (0) (should be 4) (standard:indent)

If I remove the @Deprecated annotation and the constructor() keyword (thus having the entire class signature on one line), the issue goes away.

Steps to Reproduce

Run KtLint 1.0.0 on the previous code sample (0.50.0 did not have this issue).

Your Environment

  • Version of ktlint used: 1.0.0
  • Relevant parts of the .editorconfig settings:
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 120
tab_width = 4
ij_continuation_indent_size = 4
  • Name and version (or code for custom task) of integration used: Maven 3.9.3 with the antrun-plugin
  • Operating System and version: Windows 10
@vatbub
Copy link
Author

vatbub commented Sep 7, 2023

It seems like this is related to #2115 .

@paul-dingemans
Copy link
Collaborator

Duplicate of #2138

@paul-dingemans paul-dingemans marked this as a duplicate of #2138 Sep 7, 2023
@paul-dingemans paul-dingemans closed this as not planned Won't fix, can't repro, duplicate, stale Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants