Method Documentation plugin for IntelliJ IDEA adds an intention for writing documentation for functions and methods. Plugin supports creating documentation for Kotlin and Java. Documentation is generated by artificial intelligence OpenAI based on the code of the method.
Before running the plugin, it is necessary to get your unique OpenAI API secret key, which can be generated here: OpenAI API keys, and then write it as the value of the openAIApiSecretKey in 'MyBundle.properties' file, so that relevant documentations can be created. For example, the corresponding line in the file will look like this:
openAIApiSecretKey=sk-SOMELETTERS
Otherwise, the same stub will always be shown as the documentation.
To run the plugin, download this method-doc-intellij-plugin project and choose Run Plugin in Run Configurations.
Method Documentation plugin will be automatically installed in the created IDE:
Settings/Preferences > Plugins > Installed > Method Documentation
When launched, this plugin adds the Generate method documentation item to the SDK intentions node in the Intentions list:
Settings/Preferences > Editor > Intentions > SDK intentions > Generate method documentation
The plugin analyzes symbols under the cursor in the code opened in the editor. If the cursor is located above the method name, IntelliJ IDEA proposes to create documentation for this method, if it hasn't yet exist, or replace the previous one:
If there were any comments related to the method before the function declaration, they are saved intact.
To generate the text of the documentation, a request, which contains the code of the corresponding method and an instruction to write documentation for it, is sent to the OpenAI artificial intelligence (model text-davinci-003) through the OpenAI API client.
fun awesomeMethod(dayNumber: Int): String {
return "Awesome day number $dayNumber!"
}
/**
* Generates an awesome phrase for the specified day number.
*
* @param dayNumber The day number.
* @return An awesome phrase for the specified day number.
*/
fun awesomeMethod(dayNumber: Int): String {
return "Awesome day number $dayNumber!"
}
To check the correctness of the plugin in various cases, there are tests for Kotlin and Java code examples. To run the tests, run 'MethodDocumentationIntentionTest.kt' class.
Tests considered the plugin work for Java and Kotlin code in the following cases:
- Methods without any documentation and comments above
- Simple method
- Complicated method with arguments and return value
- Methods with previous documentation
- Only documentation before method declaration
- Documentation and comment before method declaration
- Documentation and many comments with some spaces before method declaration
- Wrong caret position -- it is expected that Method Documentation intention is not shown in such cases