160

I'm trying to document a method and trying to use @link and @code as in JavaDoc.

I know in kotlin there is a kDoc but I can't find them or at least something similar.

humazed
  • 74,687
  • 32
  • 99
  • 138

9 Answers9

202

@link and @code doesn't exist in kDoc but can easily be replaced by Inline Markup.

from KotlinDoc Linking to Elements

Inline Markup

For inline markup, KDoc uses the regular Markdown syntax, extended to support a shorthand syntax for linking to other elements in the code.

Linking to Elements

To link to another element (class, method, property or parameter), simply put its name in square brackets:

Use the method [foo] for this purpose.

If you want to specify a custom label for the link, use the Markdown reference-style syntax:

Use [this method][foo] for this purpose. You can also use qualified names in the links. Note that, unlike JavaDoc, qualified names always use the dot character to separate the components, even before a method name:

Use [kotlin.reflect.KClass.properties] to enumerate the properties of the class. Names in links are resolved using the same rules as if the name was used inside the element being documented. In particular, this means that if you have imported a name into the current file, you don't need to fully qualify it when you use it in a KDoc comment.

Note that KDoc does not have any syntax for resolving overloaded members in links. Since the Kotlin documentation generation tool puts the documentation for all overloads of a function on the same page, identifying a specific overloaded function is not required for the link to work.

humazed
  • 74,687
  • 32
  • 99
  • 138
  • 24
    for anyone else struggling to resolve non static method, you can't just do [class.methodName] you have to do [full_package_location.methodName] – hmac Oct 04 '18 at 14:16
  • 4
    @hmac is correct, however you can also add the method name to your imports. The IDE won't give you the intellisense option to do this for you, but it does work. – Dave Oct 04 '18 at 21:53
  • how does apply to external links? – desgraci Apr 20 '20 at 08:42
  • I finally got the correct link to use by right-clicking the method name and selecting the second option "Copy Reference". I'm not sure if it makes a difference, but my method was a JVM static method inside of an Object. – beyondtheteal Sep 21 '20 at 16:50
  • It may be important: KDoc comments only start with /** and end with */. – antaki93 Apr 19 '21 at 11:24
  • Oh, but it doesn't work for overloads – Ovi Trif Nov 14 '22 at 13:00
48

For {@link SomeClass} in Java maps to [SomeClass] in Kotlin

For {@code true} in Java maps to `true` in Kotlin

38

You can write your code with java and convert class into Kotlin.

/**
 * @see <a href="http://somelink.com">link</a>
 */
public class Some {
}

will be converted to

/**
 * @see [link](http://somelink.com)
 */
class Some
Artur Dumchev
  • 1,252
  • 14
  • 17
  • I attempted this method in [this gist](https://gist.github.com/AdamSHurwitz/3a4098d0c810a5d270dda12721b91616). However, no formatting occurred for the link. – AdamHurwitz May 21 '19 at 20:59
  • 1
    Sorry for the late reply. It seems you had wrong docs format. For example, you should write your docs with /** docs here */, not just "//" which you probably used in your ContentRepository file. To check that everything is works as expected you can try example I provided. – Artur Dumchev Jun 08 '19 at 04:39
  • I've updated the format in the *Gist* above @Artur Dumchev. Your sample is in Java, whereas mine is in Kotlin which is why I'm unsure the exact format. Thanks! – AdamHurwitz Jun 09 '19 at 03:29
  • @ArturDumchev your answer is not correct, see [Michael's answer](https://stackoverflow.com/a/57590279/4843344). – Sobik Feb 23 '23 at 15:02
21

The answer that Artur gave is a good hint in general, but the result is wrong. At least IntelliJ IDEA does not grok the result. The markdown link format using []() is fine in the main comment text, but not for external links in the @see tag. For those, you need the same syntax as in Java:

/**
 * Do something.
 *
 * @see <a href="https://stackoverflow.com/q/45195370/2621917">External links in kdoc</a>
 */
Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
18

It seems we should just use a markdown hyperlink without any special tags such as @see or @link:

/**
 * This is a doc.
 *
 * See [this](https://google.com)
 * And [this](https://stackoverflow.com)
 */
fun myfun() {}

This doc renders in following way in IDE:

render of a doc in IDE

James Bond
  • 2,229
  • 1
  • 15
  • 26
16

I struggled with this for a bit with Android Studio 3.5.2 on Mac. This worked for me:

/**
* [Your fully-qualified class name.function name]
*/

If I didn't use the fully-qualified name Kdoc would complain that it was a unresolved reference. What I couldn't figure out is how to actually use the link itself. For that you need to press and hold the COMMAND key (on Mac) and then the links would be active.

szaske
  • 1,887
  • 22
  • 32
15

To reference a method use the class:

/**
 * When the configuration succeeds **[MyCallback.onConfigured]** is called.
 */
class MyClass(myCallback: MyCallback) {

Using a variable/parameter does not work:

/**
 * When the configuration succeeds **[myCallback.onConfigured]** is called.
 */
class MyClass(myCallback: MyCallback) {
hb0
  • 3,350
  • 3
  • 30
  • 48
12

As for the @code you should use Markdown syntax (cause KDoc is an extended version of Markdown):

To produce a code block in Markdown, simply indent every line of the block by at least 4 spaces or 1 tab.

/**
 * Some code sample:
 * 
 *    Set<String> s;
 *    System.out.println(s);
 */
class Scratch
Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74
7

Sample how to leave links for classes:

/**
 * [YourClass] Methods
 * */

also with method calls

/**
 * [YourClass.someMothod] Methods
 * */

Real example:

 /**
 * [BaseActivity] Methods
 * */
override fun initVars() {
    //Just Sample
}

/**
 * [MainContract.View] - Overrides
 * */
override fun handleConnectionMassage(isShow: Boolean) {
    //Just Sample
}
Mopto
  • 370
  • 5
  • 6