Skip to content

Commit ae219e5

Browse files
committed
improvement: when inserting type simplify named tuples
Also make it work on hover, completions and inferred type
1 parent 11c3afa commit ae219e5

File tree

8 files changed

+95
-14
lines changed

8 files changed

+95
-14
lines changed

Diff for: presentation-compiler/src/main/dotty/tools/pc/HoverProvider.scala

+12-9
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,23 @@ object HoverProvider:
110110
if symbol.name == nme.selectDynamic || symbol.name == nme.applyDynamic =>
111111
fallbackToDynamics(path, printer, contentType)
112112
case symbolTpes @ ((symbol, tpe, None) :: _) =>
113-
val exprTpw = tpe.widenTermRefExpr.deepDealias
113+
val exprTpw = dealiased(tpe.widenTermRefExpr)
114114
val hoverString =
115115
tpw match
116116
// https://github.com/scala/scala3/issues/8891
117117
case tpw: ImportType =>
118118
printer.hoverSymbol(symbol, symbol.paramRef)
119119
case _ =>
120-
val (tpe, sym) =
120+
val (innerTpe, sym) =
121121
if symbol.isType then (symbol.typeRef, symbol)
122122
else enclosing.head.seenFrom(symbol)
123123

124124
val finalTpe =
125-
if tpe != NoType then tpe
125+
if tpe.isNamedTupleType then tpe.widenTermRefExpr
126+
else if innerTpe != NoType then innerTpe
126127
else tpw
127128

128-
printer.hoverSymbol(sym, finalTpe.deepDealias)
129+
printer.hoverSymbol(sym, dealiased(finalTpe))
129130
end match
130131
end hoverString
131132

@@ -134,7 +135,7 @@ object HoverProvider:
134135
.map(_.docstring())
135136
.mkString("\n")
136137

