Skip to content

Commit

Permalink
Add support for manageTrailingCommas (cortinico#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
cortinico authored Mar 23, 2024
1 parent 775f8d5 commit 7d81b76
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Please add your entries according to this format.
## Unreleased

- Make `KtfmtCheckTask` cacheable
- Add support for `manageTrailingCommas` and enables it by default for googleStyle

## Version 0.17.0 *(2024-01-31)*

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ ktfmt {
// Dropbox style - 4 space indentation
dropboxStyle()

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

// KotlinLang style - 4 space indentation - From kotlinlang.org/docs/coding-conventions.html
Expand All @@ -112,6 +112,8 @@ ktfmt {
continuationIndent.set(8)
// Whether ktfmt should remove imports that are not used.
removeUnusedImports.set(false)
// Whether ktfmt should automatically add/remove trailing commas.
manageTrailingCommas.set(false)
}
```

Expand Down Expand Up @@ -144,4 +146,4 @@ Feel free to open a issue or submit a pull request for any bugs/improvements.

## License 📄

This project is licensed under the MIT License - see the [License](License) file for details
This project is licensed under the MIT License - see the [License](License) file for details
1 change: 1 addition & 0 deletions plugin-build/plugin/api/plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public abstract class com/ncorti/ktfmt/gradle/KtfmtExtension {
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;
public abstract fun getManageTrailingCommas ()Lorg/gradle/api/provider/Property;
public abstract fun getMaxWidth ()Lorg/gradle/api/provider/Property;
public abstract fun getRemoveUnusedImports ()Lorg/gradle/api/provider/Property;
public final fun googleStyle ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@ internal data class FormattingOptionsBean(
*/
val continuationIndent: Int = 4,

/**
* Automatically remove and insert trialing commas.
*
* Lists that cannot fit on one line will have trailing commas inserted. Lists that span
* multiple lines will have them removed. Manually inserted trailing commas cannot be used as a
* hint to force breaking lists to multiple lines.
*/
val manageTrailingCommas: Boolean = false,

/** Whether ktfmt should remove imports that are not used. */
val removeUnusedImports: Boolean = true,

/**
* Print the Ops generated by KotlinInputAstVisitor to help reason about formatting (i.e.,
* newline) decisions
*/
val debuggingPrintOpsAfterFormatting: Boolean = false
val debuggingPrintOpsAfterFormatting: Boolean = false,
) : Serializable {
enum class Style {
FACEBOOK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ abstract class KtfmtExtension {
continuationIndent.convention(DEFAULT_CONTINUATION_INDENT)
removeUnusedImports.convention(DEFAULT_REMOVE_UNUSED_IMPORTS)
debuggingPrintOpsAfterFormatting.convention(DEFAULT_DEBUGGING_PRINT_OPTS)
manageTrailingCommas.convention(DEFAULT_MANAGE_TRAILING_COMMAS)
}

internal var ktfmtStyle = FACEBOOK
Expand Down Expand Up @@ -45,6 +46,15 @@ abstract class KtfmtExtension {
*/
abstract val continuationIndent: Property<Int>

/**
* Automatically remove and insert trialing commas.
*
* Lists that cannot fit on one line will have trailing commas inserted. Lists that span
* multiple lines will have them removed. Manually inserted trailing commas cannot be used as a
* hint to force breaking lists to multiple lines.
*/
abstract val manageTrailingCommas: Property<Boolean>

/** Whether ktfmt should remove imports that are not used. */
abstract val removeUnusedImports: Property<Boolean>

Expand All @@ -68,6 +78,7 @@ abstract class KtfmtExtension {
ktfmtStyle = GOOGLE
blockIndent.set(2)
continuationIndent.set(2)
manageTrailingCommas.set(true)
}

/**
Expand All @@ -88,6 +99,7 @@ abstract class KtfmtExtension {
blockIndent = blockIndent.get(),
continuationIndent = continuationIndent.get(),
removeUnusedImports = removeUnusedImports.get(),
manageTrailingCommas = manageTrailingCommas.get(),
debuggingPrintOpsAfterFormatting = debuggingPrintOpsAfterFormatting.get()
)

Expand All @@ -97,5 +109,6 @@ abstract class KtfmtExtension {
internal const val DEFAULT_CONTINUATION_INDENT: Int = 4
internal const val DEFAULT_REMOVE_UNUSED_IMPORTS: Boolean = true
internal const val DEFAULT_DEBUGGING_PRINT_OPTS: Boolean = false
internal const val DEFAULT_MANAGE_TRAILING_COMMAS: Boolean = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ internal fun FormattingOptionsBean.toFormattingOptions(): FormattingOptions =
blockIndent = blockIndent,
continuationIndent = continuationIndent,
removeUnusedImports = removeUnusedImports,
manageTrailingCommas = manageTrailingCommas,
debuggingPrintOpsAfterFormatting = debuggingPrintOpsAfterFormatting
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FormattingOptionsBeanTest {
blockIndent = 43,
continuationIndent = 44,
removeUnusedImports = false,
manageTrailingCommas = true,
debuggingPrintOpsAfterFormatting = true
)

Expand All @@ -23,6 +24,7 @@ class FormattingOptionsBeanTest {
assertThat(opts.blockIndent).isEqualTo(43)
assertThat(opts.continuationIndent).isEqualTo(44)
assertThat(opts.removeUnusedImports).isEqualTo(false)
assertThat(opts.manageTrailingCommas).isEqualTo(true)
assertThat(opts.debuggingPrintOpsAfterFormatting).isEqualTo(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class KtfmtExtensionTest {
extension.blockIndent.set(43)
extension.continuationIndent.set(44)
extension.removeUnusedImports.set(false)
extension.manageTrailingCommas.set(true)
extension.debuggingPrintOpsAfterFormatting.set(true)

val bean = extension.toBean()
Expand All @@ -98,6 +99,7 @@ class KtfmtExtensionTest {
assertThat(bean.blockIndent).isEqualTo(43)
assertThat(bean.continuationIndent).isEqualTo(44)
assertThat(bean.removeUnusedImports).isEqualTo(false)
assertThat(bean.manageTrailingCommas).isEqualTo(true)
assertThat(bean.debuggingPrintOpsAfterFormatting).isEqualTo(true)
}

Expand Down

0 comments on commit 7d81b76

Please sign in to comment.