AlikElzin-kilaka
AlikElzin-kilaka

Reputation: 36031

How to see Kotlin stdlib source code in Intellij?

Any idea how to obtain the source code for Kotlin's stdlib?

In the screencap below, I don't have any option to download the source code like other maven libraries.

I'm using the following Kotlin dependency:

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib-jdk8</artifactId>
    <version>1.2.30</version>
</dependency>

enter image description here

Upvotes: 21

Views: 5335

Answers (3)

dkb
dkb

Reputation: 4596

We can solve this by

  1. import and attach source code or
  2. using "idea" plugin in gradle build file.

=> manually importing source code in intelliJ-idea

Get source file from github-release
I selected 1.6.21 and added at following macos path(or find .m2 maven folder at respective OS)

/Users/${USER}/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib/<kotlin-version>
e.g.
/Users/${USER}/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib/1.6.21

When IDE prompts to download a source or attach a source, similar to following screen

Verify by checking the implementation of take for String class i.e. "abcd".take(3).
when you go to take method's implementation before IDE decompiles String class and points to StringsKt.java but after source is attached it points to _Strings.kt And it looks as

enter image description here

OR

=> You can add following code to build.gradle.kts, in latest kotlin version, source is getting downloaded along with versions so no manual effort.

// Add idea plugin
plugins {
    id("idea")
}
// configure idea plugin
idea {
    module {
        isDownloadJavadoc = true
        isDownloadSources = true
    }
}

Upvotes: 1

Grigory Kislin
Grigory Kislin

Reputation: 18020

enter image description here

Take in note of not all classes present in ...-source.jar (e.g. missed mutableSetOf() in Sets.Kt)

Upvotes: 0

kuceraf
kuceraf

Reputation: 601

For me it helps to change maven dependency on Kotlin in pom.xml from

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib-jdk8</artifactId>
    <version>1.2.30</version>
</dependency>

to

<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib</artifactId>
        <version>1.3.31</version>
    </dependency>
</dependencies>

more info on adding Kotlin to project as maven dependency can be found here: https://kotlinlang.org/docs/reference/using-maven.html

Upvotes: 3

Related Questions