Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick Kotlin stdlib based on compiled Kotlin version #398

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package org.javacs.kt.classpath
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.attribute.BasicFileAttributes
import java.util.function.BiPredicate
import org.javacs.kt.util.tryResolving
import kotlin.math.min
import org.javacs.kt.util.findCommandOnPath
import java.nio.file.Paths
import org.javacs.kt.util.tryResolving

/** Backup classpath that find Kotlin in the user's Maven/Gradle home or kotlinc's libraries folder. */
object BackupClassPathResolver : ClassPathResolver {
Expand All @@ -27,24 +28,13 @@ private fun findLocalArtifact(group: String, artifact: String) =
private fun tryFindingLocalArtifactUsing(@Suppress("UNUSED_PARAMETER") group: String, artifact: String, artifactDirResolution: LocalArtifactDirectoryResolution): Path? {
val isCorrectArtifact = BiPredicate<Path, BasicFileAttributes> { file, _ ->
val name = file.fileName.toString()
when (artifactDirResolution.buildTool) {
"Maven" -> {
val version = file.parent.fileName.toString()
val expected = "${artifact}-${version}.jar"
name == expected
}
else -> name.startsWith(artifact) && name.endsWith(".jar")
}
name == "$artifact-${KotlinVersion.CURRENT}.jar"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses the language server's version of Kotlin, right? This might go wrong if the user has a different version of Kotlin installed than what the language server uses.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. However, I think we need a better way to figure out the Kotlin version in the project on which the LSP runs, maybe via extracting the version from the Gradle configuration, if possible.
The current implementation uses the first version it finds, which also doesn't necessarily match the Kotlin version used in the project.

}
return Files.list(artifactDirResolution.artifactDir)
.sorted(::compareVersions)
.findFirst()
.orElse(null)
?.let {
Files.find(artifactDirResolution.artifactDir, 3, isCorrectArtifact)
.findFirst()
.orElse(null)
}
?.let { Files.find(artifactDirResolution.artifactDir, 3, isCorrectArtifact).findFirst().orElse(null) }
}

private data class LocalArtifactDirectoryResolution(val artifactDir: Path?, val buildTool: String)
Expand Down Expand Up @@ -105,7 +95,7 @@ private fun compareVersions(left: Path, right: Path): Int {
val leftVersion = extractVersion(left)
val rightVersion = extractVersion(right)

for (i in 0 until Math.min(leftVersion.size, rightVersion.size)) {
for (i in 0 until min(leftVersion.size, rightVersion.size)) {
val leftRev = leftVersion[i].reversed()
val rightRev = rightVersion[i].reversed()
val compare = leftRev.compareTo(rightRev)
Expand All @@ -116,6 +106,6 @@ private fun compareVersions(left: Path, right: Path): Int {
return -leftVersion.size.compareTo(rightVersion.size)
}
private fun extractVersion(artifactVersionDir: Path): List<String> {
return artifactVersionDir.toString().split(".")
return artifactVersionDir.toString().split("/").last().split(".")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return artifactVersionDir.toString().split("/").last().split(".")
return artifactVersionDir.fileName.toString().split(".")

}