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

Add support for multiple/runtime-specific versions of ktlint #35

Closed
IvanPizhenko opened this issue Jan 23, 2021 · 21 comments · Fixed by #487
Closed

Add support for multiple/runtime-specific versions of ktlint #35

IvanPizhenko opened this issue Jan 23, 2021 · 21 comments · Fixed by #487
Labels
enhancement New feature or request
Milestone

Comments

@IvanPizhenko
Copy link

I have a code like this:

  val someVar = when (ctx.path()) {
      "/some/path/here" -> {
        println("hello")
      }
      else -> ctx.attribute<AttributeClassName>(Attributes.ATTRIBUTE_IDENTIFIER)
        ?: throw MyException("some error message")
    }
    println(someVar.toString())

IDE with installed ktlint plugin is happy with this code, i.e. doesn't put red marks.
But when I run my Gradle build, ktlint check compains at the same code:

SourceFileName.kt:123:13: Missing newline after "->"

I'm expecting that should plugin also detect this inconsistency, but actually it is not.

@IvanPizhenko
Copy link
Author

IvanPizhenko commented Jan 23, 2021

Here is copy of my editorconfig (uploading as txt because github doesn't want to accept Linux hidden file attachment):
editorconfig.txt

@IvanPizhenko IvanPizhenko changed the title ktlint pluging is happy with "when-else" code which ktlint then is not happy with ktlint plugin is happy with "else" in the "when" code which ktlint then is not happy with during build Jan 23, 2021
@IvanPizhenko
Copy link
Author

Here are some more IntelliJ configs (uploading as txt because github doesn't want to accept xml attachment):
codeStyleConfig.xml.txt
Project.xml.txt
ktlint.xml.txt

@nbadal
Copy link
Owner

nbadal commented Jan 23, 2021

Thanks for flagging this and for the sample files! I'll take a look today 😄

@nbadal
Copy link
Owner

nbadal commented Jan 23, 2021

@IvanPizhenko is it possible this is caused by #28 ? The workaround / way to test this would be to explicitly set the folder for your .editorconfig in the plugin settings.

I wasn't able to reproduce that ktlint error with a short example, but that message comes from the indent rule, so maybe it only gets triggered under more complex circumstances 🤔

@IvanPizhenko
Copy link
Author

IvanPizhenko commented Jan 23, 2021

@nbadal Yes, I've faced #28, but I've configured explicit path to the editorconfig, you can see <editorConfigPath>$PROJECT_DIR$</editorConfigPath> in the attached ktlint.xml. So this one is already mitigated. I have the plugin generally working, but in this particular situation it doesn't work correctly.

@IvanPizhenko
Copy link
Author

I will try to make it reproduced in a small sample project.

@IvanPizhenko
Copy link
Author

Here is sample project https://github.com/IvanPizhenko/kotlin-sample-project

@IvanPizhenko
Copy link
Author

IvanPizhenko commented Jan 24, 2021

Look here https://github.com/IvanPizhenko/kotlin-sample-project/blob/bb95c355aa5e78af4b2744b0d70fb080bc5c2282/module1/src/main/kotlin/org/example/Controller.kt#L15
In the IDE I don't see any "red signs". But when I run ./gradlew build ktlint error appears.

@nbadal
Copy link
Owner

nbadal commented Jan 24, 2021

Awesome, thank you! I'll make sure to distill this down into a test case as well once I've got that set up 😁

Looking into it now, then I'll publish a fix in 0.7.1 hopefully 🤞

@nbadal
Copy link
Owner

nbadal commented Jan 24, 2021

I think this may be due to the gradle ktlint plugin using ktlint 0.39.0, and my plugin using 0.40.0 – I'll have to verify this tomorrow morning, but using ktlint v0.40.0 on the command line directly also doesn't show the error that the gradle plugin does.

This might be fixed by pinterest/ktlint#901 ? Seems like a relevant entry in the 0.40.0 changelog
I'll get my hands on 0.39.0 in the morning to double check though!

@IvanPizhenko
Copy link
Author

IvanPizhenko commented Jan 24, 2021

Tried setting ktlint version to 0.40.0 in the gradle - it worked.
But what we really need is to be able to set the same version of ktlint in the both gradle and IntelliJ plugin.
Can you please add such possibility? It could be this way: a checkbox and edit field in the plugin configuration page, like this
configuration
if checkbox is checked, specific version is used, otherwise - whatever default version you apply (I assume it checks for the latest version). This configuration should be saved in the ktlint.xml.

@nbadal
Copy link
Owner

nbadal commented Jan 24, 2021

That would be a nice feature – albeit a bit trickier to implement within the confines of the IDE plugin, but it might be possible!

I'll add it to the roadmap 😄

@nbadal nbadal changed the title ktlint plugin is happy with "else" in the "when" code which ktlint then is not happy with during build No way to specify prior versions of ktlint (e.g. 0.39.0) Jan 24, 2021
@nbadal nbadal added the enhancement New feature or request label Jan 24, 2021
@alexfacciorusso
Copy link

@nbadal If you wouldn't mind, I could try finding some spare time to develop this feature and contribute to the plugin? I don't want to step on anyone else's toes if someone is already on it!

@nbadal
Copy link
Owner

nbadal commented Jul 12, 2021

@alexfacciorusso absolutely, take a stab at it!

@nbadal nbadal changed the title No way to specify prior versions of ktlint (e.g. 0.39.0) Add support for multiple/runtime-specific versions of ktlint Nov 18, 2022
@erikhuizinga
Copy link

I'm also interested in this feature. Until then, this plugin is probably not very UX friendly for me.

I specifically use https://github.com/JLLeitschuh/ktlint-gradle to simply run ktlint as Gradle tasks (e.g. ktlintCheck, ktlintFormat). If this IDEA plugin would allow setting the ktlint version or executable location, and if the ktlint Gradle plugin declares where its executable is located, then I could bridge the gap.

Wishful thinking: have your plugin detect the Gradle plugin and use its executable, even if it later updates to a newer version.

@IvanPizhenko
Copy link
Author

Any chance to have it in the nearest next version?

@nbadal
Copy link
Owner

nbadal commented Mar 14, 2023

I haven't heard any progress from @alexfacciorusso - I can start to take a look at this again

@takanuva15
Copy link

takanuva15 commented Apr 2, 2023

I specifically use https://github.com/JLLeitschuh/ktlint-gradle to simply run ktlint as Gradle tasks (e.g. ktlintCheck, ktlintFormat). If this IDEA plugin would allow setting the ktlint version or executable location, and if the ktlint Gradle plugin declares where its executable is located, then I could bridge the gap.

Lol I read this and got a smile since I'm in the same boat. I maintain a different IntelliJ plugin that uses Kotlin and I use the same org.jlleitschuh.gradle.ktlint plugin for linting my kotlin files. By default, it's using ktlint v47 but this plugin uses ktlint v48 so they're out-of-sync. Would be nice to be able to specify the ktlint version as well.

@nbadal Could you point me to the class(es) I should look at which determine the version of ktlint that's downloaded? I've done plugin and UI work with IntelliJ's platform before so I could try poking around to see if I can make a custom version box of some kind work.

@paul-dingemans
Copy link
Collaborator

I have had a look at how this is done in ktlint-gradle plugin. My Gradle knowledge is too limited to get this working.

paul-dingemans added a commit that referenced this issue Mar 19, 2024
The StandardRulesetProvider from each supported version of ktlint is transformed with shadow plugin to a unique class so that multiple versions of the StandardRulesetProvider class can be compiled into the plugin.

Closes #426
Closes #35
@paul-dingemans
Copy link
Collaborator

Next version of plugin supports ktlint versions 0.50.0, 1.0.1, 1.1.1 and 1.2.1.

@paul-dingemans paul-dingemans added this to the 0.22.0 milestone Mar 19, 2024
@paul-dingemans paul-dingemans removed the help wanted Extra attention is needed label Mar 19, 2024
paul-dingemans added a commit that referenced this issue Apr 7, 2024
* Support multiple versions of ktlint standard ruleset

The StandardRulesetProvider from each supported version of ktlint is transformed with shadow plugin to a unique class so that multiple versions of the StandardRulesetProvider class can be compiled into the plugin.

Closes #426
Closes #35

* Increase build memory

(cherry picked from commit 516955b)
@paul-dingemans
Copy link
Collaborator

This feature will be postponed. Current solution has unwanted side effect that size of plugin increases with 64MB per version of ktlint rules included. See #498.

@paul-dingemans paul-dingemans reopened this Apr 7, 2024
@paul-dingemans paul-dingemans modified the milestones: 0.22.0, 0.23.0 Apr 7, 2024
paul-dingemans added a commit that referenced this issue Apr 10, 2024
* Support multiple versions of ktlint standard ruleset

The StandardRulesetProvider from each supported version of ktlint is transformed with shadow plugin to a unique class so that multiple versions of the StandardRulesetProvider class can be compiled into the plugin.

Closes #426
Closes #35

* Increase build memory

(cherry picked from commit 516955b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants