Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,76 +598,74 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
)

@Test def `backticks-4` =
checkEdit(
"""|case class `Foo-Foo`(i: Int)
|object O{
| val <<foo>> = `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 <<foo>> = A.`Foo-Foo`(1)
| val <<foo>> = `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 <<foo>> = 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 <<foo>> = 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 <<foo>> = 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 <<foo>> = 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(
Expand Down
Loading