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?

0
7 comments

Hello,

Do you face this issue with Kotlin files only? Do you use default code style settings? 

0

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:

import com.myTiM.V2.sql.SQLHandler;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotNull;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class CatalogueStoreImpl implements CatalogueStore {
private static final Logger logger = LogManager.getLogger(CatalogueStoreImpl.class);
private final Map<String,Object> storedMetadata = new HashMap<>();
private final SQLHandler sql = new SQLHandler();
private final String lastUpdateKey = "lastUpdateTimestamp";

private final File thing = null;

@Override
public int getLastUpdateTimestamp() {

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;`.

0

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.*".

0

Delete this pattern and the problem should be fixed.

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.

0

Have you tried to set your rules for formatter? Or a lot of changes should be done? 

0

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.

0

Please sign in to leave a comment.