Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Reformat to over-lines code with max_line_length limited #1643

Closed
Goooler opened this issue Sep 11, 2022 · 3 comments · Fixed by #1653
Closed

Reformat to over-lines code with max_line_length limited #1643

Goooler opened this issue Sep 11, 2022 · 3 comments · Fixed by #1653

Comments

@Goooler
Copy link
Contributor

Goooler commented Sep 11, 2022

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
@paul-dingemans
Copy link
Collaborator

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.

@Goooler
Copy link
Contributor Author

Goooler commented Sep 17, 2022

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.

@paul-dingemans
Copy link
Collaborator

paul-dingemans commented Sep 17, 2022

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 added a commit to paul-dingemans/ktlint that referenced this issue Sep 21, 2022
…g that element exceeds the max line length or in case the block contains a newline

Closes pinterest#1643
paul-dingemans added a commit that referenced this issue Sep 26, 2022
…g that element exceeds the max line length or in case the block contains a newline (#1653)

Closes #1643
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants