https://kotlinlang.org logo
Docs
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
ktlint
  • c

    Colton Idle

    04/24/2020, 1:19 AM
    How do people set up ktlint in their Android development team workflows? My issue is that we now have ktlint added to my teams Android project, but everyone on my team loves to press cmd + shift + L (reformat code) and everyone uses like a slightly different version of intellij/Android Studio and so cmd shift L is always kind of messing things up. What's the best way to setup ktlint so that we always stay in sync? I know I've mentioned a few times above that I'm new at all of this code style/formatting (it's my first real team project) and so I'm lost to what the conventions are as to how to set this up.
    s
    s
    t
    • 4
    • 37
  • c

    Colton Idle

    04/29/2020, 6:42 AM
    According to https://github.com/pinterest/ktlint/issues/701 it sounds like intellij idea integration is imperfect.
    I am proposing to deprecate current IntellijIDEA integration. Problem with it:
    - writes IDEA specific configuration xml files that could not be used by other editors
    - is detached from used rules and not configurable. Only generic style is written to configuration file.
    The project readme makes it seem like it is perfect?
    . with IntelliJ IDEA
    While this is not strictly necessary it makes Intellij IDEA's built-in formatter produce 100% ktlint-compatible code
    Worth opening up a bug about the README, or am I misinterpreting something.
    t
    • 2
    • 3
  • j

    Justin Tuchek

    05/12/2020, 10:48 PM
    In progress of moving to ktlint but trying to ensure open PRs are based off latest master that contains new formatting hooks - is there a quick way to determine if ktlint has been run over a set of files? Doing a single reformat but typically have ~30 PRs merged per day so some chance for unformatted ones to slip through (require keep branches up to date is a different challenge) In my head an idea looks a bit like
    ./ktlint check <git-commit-range>
    and returns back an exit code of 0/1 based on the files in the diff. Maybe somebody has a better way for handling this? Or a solution already exist I’m unaware 🤔 Incremental would be great to minimize build impact, running with --format takes ~25 sec. If it was fast enough would consider just leaving it in to catch environments that haven’t set up hooks yet and post a comment on the PR
    s
    c
    c
    • 4
    • 11
  • b

    BElluu

    06/01/2020, 2:26 PM
    Hello. I am using the
    gradle ktlintCheck
    command in Gitlab-CI, which returns some irregularities to me. I fix them and restart CI - again I get linter errors. Can something be done to get linter to display all the irregularities the first time? It is a bit tiring when linter returns warnings in batches.
    g
    • 2
    • 2
  • s

    Steff Nicolas

    06/08/2020, 10:10 PM
    Hi there. I am wondering if ktlint should / will support kotlin 1.4.
    g
    s
    • 3
    • 2
  • p

    Paul Woitaschek

    06/09/2020, 10:40 AM
    I'm fighting against ktlint and android studio right now (AS 4.0.0, ktlint 0.37.1) Android studio always formats the code like this:
    import java.time.LocalDate
    import java.time.temporal.ChronoUnit
    
    fun test() {
      val lastActive = LocalDate.now()
      if (lastActive != null && ChronoUnit.DAYS.between(
          lastActive,
          LocalDate.now()
        ) > 30
      ) {
        println("hello")
      }
    }
    And then ktlint complains because it wants to have two less spaces before the argument
    ChronoUnit.DAYS.between
    Can someone confirm this?
    r
    • 2
    • 2
  • a

    akshay

    07/01/2020, 6:50 AM
    Is there anything in ktlint which checks if a developer has mention the variable names in comments?
    i.e. create_foo(/* fooParam= */ 1, ...)
    Where ktlint should only throw an error for constants.
    b
    • 2
    • 5
  • b

    bbaldino

    07/08/2020, 4:41 PM
    Would it be possible to write a rule which would fail if a certain method was used? We're trying to look at prevent use of
    ExecutorService.submit
    (and friends) in favor of our own
    safeSubmit
    since
    submit
    swallows all exceptions by default.
    b
    t
    • 3
    • 10
  • r

    rsetkus

    07/14/2020, 3:11 PM
    Hi all. I know that Klint is for style & formatting but since you can access PSI elements, I am wondering is it possible to write a rule which would fail if not certain class is extended. Would it be possible to do that in Ktlint?
    s
    • 2
    • 3
  • t

    tapchicoma

    07/17/2020, 3:03 PM
    New ktlint-gradle plugin release `9.3.0`: https://github.com/JLLeitschuh/ktlint-gradle/blob/v9.3.0/CHANGELOG.md#930---20200717 Most valuable - new Gradle configuration cache support
    🎉 4
    c
    • 2
    • 4
  • k

    khalil

    07/31/2020, 11:50 AM
    hey folks! have been a long since I didnt ask and help anyone here (and I apologize for that) but I have a question, if anyone can help me: There is a way to run a pre commit hook with ktlintFormat instead ktlint? or both? I tried to modificate the original pre commit hook installed by CLI, changing
    ktlint
    to
    ktlintFormat
    but doesnt work and I got this error
    xargs: ktlintFormat: No such file or directory
    c
    t
    • 3
    • 4
  • m

    mp

    08/03/2020, 10:15 AM
    Hi there I have question regarding rules that ktlint applyToIDEA actually change in IntelliJ can I get them as editorconfig file(this is most efficient way to keep same guide for a team)?
    b
    r
    • 3
    • 6
  • k

    khalil

    08/10/2020, 5:46 PM
    There is a way to inspect what is difference between using ktlint with
    --android
    and without on a Android kotlin code
    b
    r
    • 3
    • 3
  • j

    james current

    08/31/2020, 9:43 PM
    Is there a list of all the rule names? I’ve been searching and can’t find one. I’m looking for the rule names in the
    no-wildcard-imports
    format that I can use to disable certain rules.
    s
    • 2
    • 9
  • v

    victorlai

    09/10/2020, 10:15 PM
    Hey there! I had a question about how to share custom rules. For example, if I published a project with a custom RuleSetProvider to maven, should I "just" be able to add a dependency to that project, and then add my rule to the
    src/main/resources/META-INF/services/com.pinterest.ktlint.core.RuleSetProvider
    file? Would appreciate if there were any recipes I could be pointed to! 🙂
    s
    t
    • 3
    • 3
  • n

    nbadal

    09/13/2020, 10:35 PM
    Hey folks! Just released an alpha version of an IntelliJ / Android Studio plugin for ktlint: https://plugins.jetbrains.com/plugin/15057 https://github.com/nbadal/ktlint-intellij-plugin At first, it just adds annotations based on the standard + experimental rule sets, but I’ve got a list of TODO’s in the repo to tackle to get it up to parity with the command line tool as soon as possible 😄
    🆒 4
    s
    • 2
    • 1
  • r

    Rob Elliot

    09/14/2020, 3:45 PM
    Is there a way to require new lines & trailing commas for any parameter list with >= 3 params?
    s
    • 2
    • 2
  • b

    brandonmcansh

    09/18/2020, 5:11 PM
    With ktlint and spotless when would be the best time to hook in and run spotlessApply?
    s
    t
    j
    • 4
    • 7
  • r

    Rob Elliot

    09/22/2020, 9:14 AM
    Cross posting this from the gradle slack because it’s sepcifically the ktlint plugin that’s giving me the issue. I’ve been using this gradle task to try and make my build runnable with no network connection:
    fun Configuration.isDeprecated() = this is DeprecatableConfiguration && resolutionAlternatives != null
    
    tasks.register("downloadDependencies") {
      doLast {
        configurations
          .filter { it.isCanBeResolved && !it.isDeprecated() }
          .forEach { it.resolve() }
      }
    }
    Idea is I run that task with a network connection, then go offline to run the actual build to prove my build will now run without a network connection. However, using the 
    org.jlleitschuh.gradle.ktlint
     plugin I’m still getting a failure running the offline build on a clean box when it tries to download 
    ktlint:0.36.0
    (I’m setting the version explicitly to
    0.39.0
    ). Does the plugin contribute its dependencies to a configuration that I could resolve up front?
    t
    • 2
    • 3
  • d

    danieeh

    09/23/2020, 4:31 PM
    Hello!! I would like to use the snapshot so ktlint can work with new
    fun interfaces
    like I found here. So I added the maven repository
    maven { url "<https://oss.sonatype.org/content/repositories/snapshots>" }
    and changed the version to:
    ktlint {
        version = "1.4-M2-kotlin-dev-SNAPSHOT"
    }
    is the version correct? thanks in advance 🙂
    s
    • 2
    • 4
  • e

    elye

    09/24/2020, 3:20 PM
    Hi, I tried
    ./ktlint -F app/src/**/*.kt
    from my Android project root directory. All works and it corrected the file format. However it is not following AndroidStudio default rule. So I tried
    ./ktlint --android applyToIDEA -F app/src/**/*.kt
    , but it crashes as below.
    Exception in thread "main" picocli.CommandLine$UnmatchedArgumentException: Unknown options: -F, app/src/androidTest/java/com/example/pdfreader/ExampleInstrumentedTest.kt, app/src/main/java/com/example/pdfreader/FileDownloader.kt, app/src/main/java/com/example/pdfreader/MainActivity.kt, app/src/main/java/com/example/pdfreader/PageAdaptor.kt, app/src/main/java/com/example/pdfreader/PageHolder.kt, app/src/main/java/com/example/pdfreader/PdfReader.kt, app/src/test/java/com/example/pdfreader/ExampleUnitTest.kt
    	at picocli.CommandLine$Interpreter.parse(CommandLine.java:7283)
    	at picocli.CommandLine$Interpreter.processArguments(CommandLine.java:7357)
    	at picocli.CommandLine$Interpreter.parse(CommandLine.java:7257)
    	at picocli.CommandLine$Interpreter.parse(CommandLine.java:7147)
    	at picocli.CommandLine.parseArgs(CommandLine.java:836)
    	at com.pinterest.ktlint.Main.main(Main.kt:55)
    What did I do wrong? I tried remove the
    -F
    , it is not working as well? I think I might have miss something…. any help? 🙏
    r
    r
    t
    • 4
    • 7
  • c

    Colton Idle

    09/28/2020, 8:55 PM
    Updated from org.jlleitschuh.gradle:ktlint-gradle:9.2.1 to 9.4.0 and now I'm getting hundreds of these kinds of errors
    filea.kt:266:21: Unexpected indentation (expected 16, actual 20)
    fileb.kt:267:17: Unexpected indentation (expected 12, actual 16)
    filec.kt:281:21: Unexpected indentation (expected 16, actual 20)
    filed.kt:282:17: Unexpected indentation (expected 12, actual 16)
    What's the easiest way to resolve all of these? I'm assuming it either Opt out or Auto format via
    ./gradlew ktlintFormat
    or intellij? Note: I'm using editorConfig (Not sure if that changes things) I tried auto formatting view ktlint and via intellij to no avail. Am I missing something?
    t
    • 2
    • 4
  • j

    Joao Birk

    09/29/2020, 6:04 PM
    Hi there! I update to 0.39.0 and while running
    ktlint
    it tells me that the following
    import
    is not being used therefore I should remove it:
    import org.mockito.Mockito.`when` as _when --- THIS LINE IS THE PROBLEM
    
    @Before
    @CallSuper
    override fun setup() {
        super.setup()
    
        _when(...).thenReturn(...)
    }
    is this a known issue?
    🙌 1
    e
    s
    • 3
    • 3
  • s

    sreekumar_droid

    10/07/2020, 2:48 AM
    we have ktlintformat that we have it in each module. it is super annoying to run the Gradle command individually in each module every time you create PR. ./gradlew ktlintformat. : I wanna get off this 😎
    s
    e
    +2
    • 5
    • 10
  • d

    daniel

    10/08/2020, 12:32 PM
    Hi.. is it somehow possible to configure ktlint with
    kotlin_imports_layout
    to allow idea and ascii import ordering (so not the whole repo needs to change it’s order but we can still use the idea built in import order)?
    r
    t
    • 3
    • 4
  • g

    Giovani Guerra

    10/09/2020, 12:28 PM
    Does ktlint has a baseline file like detekt? I want to enable ktlint on an existing project and check only new problems.
    r
    j
    • 3
    • 2
  • m

    Marek Kubiczek

    10/12/2020, 9:17 AM
    Is it possible to configure ktlint with CI so it runs only for changed files?
    t
    • 2
    • 3
  • d

    dylangreenuk

    10/12/2020, 5:49 PM
    ran it from cmd (simple ktlint in the offending folder), got a related warning:
    TextInputLayoutMatchers.kt:3:1: Imports must be ordered in lexicographic order without any empty lines in-between with "java", "javax", "kotlin" and aliases in the end
    funny, spotless didnt report the numerous other warnings eg
    unexpected indentation (28) (should be 24)
    Missing newline before ")"
    etc
    r
    • 2
    • 3
  • s

    Slackbot

    10/13/2020, 6:07 PM
    This message was deleted.
    r
    • 2
    • 1
  • x

    Xabier Gorostidi

    10/25/2020, 1:29 PM
    Hi there, I'm struggling a lot with ktlint when trying to format my code. If I run an Android Studio code reformat of a long if statement, it formats the if as Kotlin Official Code Style, however ktlint complains about an unexpected indentation:
    if (records.containsKey(AdvRecord.TYPE_UUID128) && Arrays.equals(
            Config.MYOWN_GATT_SERVICE_RAW,  // <-- Unexpected indentation (expected 12, actual 16)
            AdvRecord.getServiceUUID128(records[AdvRecord.TYPE_UUID128]!!) // <-- Same as above
        ) // <-- Unexpected indentation (expected 8, actual 12)
    ) {
        ...
    }
    However, if I change manually to the expected result,
    ktlintFormat
    complains about the same lines to be changed into the previous code indentation again. I don't understand anything. What I'm trying to achieve is Android Studio IDE code reformat fits with ktlint criteria, and somehow they're unsynced now. I'm using the latest gradle plugin:
    id "org.jlleitschuh.gradle.ktlint" version "9.4.1"
    I have even tried to run
    ktlint --android applyToIDEAProject
    to apply project specific code style, but the problem persist. Any idea how to solve this guys?
    t
    r
    • 3
    • 3
