Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ does not sort Kotlin imports

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)

like image 946
mattbdean Avatar asked Dec 28 '14 01:12

mattbdean


People also ask

How do I sort imports in IntelliJ?

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.

How do I fix the wrong lexicographical order in IntelliJ?

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.

How do I fix all imports in IntelliJ?

To optimize imports in a file, you can also press Ctrl+Alt+Shift+L , select Optimize imports, and click Run.


1 Answers

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.

like image 174
Andrew M Avatar answered Sep 20 '22 15:09

Andrew M