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

✨ Fix version catalog writing #724

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ internal sealed class TomlSection(open val name: String) {

object Root: TomlSection("root")
object Versions: TomlSection("versions")
object Plugins: TomlSection("plugins")
object Bundles: TomlSection("bundles")
object Libraries: TomlSection("libraries")
object Bundles: TomlSection("bundles")
object Plugins: TomlSection("plugins")
data class Custom(override val name: String) : TomlSection(name)

companion object {
val orderedSections = listOf(Root, Bundles, Plugins, Versions, Libraries)
val orderedSections = listOf(Root, Versions, Libraries, Bundles, Plugins)

fun from(name: String): TomlSection = orderedSections.firstOrNull { it.name == name } ?: Custom(name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal class VersionsCatalogUpdater(
Version -> {
linesForUpdates(line, findLineReferencing(line))
}

Libs, Plugin -> {
val updates = dependenciesUpdates.firstOrNull {
it.moduleId.name == line.name && it.moduleId.group == line.group
Expand Down Expand Up @@ -71,7 +72,7 @@ internal class VersionsCatalogUpdater(
val isObject = initialLine.unparsedValue.endsWith("}")

val commentPrefix = "##"
val availableSymbol = ""
val availableSymbol = "^"
val versionPrefix = when {
isObject || initialLine.section == TomlSection.Versions -> """= """"
else -> """:"""
Expand All @@ -86,14 +87,13 @@ internal class VersionsCatalogUpdater(
yield(commentPrefix)
yield(availableSymbol)
yield(versionPrefix)
}.sumOf { it.length }.let {
it + 1 // Because the length of availableSymbol (⬆) is 1, but it takes the width of 2 chars in the IDE.
}.let {
val indexOfCurrentVersion = initialLine.text.indexOf(currentVersion)
val gap = indexOfCurrentVersion - it
leadingSpace = " ".repeat((gap - 1).coerceAtLeast(0))
trailingSpace = if (gap > 0 && versionPrefix.endsWith(' ').not()) " " else ""
}
}.sumOf { it.length }
.let {
val indexOfCurrentVersion = initialLine.text.indexOf(currentVersion)
val gap = indexOfCurrentVersion - it
leadingSpace = " ".repeat((gap - 1).coerceAtLeast(0))
trailingSpace = if (gap > 0 && versionPrefix.endsWith(' ').not()) " " else ""
}
return availableUpdates.mapTo(mutableListOf(initialLine)) { versionCandidate: MavenVersion ->
val version = versionCandidate.value
TomlLine(
Expand Down
Loading