ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

Reformat to over-lines code with `max_line_length` limited

Open Goooler opened this issue 3 years ago • 3 comments
trafficstars

Expected Behavior

No need too many lines.

Observed Behavior

Original:

fun fooooooooooooo(key: String, default: Boolean) = Unit

class Bar {
  val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy { fooooooooooooo("fooooooooooooooooooooooooooooooooooooooooooooo", true) }
}

Reformay manually:

class Bar {
  val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy {
    fooooooooooooo("fooooooooooooooooooooooooooooooooooooooooooooo", true)
  }
}

Steps to Reproduce

Reformatd by ktlint:

class Bar {
  val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy {
    fooooooooooooo(
      "fooooooooooooooooooooooooooooooooooooooooooooo",
      true,
    )
  }
}

Your Environment

  • Version of ktlint used: 0.47.1
  • Relevant parts of the .editorconfig settings
root = true

[*]
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.{kt,kts}]
ij_kotlin_imports_layout = *
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_name_count_to_use_star_import = 999
ij_kotlin_name_count_to_use_star_import_for_members = 999
max_line_length = 80
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): CLI
  • Version of Gradle used (if applicable): None
  • Operating System and version: MacOS 12.5.1

Goooler avatar Sep 11 '22 04:09 Goooler

I can not reproduce the issue. For me the parameter wrapping is not activated. I have to reduce the max_line_length to 73 before the behavior is triggered. Please note that whenever you change the .editorconfig file with IntelliJ, that you need to save that file explicitly before invoking ktlint. I have been fooled by that many times.

paul-dingemans avatar Sep 16 '22 19:09 paul-dingemans

I've tried again on my other Mac, with the same result. Maybe I hadn't described this issue clearly, I meant ktlint formats

class Bar {
  val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy { fooooooooooooo("fooooooooooooooooooooooooooooooooooooooooooooo", true) }
}

to

class Bar {
  val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy {
    fooooooooooooo(
      "fooooooooooooooooooooooooooooooooooooooooooooo",
      true,
    )
  }
}

but the

class Bar {
  val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy {
    fooooooooooooo("fooooooooooooooooooooooooooooooooooooooooooooo", true)
  }
}

is ok, can pass the lint. If I enlarge max_line_length to 100, will still have the second result.

Goooler avatar Sep 17 '22 07:09 Goooler

Ah, now I see what you mean. I consider this as an enhancement on the wrapping rule. If a line exceeds the max_line_length and it contains a BLOCK element type, it should break before start and after end of the block.

  ~.c.i.p.impl.source.tree.LeafPsiElement (IDENTIFIER) "lazy"
~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " "
~.psi.KtLambdaArgument (LAMBDA_ARGUMENT)
  ~.psi.KtLambdaExpression (LAMBDA_EXPRESSION)
    ~.psi.KtFunctionLiteral (FUNCTION_LITERAL)
      ~.c.i.p.impl.source.tree.LeafPsiElement (LBRACE) "{"
      ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " "  <--- add line break
      ~.psi.KtBlockExpression (BLOCK)
        ...
      ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " <--- add line break
      ~.c.i.p.impl.source.tree.LeafPsiElement (RBRACE) "}"

In addition to this, the wrapping run needs to run before the argument-list-wrapping / function-signature rule.

paul-dingemans avatar Sep 17 '22 18:09 paul-dingemans