Kotlin import declaration is inserted incorrectly
Answered
E.g., I have:
import java.lang.StringBuffer
class Stuff {
val cursor: StringBuffer
}
and I add a `java.io.File` component:
class Stuff {
val cursor: StringBuffer
val handle: File
}
then IntelliJ shows `File` in red and the context menu has the option to import the required declaration, but it inserts it like so:
import java.io.Fileimport java.lang.StringBuffer
class Stuff {
val cursor: StringBuffer
val handle: File
}
so I have to go back and reformat the import list. How can I stop that and make IntelliJ insert the required new line as well?
Please sign in to leave a comment.
Hello,
Do you face this issue with Kotlin files only? Do you use default code style settings?
How can I find out if I'm using the default code style settings? I think I am, although I do use tab-indentation rather than spaces, so maybe not.
Curiously, with Java, an extra blank line is inserted:
In that sample, I just added `private final File thing = null;` and IntelliJ inserted `import java.io.File;` with an extra blank line. Just to be clear, before the insertion, there was no blank line between `import com.myTiM.V2.sql.SQLHandler;` and `import java.io.IOException;`.
Looks like the
*.kt
files in the list of "Do not format"
in your scheme -"Settings/Preferences | Editor | Code Style - Formatter"
. Delete this pattern and the problem should be fixed.In Java there are "Import Layout" rules (
"Settings/Preferences | Editor | Code Style | Java - Imports"
) which places blank line between all imports and "import java.*".You are right; the *.kt pattern is there. This is a most regrettable solution. I found IntelliJ's reformatting of code extremely annoying, and was delighted to discover the possibility of disabling it. I think I'd rather accept having to reformat the automatically-generated import declarations, than fight IntelliJ's insistence on reformatting my code.
The solution for Java is acceptable; thank you.
Have you tried to set your rules for formatter? Or a lot of changes should be done?
Do you mean have I tried changing the rules so that the results of formatting are acceptable? No; I could try that.
You've answered the question, so thank you and we can close this ticket.
You are welcome!