diff --git a/presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala b/presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala index b66fbe56fb9b..804ff21604c0 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala @@ -31,6 +31,7 @@ import dotty.tools.pc.Params import dotty.tools.pc.utils.InteractiveEnrichments.* import org.eclipse.lsp4j.TextEdit +import scala.meta.internal.mtags.KeywordWrapper /** * A type printer that shortens types by replacing fully qualified names with shortened versions. @@ -47,7 +48,9 @@ class ShortenedTypePrinter( )(using indexedCtx: IndexedContext, reportCtx: ReportContext) extends RefinedPrinter(indexedCtx.ctx): private val missingImports: mutable.Set[ImportSel] = mutable.LinkedHashSet.empty private val defaultWidth = 1000 - + private val keywordWrapper = new KeywordWrapper { + override def keywords: Set[String] = KeywordWrapper.Scala3HardKeywords + } private val methodFlags = Flags.commonFlags( Private, @@ -209,6 +212,12 @@ class ShortenedTypePrinter( case tp if tp.isError => super.toText(indexedCtx.ctx.definitions.AnyType) case _ => super.toText(tp) + override def nameString(name: Name): String = + val nameStr = super.nameString(name) + if nameStr.nonEmpty then + keywordWrapper.backtickWrap(nameStr) + else nameStr + override def toTextSingleton(tp: SingletonType): Text = tp match case ConstantType(const) => toText(const) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala index a96dd78be138..0b576e0f5b39 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala @@ -598,76 +598,74 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite: ) @Test def `backticks-4` = - checkEdit( - """|case class `Foo-Foo`(i: Int) - |object O{ - | val <> = `Foo-Foo`(1) - |}""".stripMargin, - """|case class `Foo-Foo`(i: Int) - |object O{ - | val foo: `Foo-Foo` = `Foo-Foo`(1) - |} - |""".stripMargin - ) - - - @Test def `backticks-5` = checkEdit( - """|object A{ - | case class `Foo-Foo`(i: Int) - |} + """|case class `Foo-Foo`(i: Int) |object O{ - | val <> = A.`Foo-Foo`(1) + | val <> = `Foo-Foo`(1) |}""".stripMargin, - """|import A.`Foo-Foo` - |object A{ - | case class `Foo-Foo`(i: Int) - |} + """|case class `Foo-Foo`(i: Int) |object O{ - | val foo: `Foo-Foo` = A.`Foo-Foo`(1) + | val foo: `Foo-Foo` = `Foo-Foo`(1) |} |""".stripMargin ) - - @Test def `backticks-6` = + @Test def `backticks-5` = checkEdit( """|object A{ - | case class `Foo-Foo`[A](i: A) + | case class `Foo-Foo`(i: Int) |} |object O{ | val <> = A.`Foo-Foo`(1) |}""".stripMargin, """|import A.`Foo-Foo` |object A{ - | case class `Foo-Foo`[A](i: A) + | case class `Foo-Foo`(i: Int) |} |object O{ - | val foo: `Foo-Foo`[Int] = A.`Foo-Foo`(1) + | val foo: `Foo-Foo` = A.`Foo-Foo`(1) |} |""".stripMargin - ) + ) + + @Test def `backticks-6` = + checkEdit( + """|object A{ + | case class `Foo-Foo`[A](i: A) + |} + |object O{ + | val <> = A.`Foo-Foo`(1) + |}""".stripMargin, + """|import A.`Foo-Foo` + |object A{ + | case class `Foo-Foo`[A](i: A) + |} + |object O{ + | val foo: `Foo-Foo`[Int] = A.`Foo-Foo`(1) + |} + |""".stripMargin + ) @Test def `backticks-7` = checkEdit( - """|object A{ - | class `x-x` - | case class Foo[A](i: A) - |} - |object O{ - | val <> = A.Foo(new A.`x-x`) - |}""".stripMargin, - """|import A.Foo - |import A.`x-x` - |object A{ - | class `x-x` - | case class Foo[A](i: A) - |} - |object O{ - | val foo: Foo[`x-x`] = A.Foo(new A.`x-x`) - |} - |""".stripMargin - ) + """|object A{ + | class `x-x` + | case class Foo[A](i: A) + |} + |object O{ + | val <> = A.Foo(new A.`x-x`) + |}""".stripMargin, + """|import A.Foo + |import A.`x-x` + |object A{ + | class `x-x` + | case class Foo[A](i: A) + |} + |object O{ + | val foo: Foo[`x-x`] = A.Foo(new A.`x-x`) + |} + |""".stripMargin + ) @Test def `literal-types1` = checkEdit(