nbadal / ktlint-intellij-plugin

Ktlint plugin for IntelliJ IDEA + Android Studio
MIT License
158 stars 24 forks source link

Plugin should respect IntellIj formatter tags #527

Closed ctdavids closed 3 months ago

ctdavids commented 4 months ago

Ktlint supports them: https://github.com/pinterest/ktlint/pull/1895

However, the plugin will still format within those sections.

For example:

    fun foo() {
        // @formatter:off
        listOf(
            "x" to 1, "x" to 2, "x" to 3,
            "y" to 1, "y" to 2, "y" to 3,
            "z" to 1, "z" to 2, "z" to 3
        )
        // @formatter:on
    }

with an .editorconfig of:

[*]
ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
ij_formatter_tags_enabled = true

should result in the above not being formatted.

If you run ktlintFormat on the above it will NOT format it. But if you enable the plugin in distract free mode it will reformat it. And in manual mode it will complain about it.

paul-dingemans commented 4 months ago

I cannot reproduce your problem with current development version of ktlint plugin. The plugin relies completely on ktlint for reading an applying the .editorconfig on the source code. So this looks like a configuration issue.

Which version of the plugin are you using?

Please note that when you modify the .editorconfig in Intellij IDEA, you need to explicitly save it before Ktlint picks it up correctly.

ctdavids commented 4 months ago

Plugin version: 0.22.0 which seems to be the latest

The .editorconfig is pre-existing so I don't think it's that.

It's possible that my minimal example above is too minimal. I can see if I can setup a simple example. Is there a preferred way of providing such a thing? Just rar/zip up a gradle project?

ctdavids commented 4 months ago

Ok, here is a minimal example. The .editorconfig has almost nothing except the above stuff in it and a root tag which I assume means it's relying on a variety of defaults (my actual use case has a ton of configuration specified, but kept it as small as possible here for a repro).

If you run gradle build, it works. If you run ktlintCheck it passes. If you run ktlintFormat it makes no changes.

But if you install the ktlint IntelliJ plugin (version 0.22.0) in IntelliJ (my version is IntelliJ IDEA 2024.1.2 (Ultimate Edition), Build #IU-241.17011.79, built on May 21, 2024) and enable the plugin mode in Manual it will complain about the list items on the same line. If you enable it in Distract free mode and format in IntelliJ it will reformat things. If you don't enable it and format IntelliJ won't make any changes.

I just imported it into IntelliJ using existing sources (gradle).

ktlintExample.zip

paul-dingemans commented 4 months ago

This is quite a mystery sofar. At least I have a temporary workaround:

    private fun foo() {
        // @formatter:off
        listOf(
            "x" to 1, "x" to 2, "x" to 3,
            "y" to 1, "y" to 2, "y" to 3,
            "z" to 1, "z" to 2, "z" to 3,
        )
        // @formatter:on
    }
Screenshot 2024-06-05 at 09 10 35 Screenshot 2024-06-05 at 09 10 56
ctdavids commented 4 months ago

Thanks for the workaround. That's a weird behavioural quirk you've found there.

ctdavids commented 3 months ago

Do note the workaround only works for a single function at at time. This will result in complaints about the bar method:

    // @formatter:off
    fun foo() {
        listOf(
            "x" to 1, "x" to 2, "x" to 3,
            "y" to 1, "y" to 2, "y" to 3,
            "z" to 1, "z" to 2, "z" to 3,
        )
    }

    fun bar() {
        listOf(
            "x" to 1, "x" to 2, "x" to 3,
            "y" to 1, "y" to 2, "y" to 3,
            "z" to 1, "z" to 2, "z" to 3,
        )
    }
    // @formatter:on

If you put them around a class it (seems) to work for all things in the class.

weird

paul-dingemans commented 3 months ago

It is not ideal, and I will most likely be able to fix it at a later time. But for now this has less priority to me compared to release the 1.3.0 compatible version of the plugin.

paul-dingemans commented 3 months ago

The problem is somehow magically resolved in the new (development) version of the plugin: https://plugins.jetbrains.com/plugin/15057-ktlint/versions/dev.

ctdavids commented 3 months ago

Confirmed somewhat fixed by this version, but there's a new failure: https://github.com/nbadal/ktlint-intellij-plugin/issues/535