Ktlint: Max line length

Created on 30 May 2017  路  9Comments  路  Source: pinterest/ktlint

I would like to enforce a maximum line length on the files being linted. I'm partial to 100 characters, or even 120.

Most helpful comment

On the other hand we could make it part of the ktlint and enable it whenever max_line_length is explicitly set for kt/kts files.

All 9 comments

Hi @Nycto. I'm not sure this is something that can be part of the standard ruleset (while I try to stay within 120 characters myself). But here are some of the options that you might want to consider:

  • set max_line_length in your .editorconfig
  • enforce max line length through additional ruleset e.g (MaximumLineLengthRule rule by @mhlz). On the command line you can enable it with ktlint -R com.github.mhlz:ktlint-rules:master-SNAPSHOT (for Maven/Gradle see readme.md).

On the other hand we could make it part of the ktlint and enable it whenever max_line_length is explicitly set for kt/kts files.

I'm not sure this is something that can be part of the standard ruleset

I suppose it depends on what your goals with this project are. Do you have any tenants you're working with to help you decide whether a rule belomgs in the standard set? For example, if you told me "All Kotlin files should feel like they were written by the same person", I could make a good case for this. But I would have a harder time with something like "Kotlin files should be as short and noise free as possible".

Place where kotlin code style stuff is discussed: https://github.com/yole/kotlin-style-guide

Perhaps pose this question here and let the community come to some sort of consensus.

@Nycto available in 0.9.0.

Add

[*.{kt,kts}]
max_line_length = 120

to the .editorconfig. ktlint will take it from there.

P.S. comments are not checked (intentionally).
P.P.S. ktlint does not (and probably never will) enforce any max_line_length by default.

In my rule I also excluded imports since they can't be broken up and some class names are really long (especially in libraries we don't control).

Imports are always folded away in IntelliJ anyways. Does ktlint's implementation ignore them?

@mhlz now it does :) (both package & import directives)

@shyiko Is it possible to ignore max line length for kotlin raw strings?

@alashow yes, please check this section of project readme: https://github.com/pinterest/ktlint#how-do-i-suppress-an-error-for-a-lineblockfile

If it will not work for you, please open a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

plastiv picture plastiv  路  4Comments

DanteAndroid picture DanteAndroid  路  3Comments

nokite picture nokite  路  4Comments

Tapchicoma picture Tapchicoma  路  3Comments

artnc picture artnc  路  3Comments