Skip to content

Trailing Comma misformats when expression #1519

@pschichtel

Description

@pschichtel
Contributor

Expected Behavior

enum class Test {
    TEST_1,
    TEST_2,
    TEST_3,
}

fun bla(value: Test) = when (value) {
    Test.TEST_1,
    Test.TEST_2 -> {
        println("1, 2")
    }
    Test.TEST_3 -> {
        println("3")
    }
}

gets formatted to (omitting the enum):

fun bla(value: Test) = when (value) {
    Test.TEST_1,
    Test.TEST_2,
-> {
        println("1, 2")
    }
    Test.TEST_3 -> {
        println("3")
    }
}

intellij does not modify it.

when the function initially looks like this:

fun bla(value: Test) = when (value) {
    Test.TEST_1,
    Test.TEST_2, -> {
        println("1, 2")
    }
    Test.TEST_3 -> {
        println("3")
    }
}

then ktlint doesn't change it, but intellij changes it to:

fun bla(value: Test) = when (value) {
    Test.TEST_1,
    Test.TEST_2,
    -> {
        println("1, 2")
    }
    Test.TEST_3 -> {
        println("3")
    }
}

when the function initially looks like this:

fun bla(value: Test) = when (value) {
    Test.TEST_1,
    Test.TEST_2
    -> {
        println("1, 2")
    }
    Test.TEST_3 -> {
        println("3")
    }
}

then ktlint changes it to:

fun bla(value: Test) = when (value) {
    Test.TEST_1,
    Test.TEST_2,
    -> {
        println("1, 2")
    }
    Test.TEST_3 -> {
        println("3")
    }
}

intellij however does not change it.

