30

When commenting methods for Java in Android Studio, I can type /** and AS generates javadoc with method parameters and return type for me. But it seems it doesn't work for Kotlin.

Is there some way to teach AS to generate method docs in Kotlin KDoc format automatically?

Edit: yes, this question is about the same thing as the possible duplicate, but my question isn't "why?", I understand that KDoc has different format. My question is if there is a way to do the same for Kotlin in AS? Maybe there is a way to add/edit this template or something like this?

Georgiy Shur
  • 748
  • 1
  • 8
  • 19
  • 1
    Possible duplicate of [Generating Kotlin method/class comments](http://stackoverflow.com/questions/38015307/generating-kotlin-method-class-comments) – yole Nov 13 '16 at 20:45
  • 1
    You can of course write your own plugin to generate those tags for you. Other than that, there is no possibility. – yole Nov 14 '16 at 11:51
  • Try this approach http://stackoverflow.com/a/41589000/2267723 – Maher Abuthraa Jan 11 '17 at 10:58
  • Does KDoc support `{@link}` ? – AutonomousApps Jul 14 '17 at 18:58
  • Instead of `{@link}` you just write `[target]` or `[label][target]` in KDoc. – tango24 Oct 01 '17 at 13:10
  • 3
    I disagree that this question is off topic. New Kotlin developers will certainly be questioning how to generate KDoc comments on their methods, etc. The answer by @Shorxy works great for me. – mpalmer78 Sep 02 '19 at 16:03
  • I agree that it's not off-topic, but it _is_ a duplicate of [Generating Kotlin method/class comments](https://stackoverflow.com/questions/38015307/generating-kotlin-method-class-comments), linked earlier – Ryan M Apr 22 '20 at 23:13
  • 2
    It is totally not off-topic in my opinion. However both questions more or less ask for the same answer, which means that one is a duplicate of the other. Event though the question @Ryan M mentions is older, that question should be marked as duplicate. – Shorxy Jun 02 '20 at 16:15
  • Javadoc tags are not generated by default as this is not the recommended way to do it in Kotlin. Instead you should use `[myParam]` syntax to link to your parameters. See: https://kotlinlang.org/docs/coding-conventions.html#documentation-comments – Slion Jun 01 '21 at 05:19

2 Answers2

28

Since January 2019, there is a plugin on the Jetbrains website called kdoc-generator.

https://plugins.jetbrains.com/plugin/10389-kdoc-generator

Plugin to generate class and method KDoc.

This generator functions exactly like JavaDoc. That means when you create a commentary with "/**" and press enter, the kdoc-generator creates automatically "@param" or "@return" tags for the parameter and return values of the method directly below this created commentary.

When you type /** in your file above a method you wrote and then press enter you get something similar to this:

/**
 *
 * @param str 
 * @param age
 */
fun foo(str: String, age: Int) {

}

To install this Plugin directly in Android Studio:

  1. open File/Settings (Mac: AndroidStudio/Preferences)
  2. choose Plugins
  3. click Browse repositories...
  4. enter "kdoc-generator" in searchbar
  5. klick Install
  6. restart Android Studio Now the kdoc-generator is installed and you can use it directly.

Additionally Jetbrains developed a documentation engine for Kotlin, that you can use to convert your documentation in Kotlin files to some standard formats, e.g. HTML

https://github.com/Kotlin/dokka

Dokka is a documentation engine for Kotlin, performing the same function as javadoc for Java. Just like Kotlin itself, Dokka fully supports mixed-language Java/Kotlin projects. It understands standard Javadoc comments in Java files and KDoc comments in Kotlin files, and can generate documentation in multiple formats including standard Javadoc, HTML and Markdown.

Shorxy
  • 598
  • 5
  • 12
  • 4
    The fact it's on JetBrains website, doesn't mean it was developed by JetBrains. The vendor name reads "siosio" and AndroidStudio gives a warning that it might process my sensitive data. Thank you, but no. – RexSplode Feb 14 '19 at 09:39
  • Oh, thank you for pointing this out. I fixed it right away. – Shorxy Feb 15 '19 at 08:32
3

As this is most likely a bug with IntelliJ, I've taken the liberty of filing a bug report here. You can choose to watch this issue to get notified of any updates.

Andrew Orobator
  • 7,978
  • 3
  • 36
  • 36