Skip to content

Commit 0749565

Browse files
authored
bugfix: highlight for enum type params (#18528)
backport from metals: scalameta/metals@ba67d09 CC: @tgodzik
2 parents 1be790c + e08c70e commit 0749565

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

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

+33-2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,33 @@ abstract class PcCollector[T](
111111
end adjust
112112

113113
def symbolAlternatives(sym: Symbol) =
114+
def member(parent: Symbol) = parent.info.member(sym.name).symbol
115+
def primaryConstructorTypeParam(owner: Symbol) =
116+
for
117+
typeParams <- owner.primaryConstructor.paramSymss.headOption
118+
param <- typeParams.find(_.name == sym.name)
119+
if (param.isType)
120+
yield param
121+
def additionalForEnumTypeParam(enumClass: Symbol) =
122+
if enumClass.is(Flags.Enum) then
123+
val enumOwner =
124+
if enumClass.is(Flags.Case)
125+
then
126+
Option.when(member(enumClass).is(Flags.Synthetic))(
127+
enumClass.maybeOwner.companionClass
128+
)
129+
else Some(enumClass)
130+
enumOwner.toSet.flatMap { enumOwner =>
131+
val symsInEnumCases = enumOwner.children.toSet.flatMap(enumCase =>
132+
if member(enumCase).is(Flags.Synthetic)
133+
then primaryConstructorTypeParam(enumCase)
134+
else None
135+
)
136+
val symsInEnumOwner =
137+
primaryConstructorTypeParam(enumOwner).toSet + member(enumOwner)
138+
symsInEnumCases ++ symsInEnumOwner
139+
}
140+
else Set.empty
114141
val all =
115142
if sym.is(Flags.ModuleClass) then
116143
Set(sym, sym.companionModule, sym.companionModule.companion)
@@ -129,7 +156,11 @@ abstract class PcCollector[T](
129156
) ++ sym.allOverriddenSymbols.toSet
130157
// type used in primary constructor will not match the one used in the class
131158
else if sym.isTypeParam && sym.owner.isPrimaryConstructor then
132-
Set(sym, sym.owner.owner.info.member(sym.name).symbol)
159+
Set(sym, member(sym.maybeOwner.maybeOwner))
160+
++ additionalForEnumTypeParam(sym.maybeOwner.maybeOwner)
161+
else if sym.isTypeParam then
162+
primaryConstructorTypeParam(sym.maybeOwner).toSet
163+
++ additionalForEnumTypeParam(sym.maybeOwner) + sym
133164
else Set(sym)
134165
all.filter(s => s != NoSymbol && !s.isError)
135166
end symbolAlternatives
@@ -409,7 +440,7 @@ abstract class PcCollector[T](
409440
* All select statements such as:
410441
* val a = hello.<<b>>
411442
*/
412-
case sel: Select
443+
case sel: Select
413444
if sel.span.isCorrect && filter(sel) &&
414445
!isForComprehensionMethod(sel) =>
415446
occurrences + collect(

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

+49-8
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,8 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
758758
| }
759759
|}""".stripMargin
760760
)
761-
762-
@Test def `for-comp-map` =
761+
762+
@Test def `for-comp-map` =
763763
check(
764764
"""|object Main {
765765
| val x = List(1).<<m@@ap>>(_ + 1)
@@ -770,7 +770,7 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
770770
|""".stripMargin,
771771
)
772772

773-
@Test def `for-comp-map1` =
773+
@Test def `for-comp-map1` =
774774
check(
775775
"""|object Main {
776776
| val x = List(1).<<m@@ap>>(_ + 1)
@@ -782,7 +782,7 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
782782
|""".stripMargin,
783783
)
784784

785-
@Test def `for-comp-foreach` =
785+
@Test def `for-comp-foreach` =
786786
check(
787787
"""|object Main {
788788
| val x = List(1).<<for@@each>>(_ => ())
@@ -793,7 +793,7 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
793793
|""".stripMargin,
794794
)
795795

796-
@Test def `for-comp-withFilter` =
796+
@Test def `for-comp-withFilter` =
797797
check(
798798
"""|object Main {
799799
| val x = List(1).<<with@@Filter>>(_ => true)
@@ -805,7 +805,7 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
805805
|""".stripMargin,
806806
)
807807

808-
@Test def `for-comp-withFilter1` =
808+
@Test def `for-comp-withFilter1` =
809809
check(
810810
"""|object Main {
811811
| val x = List(1).withFilter(_ => true).<<m@@ap>>(_ + 1)
@@ -817,7 +817,7 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
817817
|""".stripMargin,
818818
)
819819

820-
@Test def `for-comp-flatMap1` =
820+
@Test def `for-comp-flatMap1` =
821821
check(
822822
"""|object Main {
823823
| val x = List(1).<<flat@@Map>>(_ => List(1))
@@ -830,7 +830,7 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
830830
|""".stripMargin,
831831
)
832832

833-
@Test def `for-comp-flatMap2` =
833+
@Test def `for-comp-flatMap2` =
834834
check(
835835
"""|object Main {
836836
| val x = List(1).withFilter(_ => true).<<flat@@Map>>(_ => List(1))
@@ -1102,3 +1102,44 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
11021102
|val alpha = MyOption.<<MySome>>(1)
11031103
|""".stripMargin,
11041104
)
1105+
1106+
@Test def `type-params-in-enum` =
1107+
check(
1108+
"""|enum MyOption[+<<A@@A>>]:
1109+
| case MySome(value: <<AA>>)
1110+
| case MyNone
1111+
|""".stripMargin,
1112+
)
1113+
1114+
@Test def `type-params-in-enum2` =
1115+
check(
1116+
"""|enum MyOption[+<<AA>>]:
1117+
| case MySome(value: <<A@@A>>)
1118+
| case MyNone
1119+
|""".stripMargin,
1120+
)
1121+
1122+
@Test def `type-params-in-enum3` =
1123+
check(
1124+
"""|enum MyOption[<<AA>>](v: <<AA>>):
1125+
| def get: <<A@@A>> = ???
1126+
| case MySome[AA](value: AA) extends MyOption[Int](1)
1127+
|""".stripMargin,
1128+
)
1129+
1130+
@Test def `type-params-in-enum4` =
1131+
check(
1132+
"""|enum MyOption[+<<AA>>]:
1133+
| def get: <<A@@A>> = ???
1134+
| case MySome(value: <<AA>>)
1135+
| case MyNone
1136+
|""".stripMargin,
1137+
)
1138+
1139+
@Test def `type-params-in-enum5` =
1140+
check(
1141+
"""|enum MyOption[AA]:
1142+
| def get: AA = ???
1143+
| case MySome[<<AA>>](value: <<A@@A>>) extends MyOption[Int]
1144+
|""".stripMargin,
1145+
)

0 commit comments

Comments
 (0)