Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

FunctionSignatureRule with long function name suggest expression body on the same line, when no space available #1773

Closed
jtietema opened this issue Jan 12, 2023 · 3 comments · Fixed by #1774

Comments

@jtietema
Copy link
Contributor

jtietema commented Jan 12, 2023

Given a long function name (as found in test cases for example) with an expression body and no parameters:

fun `a very long function name as found in a test case`() =
                "some-result"

The function name here exceeds the maxLineLength of the FunctionSignatureRule but not the overal MaxLineLength.

Expected Behavior

To accept this formatting as breaking down into multiline function signature will not fix the problem.

Observed Behavior

Ktlint will assume there are parameters and we will go into multiline mode. Suggesting to put the expression on the same line as the function signature.

Steps to Reproduce

Unit testcase is included in the following PR: #1774

Your Environment

  • Version of ktlint used: 0.47.1
  • .editorconfig not used
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): detekt formatting plugin 1.22
  • Version of Gradle used (if applicable): 7.6
  • Operating System and version: Fedora 37 - Linux
jtietema pushed a commit to jtietema/ktlint that referenced this issue Jan 12, 2023
The FunctionSignature rule would assume multiline mode when exceeding the
maxLineLength, but this isn't always the case.

Fixes: pinterest#1773
@paul-dingemans
Copy link
Collaborator

The function signature rule does not have a separate maxLineLength property but uses the same property as the max line length rule.

Test names between backticks may exceed the max line length when property ktlint_ignore_back_ticked_identifier is enabled.

You have specified that you do not use a '.editorconfig' file. What is the exact command that you executed that reproduces problem above?

@jtietema
Copy link
Contributor Author

I am using the detekt-formatting plugin, so it is executed as part of ./gradlew detekt.
Detekt does have a feature to override the maxLineLength for the rule https://detekt.dev/docs/rules/formatting#functionsignature

I have added a test case in the PR #1774 which demonstrates the issue. If you have a function with:

  • a very long name that runs past the maxLineLength
  • has no arguments
  • has a short expression body
    The ktlint rule will incorrectly calculate the remaining space as it will assume the ) = will be on its own line. Resulting in a remaing space of maxLineLength - ") =".length when in reality it is a single line (as we don't have parameters to wrap) so the remaining space should be 0 (at least, that is what I think it should be 😅).

@paul-dingemans
Copy link
Collaborator

This is a regression bug introduced in 0.48.x.

@paul-dingemans paul-dingemans added this to the 0.48.2 milestone Jan 15, 2023
paul-dingemans added a commit that referenced this issue Jan 15, 2023
…meters

Closes: #1773

Co-authored-by: Jeroen Tietema <jeroen.tietema@siriusxm.com>
Co-authored-by: paul-dingemans <paul-dingemans@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants