How to add a newline to the PSI tree?

I've tried


element.addAfter(JetPsiFactory.createNewLine(project), anotherElement);

but it doesn’t seem to work. IDEA seems to insist on formatting inserted elements a certain way.

First question: why don't you like the way the code is formatted automatically?

Fair question. I guess I'm just used to the Java style of putting a newline between function declarations, but IDEA won't let me do that.

So it creates code like this:


fun foo() {}
fun bar() {}

Right? It’s a fair point that it doesn’t look very pretty, but the problem is most likely not in your code, but in Kotlin’s formatter. Feel free to fix it and submit a pull request