From 98c4bd5c0c48c0e1519a1ea9f5f646b2b1bdcf14 Mon Sep 17 00:00:00 2001 From: rochala Date: Thu, 9 Oct 2025 21:47:05 +0200 Subject: [PATCH] Fix one off error in StringInterpolator completions --- .../handlers/ScalaInsertHandler.scala | 3 +- ...calaStringInterpolatorCompletionTest.scala | 96 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 scala/scala-impl/test/org/jetbrains/plugins/scala/lang/completion3/ScalaStringInterpolatorCompletionTest.scala diff --git a/scala/scala-impl/src/org/jetbrains/plugins/scala/lang/completion/handlers/ScalaInsertHandler.scala b/scala/scala-impl/src/org/jetbrains/plugins/scala/lang/completion/handlers/ScalaInsertHandler.scala index d1143576b97..839aede49f0 100644 --- a/scala/scala-impl/src/org/jetbrains/plugins/scala/lang/completion/handlers/ScalaInsertHandler.scala +++ b/scala/scala-impl/src/org/jetbrains/plugins/scala/lang/completion/handlers/ScalaInsertHandler.scala @@ -90,6 +90,7 @@ object ScalaInsertHandler { .getStartOffset document.insertString(tailOffset, "}") + // This looks like a bug, when insertString(offset) param is equal to context.currentOffset it does not increment document.insertString(startOffset, "{") document.insertString(literalOffset, "s") context.commitDocument() @@ -103,7 +104,7 @@ object ScalaInsertHandler { val element = context .getFile - .findElementAt(context.getStartOffset + 2) + .findElementAt(context.getStartOffset + 1) // There is a bug in Platform SDK in which context offset is not updated when the insertOffset is the same as current offset. val maybeBlock = element.getNode.getElementType match { case `tIDENTIFIER` => diff --git a/scala/scala-impl/test/org/jetbrains/plugins/scala/lang/completion3/ScalaStringInterpolatorCompletionTest.scala b/scala/scala-impl/test/org/jetbrains/plugins/scala/lang/completion3/ScalaStringInterpolatorCompletionTest.scala new file mode 100644 index 00000000000..8b75ab36e56 --- /dev/null +++ b/scala/scala-impl/test/org/jetbrains/plugins/scala/lang/completion3/ScalaStringInterpolatorCompletionTest.scala @@ -0,0 +1,96 @@ +package org.jetbrains.plugins.scala.lang.completion3 + +import org.jetbrains.plugins.scala.lang.completion3.base.ScalaCompletionTestBase + +class ScalaStringInterpolatorCompletionTest extends ScalaCompletionTestBase { + + def testBracesBasic(): Unit = doCompletionTest( + fileText = + s""" + |object A { + | val zzz = "" + | val y = "$$z$CARET" + |} + """.stripMargin, + resultText = + s""" + |object A { + | val zzz = "" + | val y = s"$$zzz$CARET" + |} + """.stripMargin, + item = "zzz", + ) + + def testBracesBasicEmpty(): Unit = doCompletionTest( + fileText = + s""" + |object A { + | val zzz = "" + | val y = "$$$CARET" + |} + """.stripMargin, + resultText = + s""" + |object A { + | val zzz = "" + | val y = s"$$zzz$CARET" + |} + """.stripMargin, + item = "zzz", + ) + + def testBracesSingleLetter(): Unit = doCompletionTest( + fileText = + s""" + |object A { + | val z = "" + | val y = "$$z$CARET" + |} + """.stripMargin, + resultText = + s""" + |object A { + | val z = "" + | val y = s"$$z$CARET" + |} + """.stripMargin, + item = "z", + ) + + def testBracesSingleLetterEmpty(): Unit = doCompletionTest( + fileText = + s""" + |object A { + | val z = "" + | val y = "$$$CARET" + |} + """.stripMargin, + resultText = + s""" + |object A { + | val z = "" + | val y = s"$$z$CARET" + |} + """.stripMargin, + item = "z", + ) + + def testAddBracesWhenRequiredForMethods(): Unit = doCompletionTest( + fileText = + s""" + |object A { + | def zzz(a: String): String = "" + | val y = "$$$CARET" + |} + """.stripMargin, + resultText = + s""" + |object A { + | def zzz(a: String): String = "" + | val y = s"$${zzz($CARET)}" + |} + """.stripMargin, + item = "zzz", + ) +}