ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

Another differences between AndroidStudio and Ktlint formatting

Open PPetka opened this issue 6 years ago • 10 comments

The fix on the issue I've posted here https://github.com/pinterest/ktlint/issues/685 indeed fixed a lot of inconsistencies between formatting via ktlint and AndroidStudio, however, I found another bug.

This is the code before formatting, (ktlint sees no issues) Screenshot 2020-06-24 at 10 54 20

After formatting via Android Studio Screenshot 2020-06-24 at 10 54 29

ktlint version 0.37.2

PPetka avatar Jun 24 '20 09:06 PPetka

Found another bug, Idk if, that's the same cause. Should I create separate PR for that? Before: Screenshot 2020-07-21 at 14 39 38 After: Screenshot 2020-07-21 at 14 40 36

PPetka avatar Jul 21 '20 12:07 PPetka

Hello, I have the same issue here, the indents of the if () and the function call are stacking together with Android Studio formatting which I think is normal but ktlint wants only one indent:

Android Studio formatting: image

ktlint formatting: image

AlexWink avatar Jul 30 '20 09:07 AlexWink

Not sure if I should be dropping this here or in a separate issue, imports ordering is also off:

Android Studio

import android.os.Bundle
import android.view.View
import com.honestbank.android.apply.presentation.R
import com.honestbank.android.apply.presentation.common.setFieldListener
import com.honestbank.android.common.presentation.di.ViewModelProvidersWrapper
import com.honestbank.android.common.presentation.livedata.nonNull
import com.honestbank.android.common.presentation.livedata.observe
import com.honestbank.android.common.presentation.view.BaseFragment
import com.honestbank.android.common.presentation.view.updateView
import kotlinx.android.synthetic.main.fragment_apply_email_layout.emailNavigateButton
import kotlinx.android.synthetic.main.fragment_apply_email_layout.emailPassword
import kotlinx.android.synthetic.main.fragment_apply_email_layout.emailText
import kotlinx.android.synthetic.main.fragment_apply_email_layout.load_indicator
import javax.inject.Inject

vs

ktlint 0.37.0

import android.os.Bundle
import android.view.View
import com.honestbank.android.apply.presentation.R
import com.honestbank.android.apply.presentation.common.setFieldListener
import com.honestbank.android.common.presentation.di.ViewModelProvidersWrapper
import com.honestbank.android.common.presentation.livedata.nonNull
import com.honestbank.android.common.presentation.livedata.observe
import com.honestbank.android.common.presentation.view.BaseFragment
import com.honestbank.android.common.presentation.view.updateView
import javax.inject.Inject
import kotlinx.android.synthetic.main.fragment_apply_email_layout.emailNavigateButton
import kotlinx.android.synthetic.main.fragment_apply_email_layout.emailPassword
import kotlinx.android.synthetic.main.fragment_apply_email_layout.emailText
import kotlinx.android.synthetic.main.fragment_apply_email_layout.load_indicator

jai avatar Aug 10 '20 12:08 jai

@jai are you running ktlint with --android option? If so, then it's expected behavior, as per android styleguide the imports should be sorted alphabetically

romtsn avatar Aug 10 '20 13:08 romtsn

thanks @romtsn so for happy coexistence between ktlint and Android Studio I'm going to have to disable the android option it seems?

jai avatar Aug 10 '20 13:08 jai

Either that or you could add kotlin_imports_layout=idea to your .editorconfig as mentioned here, and they will happily coexist even with --android option :)

romtsn avatar Aug 10 '20 14:08 romtsn

@romtsn so I created a .editorconfig file in the project root with the following contents:

[*.{kt,kts}]
kotlin_imports_layout=idea

Running ktlint ended up throwing a bunch of errors:

/android-app/app/src/test/kotlin/com/honestbank/android/model/CampaignTest.kt:0:0: Internal Error (import-ordering). Please create a ticket at https://github.com/pinterest/ktlint/issues (if possible, provide the source code that triggered an error) (cannot be auto-corrected)

I'll try and dig deeper to see what's going on. This is ktlint running via a Gradle plugin so will try the command line ktlint and see if that makes a difference.

jai avatar Aug 10 '20 14:08 jai

This might be a bug (also please try to use 0.37.2 as there were some fixes for the import-ordering rule). If this still isn't working, feel free to open a new issue with code sample

romtsn avatar Aug 10 '20 14:08 romtsn

Looks like still having issues even with 0.37.2. Opened https://github.com/pinterest/ktlint/issues/826

jai avatar Aug 10 '20 15:08 jai

@PPetka the first issue is fixed by #833 but the second one is unfortunately not that easy to fix - please track it in #816

romtsn avatar Aug 21 '20 07:08 romtsn