Powered by Linen
Title
x

Xabier Gorostidi

10/25/2020, 1:29 PM
Hi there, I'm struggling a lot with ktlint when trying to format my code. If I run an Android Studio code reformat of a long if statement, it formats the if as Kotlin Official Code Style, however ktlint complains about an unexpected indentation:
if (records.containsKey(AdvRecord.TYPE_UUID128) && Arrays.equals(
        Config.MYOWN_GATT_SERVICE_RAW,  // <-- Unexpected indentation (expected 12, actual 16)
        AdvRecord.getServiceUUID128(records[AdvRecord.TYPE_UUID128]!!) // <-- Same as above
    ) // <-- Unexpected indentation (expected 8, actual 12)
) {
    ...
}
However, if I change manually to the expected result,
ktlintFormat
complains about the same lines to be changed into the previous code indentation again. I don't understand anything. What I'm trying to achieve is Android Studio IDE code reformat fits with ktlint criteria, and somehow they're unsynced now. I'm using the latest gradle plugin:
id "org.jlleitschuh.gradle.ktlint" version "9.4.1"
I have even tried to run
ktlint --android applyToIDEAProject
to apply project specific code style, but the problem persist. Any idea how to solve this guys?
t

tapchicoma

10/26/2020, 2:11 PM
Have you tried to move
Arrays.equals(
to next line?
r

romtsn

10/26/2020, 6:55 PM
that’s a known bug, I think is already fixed on master, you could try the latest snapshot. If you wanna temporary suppress it, you could also try to wrap this snippet into
/* ktlint-disable parameter-list-wrapping */
... your snippet ...
/* ktlint-enable parameter-list-wrapping */
x

Xabier Gorostidi

10/27/2020, 11:28 AM
@tapchicoma I tried that, but it still fails. Related to @romtsn suppression suggestion, if
Array.equals
remains on the same line, it works. However, moving that statement to the next line, the suppression doesn't affect.
View count: 165