Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Print backticks when identifier should not be printed as is #21381

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -32,6 +32,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 @@ -48,7 +49,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 @@ -205,6 +208,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 @@ -1138,7 +1138,7 @@ class CompletionSuite extends BaseCompletionSuite:
| scala@@
|}
|""".stripMargin,
"""|scala <root>
"""|scala `<root>`
|""".stripMargin
)

Expand Down Expand Up @@ -1725,8 +1725,8 @@ class CompletionSuite extends BaseCompletionSuite:
check(
"""|import @@
|""".stripMargin,
"""|java <root>
|javax <root>
"""|java `<root>`
|javax `<root>`
|""".stripMargin,
filter = _.startsWith("java")
)
Expand All @@ -1744,8 +1744,8 @@ class CompletionSuite extends BaseCompletionSuite:
check(
"""|export @@
|""".stripMargin,
"""|java <root>
|javax <root>
"""|java `<root>`
|javax `<root>`
|""".stripMargin,
filter = _.startsWith("java")
)
Expand Down Expand Up @@ -2090,7 +2090,7 @@ class CompletionSuite extends BaseCompletionSuite:
|""".stripMargin
)

@Test def `conflict-3` =
@Test def `conflict-3` =
check(
"""|package a
|object A {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,76 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
|""".stripMargin
)

@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)
|}
|object O{
| val <<foo>> = A.`Foo-Foo`(1)
|}""".stripMargin,
"""|import A.`Foo-Foo`
|object A{
| case class `Foo-Foo`(i: Int)
|}
|object O{
| 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
)

@Test def `literal-types1` =
checkEdit(
"""|object O {
Expand Down
Loading