Skip to content

Commit

Permalink
bugfix: Document highlight on generic class init in scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk committed Nov 24, 2023
1 parent 9042ec9 commit 6e7ebb5
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
29 changes: 29 additions & 0 deletions mtags/src/main/scala-3/scala/meta/internal/pc/PcCollector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ abstract class PcCollector[T](
if id.symbol
.is(Flags.Param) && id.symbol.owner.is(Flags.ExtensionMethod) =>
Some(findAllExtensionParamSymbols(id.sourcePos, id.name, id.symbol))
/**
* Workaround for missing symbol in:
* class A[T](a: T)
* val x = new <<A>>(1)
*/
case (id: Ident) :: (_: New) :: (sel: Select) :: _
if id.symbol == NoSymbol && sel.symbol.isConstructor =>
Some(symbolAlternatives(sel.symbol.owner), id.sourcePos)
case (sel @ Select(New(_: Ident), _)) :: (_: TypeApply) :: _
if sel.symbol.isConstructor =>
Some(symbolAlternatives(sel.symbol.owner), sel.sourcePos)

/* simple identifier:
* val a = val@@ue + value
*/
Expand Down Expand Up @@ -416,6 +428,23 @@ abstract class PcCollector[T](
ident.sourcePos,
)
else occurences

/**
* Workaround for missing symbol in:
* class A[T](a: T)
* val x = new <<A>>(1)
*/
case sel @ Select(New(id: Ident), _)
if sel.span.isCorrect &&
sel.symbol.isConstructor &&
id.symbol == NoSymbol =>
if soughtFilter(_ == sel.symbol.owner) then
occurences + collect(
sel,
pos.withSpan(id.span),
Some(sel.symbol.owner),
)
else occurences
/**
* All select statements such as:
* val a = hello.<<b>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -993,4 +993,74 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite {
|}""".stripMargin
)

check(
"constructor",
"""
|object Main {
| class <<A@@bc>>[T](abc: T)
| val x = new <<Abc>>(123)
|}""".stripMargin
)

check(
"constructor1",
"""
|object Main {
| case class <<Abc>>[T](abc: T)
| val x = <<A@@bc>>(123)
|}""".stripMargin
)

check(
"constructor2",
"""
|object Main {
| class <<A@@bc>>[T](abc: T)
| object <<Abc>>
| val x = new <<Abc>>(123)
|}""".stripMargin
)

check(
"constructor3",
"""
|object Main {
| class <<Abc>>[T](abc: T)
| object <<Abc>>
| val x = new <<A@@bc>>(123)
|}""".stripMargin
)

check(
"constructor4",
"""
|object Main {
| class <<Abc>>[T](abc: T)
| object <<Ab@@c>>
| val x = new <<Abc>>(123)
|}""".stripMargin
)

check(
"constructor5",
"""
|object Main {
| class <<Abc>>[T](abc: T)
| object <<Abc>> {
| def apply(abc: Int, bde: Int) = new <<Abc>>(abc + bde)
| }
| val x = <<Ab@@c>>(123, 456)
|}""".stripMargin
)

check(
"constructor6",
"""
|class <<Abc>>[T](a: T)
|object O {
| def foo(a: Int) = new <<Abc>>[Int](a)
| val x = <<Ab@@c>>[Int](2)
|}""".stripMargin
)

}
41 changes: 41 additions & 0 deletions tests/cross/src/test/scala/tests/tokens/SemanticTokensSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,45 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite {
|}""".stripMargin
)

check(
"constructor",
"""
|object <<Main>>/*class*/ {
| class <<Abc>>/*class*/[<<T>>/*typeParameter,declaration,abstract*/](<<abc>>/*variable,declaration,readonly*/: <<T>>/*typeParameter,abstract*/)
| object <<Abc>>/*class*/
| val <<x>>/*variable,definition,readonly*/ = new <<Abc>>/*class*/(123)
|}""".stripMargin,
compat = Map(
"3" -> """
|object <<Main>>/*class*/ {
| class <<Abc>>/*class*/[<<T>>/*typeParameter,definition,abstract*/](<<abc>>/*variable,declaration,readonly*/: <<T>>/*typeParameter,abstract*/)
| object <<Abc>>/*class*/
| val <<x>>/*variable,definition,readonly*/ = new <<Abc>>/*class*/(123)
|}""".stripMargin
)
)

check(
"constructor1",
"""
|object <<Main>>/*class*/ {
| class <<Abc>>/*class*/[<<T>>/*typeParameter,declaration,abstract*/](<<abc>>/*variable,declaration,readonly*/: <<T>>/*typeParameter,abstract*/)
| object <<Abc>>/*class*/ {
| def <<apply>>/*method,definition*/[<<T>>/*typeParameter,declaration,abstract*/](<<abc>>/*parameter,declaration,readonly*/: <<T>>/*typeParameter,abstract*/, <<bde>>/*parameter,declaration,readonly*/: <<T>>/*typeParameter,abstract*/) = new <<Abc>>/*class*/(<<abc>>/*parameter,readonly*/)
| }
| val <<x>>/*variable,definition,readonly*/ = <<Abc>>/*class*/(123, 456)
|}""".stripMargin,
compat = Map(
"3" ->
"""
|object <<Main>>/*class*/ {
| class <<Abc>>/*class*/[<<T>>/*typeParameter,definition,abstract*/](<<abc>>/*variable,declaration,readonly*/: <<T>>/*typeParameter,abstract*/)
| object <<Abc>>/*class*/ {
| def <<apply>>/*method,definition*/[<<T>>/*typeParameter,definition,abstract*/](<<abc>>/*parameter,declaration,readonly*/: <<T>>/*typeParameter,abstract*/, <<bde>>/*parameter,declaration,readonly*/: <<T>>/*typeParameter,abstract*/) = new <<Abc>>/*class*/(<<abc>>/*parameter,readonly*/)
| }
| val <<x>>/*variable,definition,readonly*/ = <<Abc>>/*class*/(123, 456)
|}""".stripMargin
)
)

}

0 comments on commit 6e7ebb5

Please sign in to comment.