Templates: Default to expression body for new Kotlin functions

Answered

When overriding a method, is there a way to generated a kotlin-function as expression-body per default?

Right now

 

override fun xx() {

        TODO()

}

 

is created, I would like to have

 

override fun xx() =

    TODO()

 

by default.

 

1
4 comments

Please use https://discuss.kotlinlang.org/ for any questions related to Kotlin.

0
Avatar
Permanently deleted user

The question has nothing to do with language discussion. The author asks about IDEA feature when an overridden method shows up with some predefined body. Maybe some elaborations will help address this question. So, the author talks about the Idea setting "Settings -> Editor -> File and Code Templates -> New Kotlin Function Body". By default it fills a newly created method with the next value:

TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

This predefined body may be changed in this editor:

 

So, after overriding the method will look like this:

 

However, the thing is that Kotlin allows to define a method in a short form ("expression-body"), so the method could also be defined as follows:

 

Although it's possible to change this default value in the template editor, the main problem is that there is no possibility to tell the editor that I want to omit those curly braces which the method's body is enclosed in. Therefore, it's not possible to set up a method's body filling in the "expression-body" style.

 

So, is there any way to add this functionality manually (perhaps, by editing some IDEA config files) or will it be added later?

IDEA parameters:

  • IntelliJ IDEA 2019.3 (Community Edition)
  • Build #IC-193.5233.102, built on November 28, 2019
  • Runtime version: 11.0.4+10-b520.11 amd64
  • VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
  • Linux 5.3.0-24-generic
  • GC: ParNew, ConcurrentMarkSweep
  • Memory: 1925M
  • Cores: 8
  • Registry:
  • Non-Bundled Plugins:
0

Request is welcome at https://youtrack.jetbrains.com/newIssue?project=KT. It still belongs to Kotlin support and IDE plug-in is also responsibility of the Kotlin team, so my comment regarding the proper place to report/discuss such issues is still valid.

0
Avatar
Permanently deleted user

Ok, thanks

0

Please sign in to leave a comment.