Skip to content

Commit f0cc46a

Browse files
committed
bugfix: Fix enumeration issues when Value is imported
This is still not a perfect fix, since we add the full prefix, but that's the current status quo. Making prefixes shorter is something we need to work on separately.
1 parent 107a2f1 commit f0cc46a

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ final class InferredTypeProvider(
136136
findNamePos(sourceText, vl, keywordOffset).endPos.toLsp
137137
adjustOpt.foreach(adjust => endPos.setEnd(adjust.adjustedEndPos))
138138
val spaceBefore = name.isOperatorName
139-
140139
new TextEdit(
141140
endPos,
142141
printTypeAscription(optDealias(tpt.typeOpt), spaceBefore) + {

presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ class ShortenedTypePrinter(
174174
res.toPrefixText
175175
}
176176

177-
178177
override def toTextPrefixOf(tp: NamedType): Text = controlled {
179178
val maybeRenamedPrefix: Option[Text] = findRename(tp)
180179
def trimmedPrefix: Text =
@@ -188,7 +187,7 @@ class ShortenedTypePrinter(
188187
missingImports += ImportSel.Direct(tp.symbol)
189188
Text()
190189
// the symbol is in scope, we can omit the prefix
191-
case Result.InScope => Text()
190+
case Result.InScope if !isPathDependent(tp) => Text()
192191
// the symbol is in conflict, we have to include prefix to avoid ambiguity
193192
case Result.Conflict =>
194193
maybeRenamedPrefix.getOrElse(super.toTextPrefixOf(tp))
@@ -197,6 +196,13 @@ class ShortenedTypePrinter(
197196
optionalRootPrefix(tp.symbol) ~ maybeRenamedPrefix.getOrElse(trimmedPrefix)
198197
}
199198

199+
/* If the owner is different from the type symbol, it means that the type is defined in a parent*/
200+
private def isPathDependent(tp: NamedType) = tp match {
201+
case TypeRef(tt @ ThisType(_), _) =>
202+
tp.symbol.isClass && tt.typeSymbol != NoSymbol && tp.symbol.owner != tt.typeSymbol
203+
case _ => false
204+
}
205+
200206
override protected def selectionString(tp: NamedType): String =
201207
indexedCtx.rename(tp.symbol) match
202208
case Some(value) =>

presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala

+66
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,72 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
990990
|""".stripMargin
991991
)
992992

993+
@Test def `enums` =
994+
checkEdit(
995+
"""|object EnumerationValue:
996+
| object Day extends Enumeration {
997+
| type Day = Value
998+
| val Weekday, Weekend = Value
999+
| }
1000+
| object Bool extends Enumeration {
1001+
| type Bool = Value
1002+
| val True, False = Value
1003+
| }
1004+
| import Bool._
1005+
| def day(d: Day.Value): Unit = ???
1006+
| val <<d>> =
1007+
| if (true) Day.Weekday
1008+
| else Day.Weekend
1009+
|""".stripMargin,
1010+
"""|object EnumerationValue:
1011+
| object Day extends Enumeration {
1012+
| type Day = Value
1013+
| val Weekday, Weekend = Value
1014+
| }
1015+
| object Bool extends Enumeration {
1016+
| type Bool = Value
1017+
| val True, False = Value
1018+
| }
1019+
| import Bool._
1020+
| def day(d: Day.Value): Unit = ???
1021+
| val d: EnumerationValue.Day.Value =
1022+
| if (true) Day.Weekday
1023+
| else Day.Weekend
1024+
|""".stripMargin
1025+
)
1026+
1027+
@Test def `enums2` =
1028+
checkEdit(
1029+
"""|object EnumerationValue:
1030+
| object Day extends Enumeration {
1031+
| type Day = Value
1032+
| val Weekday, Weekend = Value
1033+
| }
1034+
| object Bool extends Enumeration {
1035+
| type Bool = Value
1036+
| val True, False = Value
1037+
| }
1038+
| import Bool._
1039+
| val <<b>> =
1040+
| if (true) True
1041+
| else False
1042+
|""".stripMargin,
1043+
"""|object EnumerationValue:
1044+
| object Day extends Enumeration {
1045+
| type Day = Value
1046+
| val Weekday, Weekend = Value
1047+
| }
1048+
| object Bool extends Enumeration {
1049+
| type Bool = Value
1050+
| val True, False = Value
1051+
| }
1052+
| import Bool._
1053+
| val b: EnumerationValue.Bool.Value =
1054+
| if (true) True
1055+
| else False
1056+
|""".stripMargin
1057+
)
1058+
9931059
def checkEdit(
9941060
original: String,
9951061
expected: String

0 commit comments

Comments
 (0)