Skip to content

Commit

Permalink
Update ktfmt to 0.51 and resolve breaking changes (cortinico#303)
Browse files Browse the repository at this point in the history
* Remove FormattingOptions.Style since it is removed from ktfmt, update changelog

* cortinico#302 remove the dropboxStyle() option because it is removed from ktfmt

* cortinico#302 fix formatting

* Update KtfmtFormatter.kt

---------

Co-authored-by: Nicola Corti <[email protected]>
  • Loading branch information
simonhauck and cortinico authored Jun 22, 2024
1 parent a2094f7 commit 3eae027
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 53 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Please add your entries according to this format.

## Unreleased

- Remove dropboxStyle since it is no longer supported by ktfmt. Use kotlinLangStyle() instead
- Fix task caching for ktfmtCheckTask when project has multiple source sets (#288)
- Kotlin to 2.0.0
- ksp to 2.0.0-1.0.22
- KtFmt to 0.51

## Version 0.18.0 _(2024-04-13)_

Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ To enable different styles you can simply:

```kotlin
ktfmt {
// Dropbox style - 4 space indentation
dropboxStyle()

// Google style - 2 space indentation & automatically adds/removes trailing commas
googleStyle()

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ diffUtils = "4.12"
junit = "5.10.2"
kotlin = "2.0.0"
ktfmt-plugin = "0.18.0"
ktfmt = "0.50"
ktfmt = "0.51"
pluginPublish = "1.2.1"
truth = "1.4.2"
versionCheck = "0.51.0"
Expand Down
1 change: 0 additions & 1 deletion plugin-build/plugin/api/plugin.api
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
public abstract class com/ncorti/ktfmt/gradle/KtfmtExtension {
public fun <init> ()V
public final fun dropboxStyle ()V
public abstract fun getBlockIndent ()Lorg/gradle/api/provider/Property;
public abstract fun getContinuationIndent ()Lorg/gradle/api/provider/Property;
public abstract fun getDebuggingPrintOpsAfterFormatting ()Lorg/gradle/api/provider/Property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import java.io.Serializable

internal data class FormattingOptionsBean(

/** The style used by ktfmt */
val style: Style = Style.FACEBOOK,

/** ktfmt breaks lines longer than maxWidth. */
val maxWidth: Int = defaultMaxWidth,

Expand Down Expand Up @@ -52,11 +49,6 @@ internal data class FormattingOptionsBean(
*/
val debuggingPrintOpsAfterFormatting: Boolean = false,
) : Serializable {
enum class Style {
FACEBOOK,
DROPBOX,
GOOGLE
}

companion object {
private const val serialVersionUID: Long = 1L
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.ncorti.ktfmt.gradle

import com.ncorti.ktfmt.gradle.FormattingOptionsBean.Style.DROPBOX
import com.ncorti.ktfmt.gradle.FormattingOptionsBean.Style.FACEBOOK
import com.ncorti.ktfmt.gradle.FormattingOptionsBean.Style.GOOGLE
import org.gradle.api.provider.Property

/** Gradle Extension to help you configure ktfmt-gradle */
Expand All @@ -17,8 +14,6 @@ abstract class KtfmtExtension {
manageTrailingCommas.convention(DEFAULT_MANAGE_TRAILING_COMMAS)
}

internal var ktfmtStyle = FACEBOOK

/** ktfmt breaks lines longer than maxWidth. Default 100. */
abstract val maxWidth: Property<Int>

Expand Down Expand Up @@ -64,18 +59,9 @@ abstract class KtfmtExtension {
*/
abstract val debuggingPrintOpsAfterFormatting: Property<Boolean>

/** Enables --dropbox-style (equivalent to set blockIndent to 4 and continuationIndent to 4). */
@Suppress("MagicNumber")
fun dropboxStyle() {
ktfmtStyle = DROPBOX
blockIndent.set(4)
continuationIndent.set(4)
}

/** Sets the Google style (equivalent to set blockIndent to 2 and continuationIndent to 2). */
@Suppress("MagicNumber")
fun googleStyle() {
ktfmtStyle = GOOGLE
blockIndent.set(2)
continuationIndent.set(2)
manageTrailingCommas.set(true)
Expand All @@ -87,14 +73,12 @@ abstract class KtfmtExtension {
*/
@Suppress("MagicNumber")
fun kotlinLangStyle() {
ktfmtStyle = GOOGLE
blockIndent.set(4)
continuationIndent.set(4)
}

internal fun toBean(): FormattingOptionsBean =
FormattingOptionsBean(
style = ktfmtStyle,
maxWidth = maxWidth.get(),
blockIndent = blockIndent.get(),
continuationIndent = continuationIndent.get(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,10 @@ internal object KtfmtFormatter {

internal fun FormattingOptionsBean.toFormattingOptions(): FormattingOptions =
FormattingOptions(
style = style.toKtfmtStyle(),
maxWidth = maxWidth,
blockIndent = blockIndent,
continuationIndent = continuationIndent,
removeUnusedImports = removeUnusedImports,
manageTrailingCommas = manageTrailingCommas,
debuggingPrintOpsAfterFormatting = debuggingPrintOpsAfterFormatting
)

internal fun FormattingOptionsBean.Style.toKtfmtStyle(): FormattingOptions.Style =
when (this) {
FormattingOptionsBean.Style.FACEBOOK -> FormattingOptions.Style.FACEBOOK
FormattingOptionsBean.Style.DROPBOX -> FormattingOptions.Style.DROPBOX
FormattingOptionsBean.Style.GOOGLE -> FormattingOptions.Style.GOOGLE
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.ncorti.ktfmt.gradle

import com.google.common.truth.Truth.assertThat
import com.ncorti.ktfmt.gradle.FormattingOptionsBean.Style.DROPBOX
import com.ncorti.ktfmt.gradle.FormattingOptionsBean.Style.GOOGLE
import com.ncorti.ktfmt.gradle.KtfmtExtension.Companion.DEFAULT_BLOCK_INDENT
import com.ncorti.ktfmt.gradle.KtfmtExtension.Companion.DEFAULT_CONTINUATION_INDENT
import com.ncorti.ktfmt.gradle.KtfmtExtension.Companion.DEFAULT_DEBUGGING_PRINT_OPTS
Expand Down Expand Up @@ -49,17 +47,6 @@ class KtfmtExtensionTest {
.isEqualTo(DEFAULT_DEBUGGING_PRINT_OPTS)
}

@Test
fun `dropboxStyle configures correctly`() {
val extension = createExtension()

extension.dropboxStyle()

assertThat(extension.blockIndent.get()).isEqualTo(4)
assertThat(extension.continuationIndent.get()).isEqualTo(4)
assertThat(extension.ktfmtStyle).isEqualTo(DROPBOX)
}

@Test
fun `googleStyle configures correctly`() {
val extension = createExtension()
Expand All @@ -68,7 +55,6 @@ class KtfmtExtensionTest {

assertThat(extension.blockIndent.get()).isEqualTo(2)
assertThat(extension.continuationIndent.get()).isEqualTo(2)
assertThat(extension.ktfmtStyle).isEqualTo(GOOGLE)
}

@Test
Expand All @@ -79,7 +65,6 @@ class KtfmtExtensionTest {

assertThat(extension.blockIndent.get()).isEqualTo(4)
assertThat(extension.continuationIndent.get()).isEqualTo(4)
assertThat(extension.ktfmtStyle).isEqualTo(GOOGLE)
}

@Test
Expand Down

0 comments on commit 3eae027

Please sign in to comment.