Skip to content

Commit

Permalink
Default style is the Meta style, making that explicit
Browse files Browse the repository at this point in the history
Summary: Instead of having this implied, let's make it explicit what is the default style

Reviewed By: strulovich

Differential Revision: D58395863

fbshipit-source-id: 46a9f0c2d79b673e668e17b4f9ef2cbe373119e0
  • Loading branch information
Nivaldo Bondança authored and facebook-github-bot committed Jun 11, 2024
1 parent 4a393bb commit 96a7b1e
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

### Changed
- Preserves blank spaces between when clauses (https://github.com/facebook/ktfmt/issues/342)
- Named the default style as `Formatter.META_FORMAT` / `--meta-style`

### Fixed
- Compilation issues with online formatter (https://github.com/facebook/ktfmt/commit/8605080cb0aadb7eaba20f3b469d6ddafe32c941)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/facebook/ktfmt/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private const val EXIT_CODE_SUCCESS = 0

private val USAGE =
"""
Usage: ktfmt [--google-style | --kotlinlang-style] [--dry-run] [--set-exit-if-changed] [--stdin-name=<name>] [--do-not-remove-unused-imports] File1.kt File2.kt ...
Usage: ktfmt [--meta-style | --google-style | --kotlinlang-style] [--dry-run] [--set-exit-if-changed] [--stdin-name=<name>] [--do-not-remove-unused-imports] File1.kt File2.kt ...
Or: ktfmt @file
"""
.trimIndent()
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ data class ParsedArgs(
/** parseOptions parses command-line arguments passed to ktfmt. */
fun parseOptions(args: Array<out String>): ParseResult {
val fileNames = mutableListOf<String>()
var formattingOptions = FormattingOptions()
var formattingOptions = Formatter.META_FORMAT
var dryRun = false
var setExitIfChanged = false
var removeUnusedImports = true
var stdinName: String? = null

for (arg in args) {
when {
arg == "--meta-style" -> formattingOptions = Formatter.META_FORMAT
arg == "--google-style" -> formattingOptions = Formatter.GOOGLE_FORMAT
arg == "--kotlinlang-style" -> formattingOptions = Formatter.KOTLINLANG_FORMAT
arg == "--dry-run" || arg == "-n" -> dryRun = true
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/com/facebook/ktfmt/format/Formatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset

object Formatter {

@JvmField val META_FORMAT = FormattingOptions()

@JvmField
val GOOGLE_FORMAT =
FormattingOptions(
Expand All @@ -66,7 +68,7 @@ object Formatter {
*/
@JvmStatic
@Throws(FormatterException::class, ParseError::class)
fun format(code: String): String = format(FormattingOptions(), code)
fun format(code: String): String = format(META_FORMAT, code)

/**
* format formats the Kotlin code given in 'code' with 'removeUnusedImports' and returns it as a
Expand All @@ -75,7 +77,7 @@ object Formatter {
@JvmStatic
@Throws(FormatterException::class, ParseError::class)
fun format(code: String, removeUnusedImports: Boolean): String =
format(FormattingOptions(removeUnusedImports = removeUnusedImports), code)
format(META_FORMAT.copy(removeUnusedImports = removeUnusedImports), code)

/**
* format formats the Kotlin code given in 'code' with the 'maxWidth' and returns it as a string.
Expand Down
10 changes: 8 additions & 2 deletions core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ class ParsedArgsTest {

val formattingOptions = parsed.formattingOptions

val defaultFormattingOptions = FormattingOptions()
val defaultFormattingOptions = Formatter.META_FORMAT
assertThat(formattingOptions).isEqualTo(defaultFormattingOptions)
}

@Test
fun `parseOptions recognizes --meta-style`() {
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--meta-style", "foo.kt")))
assertThat(parsed.formattingOptions).isEqualTo(Formatter.META_FORMAT)
}

@Test
fun `parseOptions recognizes --dropbox-style`() {
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--kotlinlang-style", "foo.kt")))
Expand Down Expand Up @@ -199,7 +205,7 @@ class ParsedArgsTest {

private fun parseResultOk(
fileNames: List<String> = emptyList(),
formattingOptions: FormattingOptions = FormattingOptions(),
formattingOptions: FormattingOptions = Formatter.META_FORMAT,
dryRun: Boolean = false,
setExitIfChanged: Boolean = false,
removedUnusedImports: Boolean = true,
Expand Down
7 changes: 4 additions & 3 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.facebook.ktfmt.format

import com.facebook.ktfmt.format.Formatter.META_FORMAT
import com.facebook.ktfmt.testutil.assertFormatted
import com.facebook.ktfmt.testutil.assertThatFormatting
import com.google.common.truth.Truth.assertThat
Expand Down Expand Up @@ -1470,7 +1471,7 @@ class FormatterTest {
.trimMargin()

assertThatFormatting(code)
.withOptions(FormattingOptions(removeUnusedImports = false))
.withOptions(META_FORMAT.copy(removeUnusedImports = false))
.isEqualTo(code)
}

Expand Down Expand Up @@ -4281,7 +4282,7 @@ class FormatterTest {
|}
|"""
.trimMargin()
assertThatFormatting(code).withOptions(FormattingOptions(maxWidth = 22)).isEqualTo(expected)
assertThatFormatting(code).withOptions(META_FORMAT.copy(maxWidth = 22)).isEqualTo(expected)
}

@Test
Expand Down Expand Up @@ -5366,7 +5367,7 @@ class FormatterTest {
|class MyClass {}
|"""
.trimMargin()
assertThatFormatting(code).withOptions(FormattingOptions(maxWidth = 33)).isEqualTo(expected)
assertThatFormatting(code).withOptions(META_FORMAT.copy(maxWidth = 33)).isEqualTo(expected)
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/com/facebook/ktfmt/testutil/KtfmtTruth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import org.junit.Assert
*/
fun assertFormatted(
@Language("kts") code: String,
formattingOptions: FormattingOptions = FormattingOptions(),
formattingOptions: FormattingOptions = Formatter.META_FORMAT,
deduceMaxWidth: Boolean = false,
) {
val first = code.lines().first()
Expand Down Expand Up @@ -81,7 +81,7 @@ fun assertThatFormatting(@Language("kts") code: String): FormattedCodeSubject {

class FormattedCodeSubject(metadata: FailureMetadata, private val code: String) :
Subject(metadata, code) {
private var options: FormattingOptions = FormattingOptions()
private var options: FormattingOptions = Formatter.META_FORMAT
private var allowTrailingWhitespace = false

fun withOptions(options: FormattingOptions): FormattedCodeSubject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import static com.facebook.ktfmt.format.Formatter.GOOGLE_FORMAT;
import static com.facebook.ktfmt.format.Formatter.KOTLINLANG_FORMAT;
import static com.facebook.ktfmt.format.Formatter.META_FORMAT;

import com.facebook.ktfmt.format.FormattingOptions;

/** Configuration options for the formatting style. */
enum UiFormatterStyle {
DEFAULT("Default", new FormattingOptions()),
META("Meta (default)", META_FORMAT),
GOOGLE("Google (internal)", GOOGLE_FORMAT),
KOTLINLANG("Kotlinlang", KOTLINLANG_FORMAT);

Expand Down

0 comments on commit 96a7b1e

Please sign in to comment.