Skip to content

Commit 33011d9

Browse files
committed
bugfix: Document highlight on class constructors
1 parent fab3f21 commit 33011d9

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ abstract class PcCollector[T](
144144
if id.symbol
145145
.is(Flags.Param) && id.symbol.owner.is(Flags.ExtensionMethod) =>
146146
Some(findAllExtensionParamSymbols(id.sourcePos, id.name, id.symbol))
147+
/**
148+
* Workaround for missing symbol in:
149+
* class A[T](a: T)
150+
* val x = new <<A>>(1)
151+
*/
152+
case t :: (n: New) :: (sel: Select) :: _
153+
if t.symbol == NoSymbol && sel.symbol.isConstructor =>
154+
Some(symbolAlternatives(sel.symbol.owner), namePos(t))
155+
/**
156+
* Workaround for missing symbol in:
157+
* class A[T](a: T)
158+
* val x = <<A>>[Int](1)
159+
*/
160+
case (sel @ Select(New(t), _)) :: (_: TypeApply) :: _
161+
if sel.symbol.isConstructor =>
162+
Some(symbolAlternatives(sel.symbol.owner), namePos(t))
147163
/* simple identifier:
148164
* val a = val@@ue + value
149165
*/
@@ -403,6 +419,22 @@ abstract class PcCollector[T](
403419
ident.sourcePos
404420
)
405421
else occurrences
422+
/**
423+
* Workaround for missing symbol in:
424+
* class A[T](a: T)
425+
* val x = new <<A>>(1)
426+
*/
427+
case sel @ Select(New(t), _)
428+
if sel.span.isCorrect &&
429+
sel.symbol.isConstructor &&
430+
t.symbol == NoSymbol =>
431+
if soughtFilter(_ == sel.symbol.owner) then
432+
occurrences + collect(
433+
sel,
434+
namePos(t),
435+
Some(sel.symbol.owner),
436+
)
437+
else occurrences
406438
/**
407439
* All select statements such as:
408440
* val a = hello.<<b>>
@@ -564,6 +596,11 @@ abstract class PcCollector[T](
564596
Span(span.start, span.start + realName.length, point)
565597
else Span(point, span.end, point)
566598
else span
599+
600+
private def namePos(tree: Tree): SourcePosition =
601+
tree match
602+
case sel: Select => sel.sourcePos.withSpan(selectNameSpan(sel))
603+
case _ => tree.sourcePos
567604
end PcCollector
568605

569606
object PcCollector:

presentation-compiler/test/dotty/tools/pc/tests/highlight/DocumentHighlightSuite.scala

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,3 +1185,97 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
11851185
|val a = MyIntOut(1) <<+@@+>> 3
11861186
|""".stripMargin,
11871187
)
1188+
1189+
@Test def `constructor` =
1190+
check(
1191+
"""
1192+
|object Main {
1193+
| class <<A@@bc>>[T](abc: T)
1194+
| val x = new <<Abc>>(123)
1195+
|}""".stripMargin
1196+
)
1197+
1198+
@Test def `constructor1` =
1199+
check(
1200+
"""
1201+
|object Main {
1202+
| case class <<Abc>>[T](abc: T)
1203+
| val x = <<A@@bc>>(123)
1204+
|}""".stripMargin
1205+
)
1206+
1207+
@Test def `constructor2` =
1208+
check(
1209+
"""
1210+
|object Main {
1211+
| class <<A@@bc>>[T](abc: T)
1212+
| object <<Abc>>
1213+
| val x = new <<Abc>>(123)
1214+
|}""".stripMargin
1215+
)
1216+
1217+
@Test def `constructor3` =
1218+
check(
1219+
"""
1220+
|object Main {
1221+
| class <<Abc>>[T](abc: T)
1222+
| object <<Abc>>
1223+
| val x = new <<A@@bc>>(123)
1224+
|}""".stripMargin
1225+
)
1226+
1227+
@Test def `constructor4` =
1228+
check(
1229+
"""
1230+
|object Main {
1231+
| class <<Abc>>[T](abc: T)
1232+
| object <<Ab@@c>>
1233+
| val x = new <<Abc>>(123)
1234+
|}""".stripMargin
1235+
)
1236+
1237+
@Test def `constructor5` =
1238+
check(
1239+
"""
1240+
|object Main {
1241+
| class <<Abc>>[T](abc: T)
1242+
| object <<Abc>> {
1243+
| def apply(abc: Int, bde: Int) = new <<Abc>>(abc + bde)
1244+
| }
1245+
| val x = <<Ab@@c>>(123, 456)
1246+
|}""".stripMargin
1247+
)
1248+
1249+
@Test def `constructor6` =
1250+
check(
1251+
"""
1252+
|class <<Abc>>[T](a: T)
1253+
|object O {
1254+
| def foo(a: Int) = new <<Abc>>[Int](a)
1255+
| val x = <<Ab@@c>>[Int](2)
1256+
|}""".stripMargin
1257+
)
1258+
1259+
@Test def `constructor7` =
1260+
check(
1261+
"""
1262+
|object Bar {
1263+
|class <<Abc>>[T](a: T)
1264+
|}
1265+
|
1266+
|object O {
1267+
| val x = new Bar.<<Ab@@c>>(2)
1268+
|}""".stripMargin
1269+
)
1270+
1271+
@Test def `constructor8` =
1272+
check(
1273+
"""
1274+
|object Bar {
1275+
|class <<Abc>>[T](a: T)
1276+
|}
1277+
|
1278+
|object O {
1279+
| val x = Bar.<<Ab@@c>>[Int](2)
1280+
|}""".stripMargin
1281+
)

presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,40 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
375375
|end <<foo>>/*method,definition*/
376376
|""".stripMargin,
377377
)
378+
379+
@Test def `constructor` =
380+
check(
381+
"""
382+
|object <<Bar>>/*class*/ {
383+
| class <<Abc>>/*class*/[<<T>>/*typeParameter,definition,abstract*/](<<a>>/*variable,declaration,readonly*/: <<T>>/*typeParameter,abstract*/)
384+
|}
385+
|
386+
|object <<O>>/*class*/ {
387+
| val <<x>>/*variable,definition,readonly*/ = new <<Bar>>/*class*/.<<Abc>>/*class*/(2)
388+
| val <<y>>/*variable,definition,readonly*/ = new <<Bar>>/*class*/.<<Abc>>/*class*/[<<Int>>/*class,abstract*/](2)
389+
| val <<z>>/*variable,definition,readonly*/ = <<Bar>>/*class*/.<<Abc>>/*class*/(2)
390+
| val <<w>>/*variable,definition,readonly*/ = <<Bar>>/*class*/.<<Abc>>/*class*/[<<Int>>/*class,abstract*/](2)
391+
|}""".stripMargin
392+
)
393+
394+
@Test def `constructor1` =
395+
check(
396+
"""
397+
|object <<Main>>/*class*/ {
398+
| class <<Abc>>/*class*/[<<T>>/*typeParameter,definition,abstract*/](<<abc>>/*variable,declaration,readonly*/: <<T>>/*typeParameter,abstract*/)
399+
| object <<Abc>>/*class*/
400+
| val <<x>>/*variable,definition,readonly*/ = new <<Abc>>/*class*/(123)
401+
|}""".stripMargin
402+
)
403+
404+
@Test def `constructor2` =
405+
check(
406+
"""
407+
|object <<Main>>/*class*/ {
408+
| class <<Abc>>/*class*/[<<T>>/*typeParameter,definition,abstract*/](<<abc>>/*variable,declaration,readonly*/: <<T>>/*typeParameter,abstract*/)
409+
| object <<Abc>>/*class*/ {
410+
| 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*/)
411+
| }
412+
| val <<x>>/*variable,definition,readonly*/ = <<Abc>>/*class*/(123, 456)
413+
|}""".stripMargin
414+
)

0 commit comments

Comments
 (0)