Skip to content

Commit

Permalink
bugfix: Document highlight on class constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk committed Dec 6, 2023
1 parent fab3f21 commit 33011d9
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
37 changes: 37 additions & 0 deletions presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ 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 @@ -403,6 +419,22 @@ abstract class PcCollector[T](
ident.sourcePos
)
else occurrences
/**
* 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
occurrences + collect(
sel,
namePos(t),
Some(sel.symbol.owner),
)
else occurrences
/**
* All select statements such as:
* val a = hello.<<b>>
Expand Down Expand Up @@ -564,6 +596,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 @@ -1185,3 +1185,97 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
|val a = MyIntOut(1) <<+@@+>> 3
|""".stripMargin,
)

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

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

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

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

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

@Test def `constructor5` =
check(
"""
|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
)

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

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

@Test def `constructor8` =
check(
"""
|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 @@ -375,3 +375,40 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
|end <<foo>>/*method,definition*/
|""".stripMargin,
)

@Test def `constructor` =
check(
"""
|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
)

@Test def `constructor1` =
check(
"""
|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
)

@Test def `constructor2` =
check(
"""
|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 33011d9

Please sign in to comment.