Your Environment

  • Version of ktlint used: 0.46.1
  • Relevant parts of the .editorconfig settings
    [{*.gradle.kts,*.kt,*.kts,*.main.kts,*.space.kts}]
    max_line_length = 9999
    ij_continuation_indent_size = 4
    ij_kotlin_align_in_columns_case_branch = false
    ij_kotlin_align_multiline_binary_operation = false
    ij_kotlin_align_multiline_extends_list = false
    ij_kotlin_align_multiline_method_parentheses = false
    ij_kotlin_align_multiline_parameters = false
    ij_kotlin_align_multiline_parameters_in_calls = false
    ij_kotlin_allow_trailing_comma = true
    ij_kotlin_allow_trailing_comma_on_call_site = true
    ij_kotlin_assignment_wrap = normal
    ij_kotlin_blank_lines_after_class_header = 0
    ij_kotlin_blank_lines_around_block_when_branches = 0
    ij_kotlin_blank_lines_before_declaration_with_comment_or_annotation_on_separate_line = 1
    ij_kotlin_block_comment_at_first_column = true
    ij_kotlin_call_parameters_new_line_after_left_paren = true
    ij_kotlin_call_parameters_right_paren_on_new_line = true
    ij_kotlin_call_parameters_wrap = on_every_item
    ij_kotlin_catch_on_new_line = false
    ij_kotlin_class_annotation_wrap = split_into_lines
    ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
    ij_kotlin_continuation_indent_for_chained_calls = false
    ij_kotlin_continuation_indent_for_expression_bodies = false
    ij_kotlin_continuation_indent_in_argument_lists = false
    ij_kotlin_continuation_indent_in_elvis = false
    ij_kotlin_continuation_indent_in_if_conditions = false
    ij_kotlin_continuation_indent_in_parameter_lists = false
    ij_kotlin_continuation_indent_in_supertype_lists = false
    ij_kotlin_else_on_new_line = false
    ij_kotlin_enum_constants_wrap = off
    ij_kotlin_extends_list_wrap = normal
    ij_kotlin_field_annotation_wrap = split_into_lines
    ij_kotlin_finally_on_new_line = false
    ij_kotlin_if_rparen_on_new_line = true
    ij_kotlin_import_nested_classes = false
    ij_kotlin_insert_whitespaces_in_simple_one_line_method = true
    ij_kotlin_keep_blank_lines_before_right_brace = 0
    ij_kotlin_keep_blank_lines_in_code = 1
    ij_kotlin_keep_blank_lines_in_declarations = 1
    ij_kotlin_keep_first_column_comment = true
    ij_kotlin_keep_indents_on_empty_lines = false
    ij_kotlin_keep_line_breaks = true
    ij_kotlin_lbrace_on_next_line = false
    ij_kotlin_line_comment_add_space = true
    ij_kotlin_line_comment_at_first_column = false
    ij_kotlin_method_annotation_wrap = split_into_lines
    ij_kotlin_method_call_chain_wrap = normal
    ij_kotlin_method_parameters_new_line_after_left_paren = true
    ij_kotlin_method_parameters_right_paren_on_new_line = true
    ij_kotlin_method_parameters_wrap = on_every_item
    ij_kotlin_name_count_to_use_star_import = 2147483647
    ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
    ij_kotlin_parameter_annotation_wrap = off
    ij_kotlin_space_after_comma = true
    ij_kotlin_space_after_extend_colon = true
    ij_kotlin_space_after_type_colon = true
    ij_kotlin_space_before_catch_parentheses = true
    ij_kotlin_space_before_comma = false
    ij_kotlin_space_before_extend_colon = true
    ij_kotlin_space_before_for_parentheses = true
    ij_kotlin_space_before_if_parentheses = true
    ij_kotlin_space_before_lambda_arrow = true
    ij_kotlin_space_before_type_colon = false
    ij_kotlin_space_before_when_parentheses = true
    ij_kotlin_space_before_while_parentheses = true
    ij_kotlin_spaces_around_additive_operators = true
    ij_kotlin_spaces_around_assignment_operators = true
    ij_kotlin_spaces_around_equality_operators = true
    ij_kotlin_spaces_around_function_type_arrow = true
    ij_kotlin_spaces_around_logical_operators = true
    ij_kotlin_spaces_around_multiplicative_operators = true
    ij_kotlin_spaces_around_range = false
    ij_kotlin_spaces_around_relational_operators = true
    ij_kotlin_spaces_around_unary_operator = false
    ij_kotlin_spaces_around_when_arrow = true
    ij_kotlin_variable_annotation_wrap = off
    ij_kotlin_while_on_new_line = false
    ij_kotlin_wrap_elvis_expressions = 1
    ij_kotlin_wrap_expression_body_functions = 1
    ij_kotlin_wrap_first_method_in_call_chain = false
    disabled_rules = no-wildcard-imports
  • Operating System and version: Archlinux

Activity

added this to the 0.47.0 milestone on Jun 26, 2022
changed the title [-]Trailing Comma misformats when expression[/-] [+]Trailing Comma misformats `when` expression[/+] on Jul 12, 2022
added 2 commits that reference this issue on Aug 7, 2022
aa1a664
507e483
paul-dingemans

paul-dingemans commented on Aug 7, 2022

@paul-dingemans
Collaborator

Thx for clear examples.

fun bla(value: Test) = when (value) {
Test.TEST_1,
Test.TEST_2 -> {
println("1, 2")
}
Test.TEST_3 -> {
println("3")
}
}

#1576 fixes the formatting to be same as IntelliJ IDEA default formatting.

fun bla(value: Test) = when (value) {
    Test.TEST_1,
    Test.TEST_2, -> {
        println("1, 2")
    }
    Test.TEST_3 -> {
        println("3")
    }
}

#1576 fixes the formatting to be same as IntelliJ IDEA default formatting.

fun bla(value: Test) = when (value) {
    Test.TEST_1,
    Test.TEST_2
    -> {
        println("1, 2")
    }
    Test.TEST_3 -> {
        println("3")
    }
}

Formatting of ktlint will not be changed. In ktlint the trailing comma is enforced instead of just allowed to improve the consistency in the codebase which is formatted by ktlint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @pschichtel@paul-dingemans

        Issue actions

          Trailing Comma misformats `when` expression · Issue #1519 · pinterest/ktlint