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

Formatting with many generics #295

Closed
PaulWoitaschek opened this issue Oct 15, 2018 · 4 comments
Closed

Formatting with many generics #295

PaulWoitaschek opened this issue Oct 15, 2018 · 4 comments

Comments

@PaulWoitaschek
Copy link
Contributor

I have a function with a lot of generics. I can't make intellij autoformat it in a way t hat ktlint stops complaining.
I installed the settings for intellij ktlint --apply-to-idea-project --android . However it complains about the intendation.

package my.package

import io.reactivex.Observable
import io.reactivex.ObservableSource

inline fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any, T5 : Any, T6 : Any, T7 : Any, T8 : Any, T9 : Any, T10 : Any, T11 : Any, T12 : Any,
  T13 : Any, T14 : Any, R : Any>
  combineLatest(
  source1: ObservableSource<T1>,
  source2: ObservableSource<T2>,
  source3: ObservableSource<T3>,
  source4: ObservableSource<T4>,
  source5: ObservableSource<T5>,
  source6: ObservableSource<T6>,
  source7: ObservableSource<T7>,
  source8: ObservableSource<T8>,
  source9: ObservableSource<T9>,
  source10: ObservableSource<T10>,
  source11: ObservableSource<T11>,
  source12: ObservableSource<T12>,
  source13: ObservableSource<T13>,
  source14: ObservableSource<T14>,
  crossinline combiner: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) -> R
): Observable<R> {
  return Observable.combineLatest<Any, R>(
    arrayOf(
      source1,
      source2,
      source3,
      source4,
      source5,
      source6,
      source7,
      source8,
      source9,
      source10,
      source11,
      source12,
      source13,
      source14
    )
  ) {
    @Suppress("UNCHECKED_CAST")
    combiner(
      it[0] as T1,
      it[1] as T2,
      it[2] as T3,
      it[3] as T4,
      it[4] as T5,
      it[5] as T6,
      it[6] as T7,
      it[7] as T8,
      it[8] as T9,
      it[9] as T10,
      it[10] as T11,
      it[11] as T12,
      it[12] as T13,
      it[13] as T14
    )
  }
}

If you run ktlint, it fails:

/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:9:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:10:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:11:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:12:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:13:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:14:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:15:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:16:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:17:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:18:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:19:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:20:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:21:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:22:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:23:3: Unexpected indentation (expected 4, actual 2)
/home/ph1b/Dev/Yazio/features/rx/src/main/java/com/yazio/android/rx/T.kt:24:1: Unexpected indentation (expected 2, actual 0)
@shyiko
Copy link
Collaborator

shyiko commented Oct 16, 2018

Yeah, unless combineLatest is kept on the same line as <T1 : Any, ..., R : Any> IntelliJ appears to format things differently. Two options until it's fixed:

  • move combineLatest to the prev line (unless I'm missing something "TN : Any" can be simplified to TN)
inline fun  <T1, ..., R> combineLatest(
	...
  • or
/* ktlint-disable parameter-list-wrapping */
inline fun <T1 : Any, ...>
	combineLatest(
		...

@PaulWoitaschek
Copy link
Contributor Author

Two options until it's fixed

Who fixes this? Is this a ktlint or an intellij issue?

unless I'm missing something "TN : Any" can be simplified to TN

I explicitly want the upper bound to be of type Any.
combineLatest<Observable<Int?>>... should fail to compile.

@shyiko
Copy link
Collaborator

shyiko commented Oct 16, 2018

Who fixes this? Is this a ktlint or an IntelliJ issue?

Ideally, IntelliJ would allow

inline fun <
  T1 : Any, 
  T2 : Any, 
  ...
> combineLatest(
  source1: ObservableSource<T1>,
  source2: ObservableSource<T2>,
  ...
) {
  ...
}

In this case, no changes would be needed in ktlint.

We could add a workaround on ktlint side to accept

inline fun <T1 : Any, ...>
  combineLatest(
  source1: ObservableSource<T1>,
  ...

but, personally, I don't think that's a good idea. It's inconsistent at best.

I explicitly want the upper bound to be of type Any.

Got it.

P.S. there is a way to force IntelliJ to skip reformatting certain regions - https://stackoverflow.com/a/23485817/482225.

@romtsn
Copy link
Collaborator

romtsn commented Jun 11, 2020

Not sure if this is still an issue with the new indentation rule promoted from experimental, and just landed #769, I will close this for now, feel free to reopen if it's still a problem

@romtsn romtsn closed this as completed Jun 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants