Generate KDoc for methods in Android Studio [close

2020-03-01 04:15发布

问题:


Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed last year.

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?

回答1:

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.



回答2:

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.