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 Dec 11, 2023
1 parent 2014643 commit 3d06d9f
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
39 changes: 39 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,23 @@ 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 t :: (n: New) :: (sel: Select) :: _
if t.symbol == NoSymbol && sel.symbol.isConstructor =>
Some(symbolAlternatives(sel.symbol.owner), namePos(t))
/**
* Workaround for missing symbol in:
* class A[T](a: T)
* val x = <<A>>[Int](1)
*/
case (sel @ Select(New(t), _)) :: (_: TypeApply) :: _
if sel.symbol.isConstructor =>
Some(symbolAlternatives(sel.symbol.owner), namePos(t))

/* simple identifier:
* val a = val@@ue + value
*/
Expand Down Expand Up @@ -416,6 +433,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(t), _)
if sel.span.isCorrect &&
sel.symbol.isConstructor &&
t.symbol == NoSymbol =>
if soughtFilter(_ == sel.symbol.owner) then
occurences + collect(
sel,
namePos(t),
Some(sel.symbol.owner),
)
else occurences
/**
* All select statements such as:
* val a = hello.<<b>>
Expand Down Expand Up @@ -600,6 +634,11 @@ abstract class PcCollector[T](
Span(span.start, span.start + realName.length, point)
else Span(point, span.end, point)
else span

private def namePos(tree: Tree): SourcePosition =
tree match
case sel: Select => sel.sourcePos.withSpan(selectNameSpan(sel))
case _ => tree.sourcePos
end PcCollector

object PcCollector:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1044,4 +1044,98 @@ 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".tag(IgnoreScala2),
"""
|class <<Abc>>[T](a: T)
|object O {
| def foo(a: Int) = new <<Abc>>[Int](a)
| val x = <<Ab@@c>>[Int](2)
|}""".stripMargin
)

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

check(
"constructor8".tag(IgnoreScala2),
"""
|object Bar {
|class <<Abc>>[T](a: T)
|}
|
|object O {
| val x = Bar.<<Ab@@c>>[Int](2)
|}""".stripMargin
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,19 @@ class SemanticTokensScala3Suite extends BaseSemanticTokensSuite {
|""".stripMargin
)

check(
"constructor2",
"""
|object <<Bar>>/*class*/ {
| class <<Abc>>/*class*/[<<T>>/*typeParameter,definition,abstract*/](<<a>>/*variable,declaration,readonly*/: <<T>>/*typeParameter,abstract*/)
|}
|
|object <<O>>/*class*/ {
| val <<x>>/*variable,definition,readonly*/ = new <<Bar>>/*class*/.<<Abc>>/*class*/(2)
| val <<y>>/*variable,definition,readonly*/ = new <<Bar>>/*class*/.<<Abc>>/*class*/[<<Int>>/*class,abstract*/](2)
| val <<z>>/*variable,definition,readonly*/ = <<Bar>>/*class*/.<<Abc>>/*class*/(2)
| val <<w>>/*variable,definition,readonly*/ = <<Bar>>/*class*/.<<Abc>>/*class*/[<<Int>>/*class,abstract*/](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 3d06d9f

Please sign in to comment.