When I select Code → Optimize Imports or Code → Reformat Code, the IntelliJ does optimize and sort imports, but even though I am using code style settings from Kotlin code style, the imports are not sorted in lexicographic order (not entirely at least). For example, this is the output produced:

import com.fasterxml.jackson.databind.ObjectMapper
import io.dropwizard.jackson.Jackson
import io.kotlintest.assertSoftly
import io.kotlintest.matchers.types.shouldBeNull
import io.kotlintest.shouldBe
import io.kotlintest.specs.ShouldSpec
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.intellij.lang.annotations.Language
import java.time.Instant // This should not be at the bottom!!!

而这正是ktlint人所期待的:

import com.fasterxml.jackson.databind.ObjectMapper
import io.dropwizard.jackson.Jackson
import io.kotlintest.assertSoftly
import io.kotlintest.matchers.types.shouldBeNull
import io.kotlintest.shouldBe
import io.kotlintest.specs.ShouldSpec
import java.time.Instant // should be here instead
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.intellij.lang.annotations.Language

换句话说,不管发生什么,格式化程序似乎总是将从java.*个包导入的内容放在底部.有没有办法让它符合ktlint的期望(以及什么会真正构成词典顺序)?我有没有遗漏什么隐藏的选项?

I am using IntelliJ IDEA 2019.3.1 (Ultimate Edition) with the Kotlin plugin version 1.3.61-release-IJ2019.3-1. Version of ktlint is 0.36.0

推荐答案

This is a bug/missing functionality in the Kotlin IDEA plugin: https://youtrack.jetbrains.com/issue/KT-10974. Please vote.

Kotlin相关问答推荐

Kotlin协程挂起继续线程

如何在Kotlin中为接受varargs的函数添加带有默认值的新参数?

在Kotlin 有更好的结合方式?

kotlin短路列表判断吗?

使用 Jetpack Compose 使用参数导航

compareBy 如何使用布尔表达式在 kotlin 中工作

Kotlin 可打包类抛出 ClassNotFoundException

Kotlin 中列表或数组的乘积

AIDL 中的 Parcelize 注释:Incompatible types: Object cannot be converted to MyCustomObject

为什么 Kotlin 扩展运算符在传递原始可变参数时需要 toTypedArray()?

在 kotlin 中写入 parcer 可空值

如何从定义它们的类外部调用扩展方法?

如何用 kotlin 打包 List

Kotlin 有 array.indexOf 但我无法弄清楚如何做 array.indexOfBy { lambda }

将ExpectedException与Kotlin一起使用

使用Dagger 2提供函数依赖性

导航架构组件 - 未生成 DestinationFragmentArgs

在 IntelliJ Idea 中未为 Kotlin @ConfigurationProperties 类生成 spring-configuration-metadata.json 文件

Kotlin反射不可用

如何判断数据是否插入到房间数据库中