question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ktfmt rearranges comments on top of imports statements

See original GitHub issue

Let’s say I have

import x
import y
//noinspection BannedImport
import z

ktfmt reformats it as

//noinspection BannedImport
import x
import y
import z

This is problematic because some linting tools use a comment to opt out of errors on an import statement. (for example android lint allows you to opt out of a import error with a comment, but then you run ktfmt and it rearranges the comments therefore causing lint to break again.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
nreid260commented, Mar 22, 2022

That comment was from me. Both the AL team and I work at Google.

The word from the lint team is “Kotlin syntax doesn’t allow annotations on imports” With that said, it seems like it makes sense for ktfmt to be able to handle comments on import statements without being re-ordered. /shruggie

0reactions
nreid260commented, Mar 28, 2022

From talking to the current maintainers:

  • Comments on imports are more-or-less best effort
  • Support has existed for at least 2 years
  • The original focus was trailing comments
  • There was initially a warning for comments on their own line, but it was disabled for ease-of-use
  • There was minimal pushback on the “gluing” behaviour because
    • The Google repo is the primary target
    • The GJF authors have a lot of control over the tools running over Java code within Google
  • No eval was done on alternative behaviors

At this point, if someone wants to make ktfmt behave like GJF, with gluing, that seems fine to me. As long as ktfmt doesn’t crash, or delete the comments, it works for us. Just be aware that there’s also weirdness in that approach.

Fundamentally, associating comments with other syntax is unspecified, so there will always be tools that chose to do it in a different way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · facebook/ktfmt - GitHub
How can i get ktfmt to add missing full stops at the end of java doc sentences ... ktfmt rearranges comments on top...
Read more >
New blog-post => Organise imports with Prettier and friends
I would use an ESLint plugin instead of a Prettier plugin to sort imports. Semantically, Prettier really should be about making formatting ...
Read more >
Facebook Ktfmt Statistics & Issues - Codesti
Facebook Ktfmt: A program that reformats Kotlin source code to comply with the common community ... ktfmt rearranges comments on top of imports...
Read more >
ktfmt - IntelliJ IDEs Plugin - JetBrains Marketplace
ktfmt is a program that reformats Kotlin source code to comply with the common community standard for Kotlin code conventions. Plugin Site. Issue...
Read more >
Import order coding standard - python - Stack Overflow
and puts them all at the top of the file grouped together by the type of ... Broadly, You need to structure your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found