https://kotlinlang.org logo
Join Slack
Powered by
# ktlint
  • a

    ansman

    11/12/2024, 2:12 PM
    I'm updating ktlint to the latest version and it wants to format like this:
    Copy code
    foo
      .bar {
        ...
      }.baz {
        ...
      }
    whereas I want to format like this (so that you can remove an operator without affecting the closing
    }
    ):
    Copy code
    foo
      .bar {
        ...
      }
      .baz {
        ...
      }
    I can disable the "Chain method continuation" rule but then you get no enforcement at all. Has anyone solved the same issue?
    p
    b
    • 3
    • 4
  • p

    Piotr Krzemiński

    11/29/2024, 11:26 AM
    if you were hit by kotlinter/ktlint's incompatibility with Kotlin 2.1.0, here's a corresponding issue: [pinterest/ktlint#2882] Kotlin 2.1 Compatibility Issue
  • j

    James

    12/05/2024, 8:22 AM
    Is there a way to force the
    .
    in a chain to be on a newline instead of after
    )
    or
    }
    ? I'm not sure I see a way to do this in Chain method continuation
    👍 1
    p
    • 2
    • 5
  • p

    Paul Dingemans

    12/05/2024, 8:31 AM
    Ktlint 1.5, including support for Kotlin 2.1 has been released. https://github.com/pinterest/ktlint/releases/tag/1.5.0
    🙌 4
    🙌🏼 1
    d
    • 2
    • 5
  • w

    wakingrufus

    12/08/2024, 4:23 PM
    Are there docs on which kotlin versions each version of ktlint is compatible with?
    p
    • 2
    • 6
  • a

    atticus

    12/20/2024, 11:41 AM
    Hi! Could anyone help me? I have the following code:
    Copy code
    class Test(
        val test1: String,
        val test2: String, // comment
        val test3: String
    )
    And I received the error: 'A comment in a 'value_parameter_list' is only allowed when placed on a separate line (cannot be auto-corrected).' I want to write the code like this. How can I disable this rule?
    p
    • 2
    • 4
  • m

    Mez Pahlan

    12/20/2024, 6:34 PM
    Hello everyone, lovely to meet you 👋 Can I get your thoughts on this please. I have the following class - note the opening squiggly bracket is outside of the character limit by 1:
    Copy code
    // Assume that the last allowed character is at the X character on the right                                                               X
    internal open class OkHttpClientAdapter(private val url: String, private val httpClient: OkHttpClient = OkHttpClient()) : HttpClientAdapter {
      ... // Some code of no interest.
    }
    Which, when formatted, turns into this:
    Copy code
    // Assume that the last allowed character is at the X character on the right                                                               X
    internal open class OkHttpClientAdapter(private val url: String, private val httpClient: OkHttpClient = OkHttpClient()) :
      HttpClientAdapter {
      ... // Some code of no interest.
    }
    I'm using
    ktlint_code_style = intellij_idea
    and would like something that does the following when format:
    Copy code
    // Assume that the last allowed character is at the X character on the right                                                               X
    internal open class OkHttpClientAdapter(
      private val url: String,
      private val httpClient: OkHttpClient = OkHttpClient(),
    ) : HttpClientAdapter {
      ... // Some code of no interest.
    }
    I can achieve this (sort of) by setting
    ktlint_standard_class-signature = disabled
    along with the code style, but unless I go round and fix all instances of the first formatting I can't benefit from an autofix in any way. It's fine once it is formatted but Ktlint doesn't automatically format in this way. IntelliJ has the ability to do this with the
    Put parameters on separate lines
    autofix and I was wondering if Ktlint could do something similar if it detects the max character count has been exceeded? In plain words: • For a class signature. • Whose character limit has been exceeded. • Put all parameters on separate lines. • With the closing class constructor bracket on the next line. • And ensure the supertype is on the same line as the closing class bracket. Is there some way I can achieve this via Ktlint? Thanks again for your time 🐱 .
  • p

    Paul Dingemans

    12/20/2024, 8:12 PM
    The class signature can do exactly what you want. See configuration option in docs: https://pinterest.github.io/ktlint/latest/rules/standard/#class-signature
    m
    • 2
    • 9
  • e

    eygraber

    01/02/2025, 5:57 PM
    @Paul Dingemans If the IDE settings for trailing commas becomes more configurable would ktlint as well? If so, should I file an issue to track that?
    p
    • 2
    • 1
  • d

    droidrcc

    02/14/2025, 5:50 PM
    Hi! I'm using ktlint 1.5.0 and was wondering why it doesn't report the below bad formatting:
    Copy code
    if (something == ABC
    ) {
      ...
    } else {
      ...
    }
    ) {
    should be on the same line of the if statement...
    Copy code
    if (something == ABC) {
    Do you have any insight?
    p
    • 2
    • 2
  • a

    andrew

    02/20/2025, 1:58 AM
    Is it normal for Ktlint to be slow to respond to
    .editorconfig
    changes when using spotless?
  • b

    Blaž Vantur

    02/20/2025, 9:00 AM
    Is it possible to ignore
    max_line_length
    for specific methods that are Annotated with
    @Test
    ?
    We are using back ticks for naming and we like having our names descriptive. But because of that the line length always exceed our defined limit for
    max_line_length
    . Is there a way to ignore it in some other global way without a need of adding suppress annotation directly where it happens?
    e
    h
    p
    • 4
    • 5
  • a

    Aditya Bhaskar

    03/03/2025, 12:45 PM
    (This is not strictly
    ktlint
    related) Does anyone know of any migration notes or discussions when updating to
    5.0.x
    of Kotlinter-gradle? I just made the update and the
    lintKotlin
    task is throwing 100s of lint errors. I’ve confirmed it’s not a ktlint issue by changing the
    ktlintVersion
    to previously working version. The release notes don’t suggest anything that could impact this. I have a feeling that the
    .editorconfig
    is not being used. As an example, one frequent error is due to the
    function-signature
    rule saying “First line of body expression fits on same line as function signature”. But that seems to ignore
    max-line-length
    set to 100 in editorconfig.
    • 1
    • 2
  • k

    Klitos Kyriacou

    03/03/2025, 6:25 PM
    I'm very confused about the indentation rules related to the superclass constructor call in subclass definitions. It seems that the indentation superclass constructor parameters depends on whether the subclass has any parameters. See code in thread.
    p
    • 2
    • 2
  • m

    Malte

    03/27/2025, 7:49 AM
    Hi! can ktlint detect usages for the
    else
    branch for
    when
    that have an enum as subject? i.e. i want to make ktlint recognize
    when(someEnum) {else -> … }
    - and raise an issue of course
    p
    k
    • 3
    • 7
  • z

    zt

    04/10/2025, 11:07 PM
    Some ktlint rule is bothering me by pointlessly wrapping the extended interfaces like this
    Copy code
    public class Hook(public override val replyUserData: Long) : MpvEvent, Reply
    into this
    Copy code
    public class Hook(public override val replyUserData: Long) :
        MpvEvent,
        Reply
    I cannot figure out what rule is doing this. There really should be an easier way to just quickly see a list of violations in the IDE
    r
    • 2
    • 1
  • k

    Klitos Kyriacou

    05/02/2025, 2:24 PM
    For
    [standard:import-ordering]
    if I have 100 imports and the 50th one is out of order, it gives the message "Imports must be ordered in lexicographic order" at the start of the first line of import. It would be very useful if it put the message on the first line that was out of order.
  • p

    Paul Dingemans

    05/20/2025, 5:23 AM
    Ktlint 1.6.0 is released. The release email which you might have received does not contain the full list of changes. Please see release page below for full list. https://github.com/pinterest/ktlint/releases/tag/1.6.0
    kodee happy 2
    🎉 4
  • m

    Marcel Overdijk

    05/22/2025, 9:13 AM
    Hi, I'm new here and new to ktlint. I like the rules and don't mind it's opinionated, however when using (data) classes with fields and annotations, these classes become unreadable...
    Copy code
    @Schema(
        name = "Country",
        description = "Representation of a country.",
    )
    data class CountryDto(
        @field:Schema(
            description = "The unique identifier for the country.",
        )
        val id: String,
        @field:Schema(
            description = "The unique ISO 3166-1 alpha-2 code of the country.",
            minLength = 2,
            maxLength = 2,
            pattern = "^[A-Z]{2}$",
        )
        val alpha2Code: String,
        @field:Schema(
            description = "The unique ISO 3166-1 alpha-3 code of the country.",
            minLength = 3,
            maxLength = 3,
            pattern = "^[A-Z]{3}$",
        )
        val alpha3Code: String,
        @field:Schema(
            description = "The name of the country.",
        )
        val name: String,
        @field:Schema(
            types = ["string", "null"],
            description = "The demonym for citizens of the country.",
            required = true,
        )
        val demonym: String?,
        @field:Schema(
            description = "The identifier for the continent where the country is located.",
        )
        val continentId: String,
    )
    a blank link between the fields would help a lot, but this does not seem to be possible... is that correct, or am I missing something?
    p
    d
    • 3
    • 9
  • b

    Benoît Liessens

    06/25/2025, 1:35 PM
    Are there any plans for KtLint to support Context Parameters with Kotlin 2.2.0?
  • p

    Paul Dingemans

    06/25/2025, 3:30 PM
    Yes, after upgrade to Kotlin 2.2.0 this will be automatically supported.
    thank you color 2
  • h

    Hadi

    07/15/2025, 5:33 PM
    Hi 👋 I want to use ktlint to restrict some lines of Kotlin code to be unique. for instance, we have a list of DI modules in
    modules
    dsl of Koin, where function calls duplicated while devs resolving merge conflicts. so we got no compile time or runtime error to notice this! another example is
    include
    calls in settings.gradle.kt file, where there’s no error when there’s duplicated calls. any idea how to solve that?
    p
    • 2
    • 2
  • h

    Hadi

    07/15/2025, 5:35 PM
    btw, I am open to use any tool to solve this problem. any suggestions?
    🧵 1
  • p

    Paul Dingemans

    07/16/2025, 6:16 AM
    Ktlint
    1.7.0
    was released earlier this week. This version is based on Kotlin 2.2.0 and as of that supports context parameters. Unfortunately it misformats the context receivers in some cases. This will be fixed in the bugfix release which is coming in 1 to 2 weeks. Full release notes: https://github.com/pinterest/ktlint/releases/tag/1.7.0
    kodee happy 3
    🎉 5
    👍 2
  • l

    Lex Luthor

    07/18/2025, 1:19 AM
    After upgrading to
    1.7.0
    I get the following error:
    Copy code
    > Task :runKtlintFormatOverKotlinScripts FAILED
    Rule 'internal:ktlint-suppression' throws exception in file 'build.gradle.kts' at position (0:0)
       Rule maintainer: KtLint
       Issue tracker  : <https://github.com/pinterest/ktlint/issues>
       Repository     : <https://github.com/pinterest/ktlint>
    Has anyone else encountered a similar issue?
    • 1
    • 2
  • p

    PoisonedYouth

    07/18/2025, 5:24 AM
    After upgrading from
    1.6.0
    to
    1.7.0
    (without doing any other change). There are a lot of changes when formatting the code like:
    Copy code
    OLD:
     val updatedDemands = data.demands
          .filterNot { userData ->
              demands.any { <http://it.cat|it.cat> == <http://userData.cat|userData.cat> }
          } + demands.filterNot { it.status == null }
    
    NEW:
     val updatedDemands = data.demands
          .filterNot { userData ->
              demands.any { <http://it.cat|it.cat> == <http://userData.cat|userData.cat> }
          } +
          demands.filterNot { it.status == null }
    Which rule is affecting this? Can I disable it?
    p
    • 2
    • 7
  • p

    Paul Dingemans

    07/21/2025, 6:14 PM
    Ktlint
    1.7.1
    is released. This fixes the problem in
    ktlint-gradle
    plugin, and wrapping of context parameters. https://github.com/pinterest/ktlint/releases/tag/1.7.1
    🎉 6
    thank you color 3
  • m

    Matteo Mirk

    07/31/2025, 12:39 PM
    Hi! I've updated a project from
    1.2.1
    to
    1.7.1
    , and the gradle plugin to the latest version. I enabled
    max_line_length
    which was previously disabled for .kt files. Unfortunately Ktlint has started wrapping some expressions in a way I really don't like, in order to fit within the line length. This and similar expression are being formatted like this:
    Copy code
    //BEFORE
    val actual = runBlocking { alternativeDatesListingExperiment.sanitizeResponse(subQueryResult, subQuerySanitizer, mockSubListingDetails, getNonPropertyCardStrippingContext(egGraphQLContext, searchParameters = searchParameters)) }
    
    //AFTER
    val actual =
        runBlocking {
            alternativeDatesListingExperiment.sanitizeResponse(
                subQueryResult,
                subQuerySanitizer,
                mockSubListingDetails,
                getNonPropertyCardStrippingContext(egGraphQLContext, searchParameters = searchParameters)
            )
        }
    No matter how I tweak the wrapping rules, it stubbornly wraps the expression on the next line after the assignment. What I'd like is this:
    Copy code
    val actual = runBlocking {
        ...
    }
    I've read the rules documentation thoroughly more than once, and I can't find a reason why it's wrapping the expression like this. Is there something I'm missing or is it impossible to configure Ktlint in such a way? Thanks. Attaching my editor config for reference.
    .editorconfig
    b
    p
    • 3
    • 8
  • o

    Oleg Nenashev

    08/12/2025, 12:23 PM
    set the channel topic: Anything about pinterest/ktlint
  • h

    Hugo Costa

    08/18/2025, 7:59 AM
    Hello, I've just created a feature request, would be great if you could take a look - Add Rule Version Filtering Based on @SinceKtlint Annotations · Issue #3099 · pinterest/ktlint