ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

Break line when over max_line_length

Open rodgavila opened this issue 7 years ago • 8 comments

Ktlint currently has a rule for "lines to long", however it cannot auto-fix it. This I believe is the only rule that cannot be autofixed, and by implementing would guarantee that the linter would always succeed after -format is passed.

rodgavila avatar Jul 13 '18 23:07 rodgavila

I think wildcard import is another rule that cannot be autofixed

gortiz avatar Sep 04 '18 09:09 gortiz

Is this being worked on?

cable729 avatar Oct 24 '18 16:10 cable729

@cable729 Not directly. I expect this to change once https://github.com/shyiko/ktlint/pull/294 is merged, though.

shyiko avatar Oct 24 '18 16:10 shyiko

Any updates here?

cable729 avatar Oct 25 '19 17:10 cable729

This is one of the few major rules of ktlint that are checked for but cannot be auto-fixed. There have been no updates for a year.

"Not directly. I expect this to change once #294 is merged, though."

#294 was closed on Feb 11 2019. Any idea the timeline on this ticket?

hravichandran0 avatar Nov 06 '19 23:11 hravichandran0

@Tapchicoma, I see you added this as an enhancement. Might you also have an approximate idea when the auto-format for this will be implemented?

sonahovhannisyan avatar Nov 28 '19 13:11 sonahovhannisyan

@sonahovhannisyan no idea, PRs are welcome :slightly_smiling_face:

Tapchicoma avatar Nov 29 '19 13:11 Tapchicoma

I've been trying to look into a fix for this, and I think that the existing IndentationRule can be re-used in some way as that already handles line breaking and indentation, see this test case and it's expected value.

The options as I see it are:

  1. Integrate the MaxLineLengthRule into IndentationRule
  2. Extract the formatting code (specifically the knowledge of when to break the line) from IndentationRule into a shared class for use in both Rules

There is another option which is to implement the logic for where to break functions, lines, and classes according to the requirements outline by the Android style guide and the various break rules outlined throughout Kotlin's code conventions doc. I wouldn't want to do it this way as we'd end up with a lot of potentially duplicate code.

I'm not super familiar with this codebase but am willing to make the change and PR if anyone else can validate this hunch or give insight the options I've presented or others I might be missing.

cdavietei avatar Feb 13 '21 22:02 cdavietei

To make matters worse, ktlint seems to be causing the problem and then complaining that it can't fix it.

I'm using KotlinPoet to generate some code. The code that comes out isn't formatted so well, so I've been trying to run ktlint afterward, so that if anybody actually has to look at the code, it's easy to read.

But I'm getting a problem because KotlinPoet outputs an extension function with the = on the next line. It has to do this because putting it on the same line as the declaration would exceed the 100 character max line length.

When I run ktlint -F on this file, it moves the = up a line and then promptly fails because it can't fix the too long line it just created. This fails the whole build.

I mean it's one thing to say we don't support auto-fixing max line length errors, but when you take a file where all the lines are under the max line length and then produce a new file that has lines over the max line length, that seems like a bug.

toddobryan avatar Oct 04 '22 00:10 toddobryan

To make matters worse, ktlint seems to be causing the problem and then complaining that it can't fix it.

I'm using KotlinPoet to generate some code. The code that comes out isn't formatted so well, so I've been trying to run ktlint afterward, so that if anybody actually has to look at the code, it's easy to read.

But I'm getting a problem because KotlinPoet outputs an extension function with the = on the next line. It has to do this because putting it on the same line as the declaration would exceed the 100 character max line length.

When I run ktlint -F on this file, it moves the = up a line and then promptly fails because it can't fix the too long line it just created. This fails the whole build.

I mean it's one thing to say we don't support auto-fixing max line length errors, but when you take a file where all the lines are under the max line length and then produce a new file that has lines over the max line length, that seems like a bug.

This indeed sound like a bug. It should however not be related to this current issue as it is not very likely to be picked up. Please create a new issue and post your concrete example with .editorconfig settings.

paul-dingemans avatar Oct 04 '22 07:10 paul-dingemans

same issue, I'll create a new bug...

xenoterracide avatar Oct 04 '22 14:10 xenoterracide

Any idea's to break up long lines should be broken down in smaller issues and preferably be part of other rules. For example the function-signature avoids long lines by taking the max_line_length parameter into account.

paul-dingemans avatar Nov 24 '22 17:11 paul-dingemans