From 1067e62b5ff91bdd22fd1f76a3c0da393eda98f8 Mon Sep 17 00:00:00 2001 From: Timur Aliberdov Date: Mon, 13 Jan 2025 17:08:35 +0100 Subject: [PATCH] [add type annotation] wrap Scala 3 fewer braces param in parentheses #SCL-23440 fixed --- .../intention/types/AddOnlyStrategy.scala | 2 +- .../ToggleTypeAnnotationIntentionTest_Scala3.scala | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/scala/scala-impl/src/org/jetbrains/plugins/scala/codeInsight/intention/types/AddOnlyStrategy.scala b/scala/scala-impl/src/org/jetbrains/plugins/scala/codeInsight/intention/types/AddOnlyStrategy.scala index 3aa77eb367d..d3b06ac583e 100644 --- a/scala/scala-impl/src/org/jetbrains/plugins/scala/codeInsight/intention/types/AddOnlyStrategy.scala +++ b/scala/scala-impl/src/org/jetbrains/plugins/scala/codeInsight/intention/types/AddOnlyStrategy.scala @@ -243,7 +243,7 @@ object AddOnlyStrategy { anchor match { case p: ScParameter => val parameter = p.getParent match { - case Parent(Parent(Parent(_: ScBlockExpr))) => p + case Parent(Parent(Parent(block: ScBlockExpr))) if !block.isEnclosedByColon => p // ensure that the parameter is wrapped in parentheses before we add the type annotation. case clause: ScParameterClause if clause.parameters.length == 1 => clause.replace(createClauseForFunctionExprFromText(p.getText.parenthesize(), clause)) diff --git a/scala/scala-impl/test/org/jetbrains/plugins/scala/codeInsight/intention/types/ToggleTypeAnnotationIntentionTest_Scala3.scala b/scala/scala-impl/test/org/jetbrains/plugins/scala/codeInsight/intention/types/ToggleTypeAnnotationIntentionTest_Scala3.scala index 925333594b7..2af2ac3fd92 100644 --- a/scala/scala-impl/test/org/jetbrains/plugins/scala/codeInsight/intention/types/ToggleTypeAnnotationIntentionTest_Scala3.scala +++ b/scala/scala-impl/test/org/jetbrains/plugins/scala/codeInsight/intention/types/ToggleTypeAnnotationIntentionTest_Scala3.scala @@ -46,4 +46,17 @@ final class ToggleTypeAnnotationIntentionTest_Scala3 extends ToggleTypeAnnotatio |} |""".stripMargin ) + + def testAddTypeToFewerBracesParameter(): Unit = doTest( + s""" + |class Example: + | Seq(1, 2, 3).foreach: x$caretTag => + | println(x) + |""".stripMargin, + s""" + |class Example: + | Seq(1, 2, 3).foreach: (x: Int)$caretTag => + | println(x) + |""".stripMargin, + ) }