When writing Java code, IntelliJ automatically sorts imports by name. However, when importin members in Kotlin, they remain unsorted. Selecting Code → Optimize Imports (Ctrl+Alt+O) does nothing.
Here is an example:
import kotlin.platform.platformStatic
import java.text.DateFormaty
import org.hibernate.validator.constraints.NotEmpty as notEmpty
import com.fasterxml.jackson.annotation.JsonProperty as jsonProperty
import javax.validation.constraints.NotNull as notNull
import javax.validation.Valid as valid
What I'm expecting:
import com.fasterxml.jackson.annotation.JsonProperty as jsonProperty
import org.hibernate.validator.constraints.NotEmpty as notEmpty
import kotlin.platform.platformStatic
import java.text.DateFormat
import javax.validation.constraints.NotNull as notNull
import javax.validation.Valid as valid
I'm using IntelliJ 14.0.2 with the Kotlin plugin (version 0.10.195)
In Eclipse, you press CTRL + SHIFT + O “Organize Imports” to import packages automatically. For IntelliJ IDEA, if you press CTRL + ALT + O “Optimize Imports”, it just removes some unused imports, never imports any package.
Open the 'Settings' (or 'Preferences' in mac) window and goto Editor > Code Style > Java. Click on 'Imports' tab. In 'Import Layout' area, you can arrange the import order by selecting the import type and clicking on the arrow (see image below). Once the changes are done apply the new settings.
To optimize imports in a file, you can also press Ctrl+Alt+Shift+L , select Optimize imports, and click Run.
This has been fixed in recent versions of IntelliJ, for example the current version (at time of writing 2017.3.3)
If you only have a perpetual fallback licence for the full version of IntelliJ 14 - you could still use the community edition of a more recent version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With