-
Notifications
You must be signed in to change notification settings - Fork 217
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
omar25h
wants to merge
1
commit into
fwcd:main
Choose a base branch
from
omar25h:omar25h/enhance-stdlib-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+7
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 { | ||||||
|
@@ -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" | ||||||
} | ||||||
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) | ||||||
|
@@ -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) | ||||||
|
@@ -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(".") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.