137-
val expresionTypeOpt =
138+
val expresionTypeOpt =
138139
if symbol.name == StdNames.nme.??? then
139140
InferExpectedType(search, driver, params).infer()
140141
else printer.expressionType(exprTpw)
@@ -161,7 +162,7 @@ object HoverProvider:
161162
ju.Optional.empty().nn
162163
end match
163164
case (_, tpe, Some(namedTupleArg)) :: _ =>
164-
val exprTpw = tpe.widenTermRefExpr.deepDealias
165+
val exprTpw = dealiased(tpe.widenTermRefExpr)
165166
printer.expressionType(exprTpw) match
166167
case Some(tpe) =>
167168
ju.Optional.of(
@@ -179,6 +180,8 @@ object HoverProvider:
179180
end if
180181
end hover
181182

183+
private def dealiased(tpe: Type)(using Context): Type = if tpe.isNamedTupleType then tpe.deepDealias.simplified else tpe.deepDealias
184+
182185
extension (pos: SourcePosition)
183186
private def isPoint: Boolean = pos.start == pos.end
184187

@@ -194,7 +197,7 @@ object HoverProvider:
194197
val resultType =
195198
rest match
196199
case Select(_, asInstanceOf) :: TypeApply(_, List(tpe)) :: _ if asInstanceOf == nme.asInstanceOfPM =>
197-
tpe.tpe.widenTermRefExpr.deepDealias
200+
dealiased(tpe.tpe.widenTermRefExpr)
198201
case _ if n == nme.selectDynamic => tpe.resultType
199202
case _ => tpe
200203

@@ -220,9 +223,9 @@ object HoverProvider:
220223
findRefinement(parent)
221224
case _ => None
222225

223-
val refTpe = sel.typeOpt.widen.deepDealias match
226+
val refTpe = dealiased(sel.typeOpt.widen) match
224227
case r: RefinedType => Some(r)
225-
case t: (TermRef | TypeProxy) => Some(t.termSymbol.info.deepDealias)
228+
case t: (TermRef | TypeProxy) => Some(dealiased(t.termSymbol.info))
226229
case _ => None
227230

228231
refTpe.flatMap(findRefinement).asJava

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ final class InferredTypeProvider(
9999
case AppliedType(tycon, args) =>
100100
isInScope(tycon) && args.forall(isInScope)
101101
case _ => true
102-
if isInScope(tpe)
103-
then tpe
102+
if isInScope(tpe) then tpe
103+
else if tpe.isNamedTupleType then tpe.dealias.simplified
104104
else tpe.deepDealias
105105

106106
val printer = ShortenedTypePrinter(

Diff for: presentation-compiler/src/main/dotty/tools/pc/PcInlayHintsProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class PcInlayHintsProvider(
142142
if isInScope(tpe) then tpe
143143
else tpe.deepDealias(using indexedCtx.ctx)
144144

145-
val dealiased = if tpe.isNamedTupleType then tpe else optDealias(tpe)
145+
val dealiased = if tpe.isNamedTupleType then optDealias(tpe).simplified else optDealias(tpe)
146146
val tpeStr = printer.tpe(dealiased)
147147
val usedRenames = printer.getUsedRenames
148148
val parts = partsFromType(dealiased, usedRenames)

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ class ShortenedTypePrinter(
214214
case ConstantType(const) => toText(const)
215215
case _ => toTextRef(tp) ~ ".type"
216216

217-
def tpe(tpe: Type): String = toText(tpe).mkString(defaultWidth, false)
217+
def tpe(tpe: Type): String =
218+
val dealiased = if (tpe.isNamedTupleType) tpe.deepDealias.simplified else tpe
219+
toText(dealiased).mkString(defaultWidth, false)
218220

219221
def hoverSymbol(sym: Symbol, info: Type)(using Context): String =
220222
val typeSymbol = info.typeSymbol

Diff for: presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala

+15
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,21 @@ class CompletionSuite extends BaseCompletionSuite:
20412041
filter = _.contains("name")
20422042
)
20432043

2044+
@Test def `namedTuple-completions-2` =
2045+
check(
2046+
"""|import scala.NamedTuple.*
2047+
|
2048+
|def hello = (path = ".", num = 5)++ (line = 1)
2049+
|val hello2 = (path = ".", num = 5)++ (line = 1)
2050+
|
2051+
|@main def bla =
2052+
| hello@@
2053+
|""".stripMargin,
2054+
"""|hello2: (path : String, num : Int, line : Int)
2055+
|hello: (path : String, num : Int, line : Int)
2056+
""".stripMargin,
2057+
)
2058+
20442059
@Test def `Selectable with namedTuple Fields member` =
20452060
check(
20462061
"""|import scala.NamedTuple.*

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

+20
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,26 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
970970
|""".stripMargin
971971
)
972972

973+
@Test def `named-tuples` =
974+
checkEdit(
975+
"""|def hello = (path = ".", num = 5)
976+
|
977+
|def <<test>> =
978+
| hello ++ (line = 1)
979+
|
980+
|@main def bla =
981+
| val x: (path: String, num: Int, line: Int) = test
982+
|""".stripMargin,
983+
"""|def hello = (path = ".", num = 5)
984+
|
985+
|def test: (path : String, num : Int, line : Int) =
986+
| hello ++ (line = 1)
987+
|
988+
|@main def bla =
989+
| val x: (path: String, num: Int, line: Int) = test
990+
|""".stripMargin
991+
)
992+
973993
def checkEdit(
974994
original: String,
975995
expected: String

Diff for: presentation-compiler/test/dotty/tools/pc/tests/hover/HoverTermSuite.scala

+41
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,47 @@ class HoverTermSuite extends BaseHoverSuite:
745745
"name: String".hover
746746
)
747747

748+
749+
@Test def `named-tuples3`: Unit =
750+
check(
751+
"""|def hello = (path = ".", num = 5)
752+
|
753+
|def test =
754+
| hello ++ (line = 1)
755+
|
756+
|@main def bla =
757+
| val x: (path: String, num: Int, line: Int) = t@@est
758+
|""".stripMargin,
759+
"def test: (path : String, num : Int, line : Int)".hover
760+
)
761+
762+
763+
@Test def `named-tuples4`: Unit =
764+
check(
765+
"""|def hello = (path = ".", num = 5)
766+
|
767+
|def test =
768+
| hel@@lo ++ (line = 1)
769+
|
770+
|@main def bla =
771+
| val x: (path: String, num: Int, line: Int) = test
772+
|""".stripMargin,
773+
"def hello: (path : String, num : Int)".hover
774+
)
775+
776+
@Test def `named-tuples5`: Unit =
777+
check(
778+
"""|def hello = (path = ".", num = 5)
779+
|
780+
|def test(x: (path: String, num: Int)) =
781+
| x ++ (line = 1)
782+
|
783+
|@main def bla =
784+
| val x: (path: String, num: Int, line: Int) = t@@est(hello)
785+
|""".stripMargin,
786+
"def test(x: (path : String, num : Int)): (path : String, num : Int, line : Int)".hover
787+
)
788+
748789
@Test def `value-of`: Unit =
749790
check(
750791
"""|enum Foo(val key: String) {

Diff for: presentation-compiler/test/dotty/tools/pc/tests/inlayHints/InlayHintsSuite.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ class InlayHintsSuite extends BaseInlayHintsSuite {
10681068
|""".stripMargin,
10691069
"""|def hello/*: (path : String<<java/lang/String#>>, num : Int<<scala/Int#>>)*/ = (path = ".", num = 5)/*[(String<<java/lang/String#>>, Int<<scala/Int#>>)]*/
10701070
|
1071-
|def test/*: Concat<<scala/NamedTuple.Concat#>>[(path : String<<java/lang/String#>>, num : Int<<scala/Int#>>), (line : Int<<scala/Int#>>)]*/ =
1071+
|def test/*: (path : String<<java/lang/String#>>, num : Int<<scala/Int#>>, line : Int<<scala/Int#>>)*/ =
10721072
| hello ++/*[Tuple1<<scala/Tuple1#>>["line"], Tuple1<<scala/Tuple1#>>[Int<<scala/Int#>>]]*/ (line = 1)/*(using refl<<scala/`<:<`.refl().>>)*//*[Tuple1<<scala/Tuple1#>>[Int<<scala/Int#>>]]*/
10731073
|
10741074
|@main def bla/*: Unit<<scala/Unit#>>*/ =

0 commit comments

Comments
 (0)