diff --git a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt index f6fd1106..3178c5f9 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt @@ -1820,9 +1820,9 @@ class KotlinInputAstVisitor( * Example `context(logger: Logger, raise: Raise)` * * Note this also supports the legacy receiver format of `context(Logger, Raise)` for - * backward compatibility. + * backward compatibility and for function types */ - override fun visitContextReceiverList(contextReceiverList: KtContextReceiverList) { + private fun handleContextReceiverList(contextReceiverList: KtContextReceiverList) { builder.sync(contextReceiverList) builder.token("context") visitEachCommaSeparated( @@ -1832,6 +1832,10 @@ class KotlinInputAstVisitor( breakAfterPrefix = false, breakBeforePostfix = false, ) + } + + override fun visitContextReceiverList(contextReceiverList: KtContextReceiverList) { + handleContextReceiverList(contextReceiverList) builder.forcedBreak() } @@ -2465,7 +2469,10 @@ class KotlinInputAstVisitor( override fun visitFunctionType(type: KtFunctionType) { builder.sync(type) - type.contextReceiverList?.let { visitContextReceiverList(it) } + type.contextReceiverList?.let { + handleContextReceiverList(it) + builder.space() + } val receiver = type.receiver if (receiver != null) { diff --git a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt index dbe23e0f..b9eb263f 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt @@ -8571,9 +8571,7 @@ class FormatterTest { | | fun testSuspend( | mock: T, - | block: - | suspend context(SomeContext) - | T.() -> Unit, + | block: suspend context(SomeContext) T.() -> Unit, | ) = startCoroutine { T.block() } |} |""" @@ -8629,9 +8627,7 @@ class FormatterTest { | | fun testSuspend( | mock: T, - | block: - | suspend context(someContext: SomeContext) - | T.() -> Unit, + | block: suspend context(someContext: SomeContext) T.() -> Unit, | ) = startCoroutine { T.block() } |} |"""