Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

[fix] prioritize most frequently used JDKs when selecting the project JDK #420

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@


## [Unreleased]
### Fixes 🛠️
- Prioritize most frequently used JDKs when selecting the project JDK.
| [#420](https://github.com/JetBrains/bazel-bsp/pull/420)

## [2.7.0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class JdkResolver(
) {

fun resolve(targets: Sequence<TargetInfo>): Jdk? {
val allCandidates = targets.mapNotNull { resolveJdkData(it) }.distinct().map { JdkCandidate(it) }
val allCandidates = targets.mapNotNull { resolveJdkData(it) }.sortByFrequency().map { JdkCandidate(it) }
if (allCandidates.none()) return null
val latestVersion = candidatesWithLatestVersion(allCandidates)
val complete = allCandidates.filter { it.isComplete }
Expand All @@ -33,7 +33,6 @@ class JdkResolver(
findLatestVersion(candidates)
?.let { version -> candidates.filter { it.version == version } }
.orEmpty()
.sortedBy { it.javaHome.toString() } // for predictable results

private fun findLatestVersion(candidates: Sequence<JdkCandidate>): String? =
candidates.mapNotNull { it.version }.maxByOrNull { Integer.parseInt(it) }
Expand Down Expand Up @@ -76,4 +75,10 @@ class JdkResolver(
val javaHome: URI?,
)

private fun <A> Sequence<A>.sortByFrequency(): Sequence<A> =
groupBy { it }.values
.sortedByDescending { it.size }
.map { it.first() }
.asSequence()

}