Kotlin / Kotlin/ktfmt

Reformat before commit and partial reformat is not supported from IntelliJ plugin

Open
#573 2 comments 5 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
1.3k
Forks
116
Avg merge
4d 10h
Merged PRs (30d)
15

Description

Ktfmt intellij plugin does not account for line ranges nor declares `Feature.FORMAT_FRAGMENTS` in it's `KtfmtFormattingService` (see [FormattingServiceUtil.findService](https://github.com/JetBrains/intellij-community/blob/72c6c8e4ad498e8ae624b5f019063d79dbdc7acc/platform/code-style-impl/src/com/intellij/formatting/service/FormattingServiceUtil.java#L25)).

As a result, using "reformat before commit", formatting a selected portion of the code or other partial reformatting from the IDE result in formatting that do not comply with Ktfmt, leading to inconsistent states and need for manual reformatting of our repo every once in a while.

Git hooks or full reformat before commit are a workaround but is not convenient (breaks committing only a part of a file's changes to VCS) and requires extra setup and computation.

Reproduction sample:
- Committing with "Reformat code" commit check leads to the formatting below
- Reformatting with Ktfmt changes indents and puts the first `padding` in the same line as `Modifier`
```kotlin
private fun MyComposeFunction() {
Function(
modifier =
Modifier
.padding(vertical = someVerticalPadding())
.padding(vertical = someVerticalPadding())
)
}

fun Function(
modifier: Any,
): Unit = TODO()

fun someVerticalPadding(): Any = TODO()

interface Modifier {
fun padding(vertical: Any): Modifier = TODO()

companion object : Modifier
}
```

Contributor guide

Open the contributing guide

Research direction

Start by inspecting the IntelliJ plugin's KtfmtFormattingService and IntelliJ's FormattingServiceUtil.findService behavior for Feature.FORMAT_FRAGMENTS and line ranges. Reproduce the commit-time and selected-range formatting cases described in the issue, then verify that partial formatting follows Ktfmt's output without breaking full-file formatting.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.