Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ktlint 0.42.0 reporting incorrect indentation, and format produced bad file #1189

Closed
ibevilinc opened this issue Jul 29, 2021 · 7 comments · Fixed by #1190
Closed

ktlint 0.42.0 reporting incorrect indentation, and format produced bad file #1189

ibevilinc opened this issue Jul 29, 2021 · 7 comments · Fixed by #1190

Comments

@ibevilinc
Copy link

Expected Behavior

ktlint 0.42.0 should not report invalid indentation results

Observed Behavior

ktlint 0.42.0 is reporting invalid indentation results.
The invalid indentation results also cause an improperly formatted file to be produced if ran with --format
Problem is reproducible using gradle as well as via command-line
This did not occur with version 0.41.0

Steps to Reproduce

  1. Save sample below to a file
fun lazyList(): Lazy<List<String>> =
    lazy { mutableListOf<String>() }

class KtlintError {

    val aListByGood:
        List<String> by lazyList()

    val aListByBad: List<String>
        by lazyList()

    val aVar = 0
}
  1. Run ktlint from command line
ktlint --relative KtlintError.kt
KtlintError.kt:12:1: Unexpected indentation (4) (should be 8)
KtlintError.kt:13:1: Unexpected indentation (0) (should be 4)
KtlintError.kt:14:1: Unexpected indentation (0) (should be 4)
  1. Running ktlint --format results in an incorrectly indented file (notice last variable and closing brace)
fun lazyList(): Lazy<List<String>> =
    lazy { mutableListOf<String>() }

class KtlintError {

    val aListByGood:
        List<String> by lazyList()

    val aListByBad: List<String>
        by lazyList()

        val aVar = 0
    }

Your Environment

  • Version of ktlint used: 0.42.0
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): id("org.jlleitschuh.gradle.ktlint") version "10.1.0"
  • Version of Gradle used (if applicable): 7.1.1
  • Operating System and version: OSX 11.5.1
@Kernald
Copy link

Kernald commented Jul 31, 2021

I also have a situation where ktlint reports an unexpected indentation of 0, should be -2 (with a 2 spaces indent size). Same, worked fine in 0.41, regressed in 0.42.

@alejandrorosas
Copy link

alejandrorosas commented Aug 2, 2021

It happens something similar with parameters with lambdas:

Correct:

    AppTheme {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = {
                        Text(text = "Feature Template")
                    },
                    navigationIcon = {
                        IconButton(onClick = { onNavigateBack() }) {
                            Icon(Icons.Filled.ArrowBack, contentDescription = null)
                        }
                    },
                )
            },
            floatingActionButton = {
                FloatingActionButton(onClick = { }) {
                    IconButton(onClick = { featureTemplateViewModel.onCounterClick() }) {
                        Icon(Icons.Filled.Add, contentDescription = null)
                    }
                }
            },
        ) {
            ScreenContent(counter)
        }
    }

Running ktlint --format:

    AppTheme {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = {
                        Text(text = "Feature Template")
                    },
                    navigationIcon = {
                        IconButton(onClick = { onNavigateBack() }) {
                        Icon(Icons.Filled.ArrowBack, contentDescription = null)
                    }
                    },
                )
            },
            floatingActionButton = {
                FloatingActionButton(onClick = { }) {
                IconButton(onClick = { featureTemplateViewModel.onCounterClick() }) {
                Icon(Icons.Filled.Add, contentDescription = null)
            }
            }
            },
        ) {
            ScreenContent(counter)
        }
    }

alejandrorosas/android-app-template#152

@ZacSweers
Copy link

We see this issue a lot with non-trailing lambda parameters too, like RxJava subscribe sites

userRepositoryLazy.get().getUser(userId)
  .subscribeOn(Schedulers.io())
  .observeOn(SlackSchedulers.immediateMainThread())
  .subscribe(
    {
      viewHolder.destination.setMessagingChannelAsDestination(
        UserUtils.getDisplayName(prefsManagerLazy.get(), it),
        isDraftInThread
      )
    },
    Observers.consumerErrorLogger()
  )

@vRallev
Copy link

vRallev commented Aug 3, 2021

Another example in case it helps. That's what KtLint wants:

  @Test fun filter_returns_empty_optional_if_false() {
    val optional = Optional.of("")

    with(optional.filter { true }) {
    assertThat(this).isEqualTo(optional)
    assertThat(this.isPresent).isTrue()
  }

    with(optional.filter { false }) {
    assertThat(this).isNotEqualTo(optional)
    assertThat(this.isPresent).isFalse()
  }
  }

@shashachu
Copy link
Contributor

I'm planning on a 0.42.1 release since this was a regression introduced to 0.42.

@ZacSweers
Copy link

ZacSweers commented Aug 6, 2021

I'm still seeing this issue widespread in 0.42.1, it's not just with delegated properties. Here's one example

class SomeTest {
  fun someTest() {
    assertThat(someList)
      .containsExactly(
        0,
        9L, // windows 4 to 11, plus implied windows 12 & 13
        60,
        2L, // windows 2 & 3
        11,
        1L, // window 1
        14,
        1L // window 0
      )
  }
}

Which reports

Unexpected indentation (expected 6, actual 8)

It seems to happen most often with multi-line parameters to a function call that is itself already on a continuation indent

@jakoss
Copy link

jakoss commented Oct 1, 2021

I do agree with @ZacSweers , we have this in random compose code places. This does not seem to be fixed

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants