Skip to content

Commit cec2001

Browse files
committed
fix tests, active parameter position and simplify code
1 parent db8b2a3 commit cec2001

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

compiler/src/dotty/tools/dotc/util/Signatures.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ object Signatures {
4949
*/
5050
def callInfo(path: List[tpd.Tree], span: Span)(using Context): (Int, Int, List[SingleDenotation]) =
5151
val enclosingApply = path.dropWhile {
52-
case Apply(fun, _) => fun.span.contains(span)
53-
case unapply @ UnApply(fun, _, _) =>
54-
fun.span.contains(span) || ctx.definitions.isTupleClass(unapply.fun.symbol.owner.companionClass)
52+
case apply @ Apply(fun, _) => fun.span.contains(span) || apply.span.end == span.end
53+
case unapply @ UnApply(fun, _, _) => fun.span.contains(span) || unapply.span.end == span.end || isTuple(unapply)
5554
case _ => true
5655
}.headOption
5756

@@ -92,16 +91,17 @@ object Signatures {
9291
)(using Context): (Int, Int, List[SingleDenotation]) =
9392
val patternPosition = patterns.indexWhere(_.span.contains(span))
9493
val activeParameter = extractParamTypess(fun.tpe.finalResultType.widen).headOption.map { params =>
95-
if patternPosition == -1 then
96-
if patterns.length > 0 then
97-
(params.size - 1)
98-
else
99-
0
100-
else
101-
(params.size - 1) min patternPosition
102-
}.getOrElse(patternPosition)
94+
(patternPosition, patterns.length) match
95+
case (-1, 0) => 0 // there are no patterns yet so it must be first one
96+
case (-1, pos) => -1 // there are patterns, we must be outside range so we set no active parameter
97+
case _ => (params.size - 1) min patternPosition max 0 // handle unapplySeq to always highlight Seq[A] on elements
98+
}.getOrElse(-1)
99+
100+
val appliedDenot = fun.symbol.asSingleDenotation.mapInfo(_ => fun.tpe) :: Nil
101+
(activeParameter, 0, appliedDenot)
103102

104-
(activeParameter, 0, fun.symbol.asSingleDenotation.mapInfo(_ => fun.tpe) :: Nil)
103+
private def isTuple(tree: tpd.Tree)(using Context): Boolean =
104+
ctx.definitions.isTupleClass(tree.symbol.owner.companionClass)
105105

106106
private def extractParamTypess(resultType: Type)(using Context): List[List[Type]] =
107107
resultType match {

language-server/test/dotty/tools/languageserver/SignatureHelpTest.scala

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SignatureHelpTest {
4040
| case s @ Even(${m1}) => println(s"s has an even number of characters")
4141
| case s => println(s"s has an odd number of characters")
4242
"""
43-
.signatureHelp(m1, Nil, Some(0), 0)
43+
.signatureHelp(m1, Nil, Some(0), -1)
4444
}
4545

4646
@Test def unapplyCustomClass: Unit = {
@@ -78,6 +78,46 @@ class SignatureHelpTest {
7878
.signatureHelp(m2, List(signature), Some(0), 1)
7979
}
8080

81+
@Test def nestedUnapplySignature: Unit = {
82+
val signatureOneTwo = S("", Nil, List(List(P("a", "One"), P("b", "Two"))), None)
83+
val signatureOne = S("", Nil, List(List(P("c", "Int"))), None)
84+
val signatureTwo = S("", Nil, List(List(P("d", "Int"))), None)
85+
86+
code"""case class One(c: Int)
87+
|case class Two(d: Int)
88+
|case class OneTwo[A, B](a: A, b: B)
89+
|
90+
|object Main {
91+
| val tp = new OneTwo(One(1), Two(5))
92+
| tp match {
93+
| case OneTwo(x${m1}, ${m2}) =>
94+
| case OneTwo(One(x${m4})${m3}, T${m5}wo(${m6})${m8})${m7} =>
95+
| }
96+
|}"""
97+
.signatureHelp(m1, List(signatureOneTwo), Some(0), 0)
98+
.signatureHelp(m2, List(signatureOneTwo), Some(0), 1)
99+
.signatureHelp(m3, List(signatureOneTwo), Some(0), 0)
100+
.signatureHelp(m4, List(signatureOne), Some(0), 0)
101+
.signatureHelp(m5, List(signatureOneTwo), Some(0), 1)
102+
.signatureHelp(m6, List(signatureTwo), Some(0), 0)
103+
}
104+
105+
@Test def properParameterIndexTest: Unit = {
106+
val signature = S("", Nil, List(List(P("a", "Int"), P("b", "String"))), None)
107+
code"""case class Two(a: Int, b: String)
108+
|
109+
|object Main {
110+
| val tp = new Two(1, "")
111+
| tp match {
112+
| case Two(x${m1}, y ${m2}, d${m3})${m4} =>
113+
| }
114+
|}"""
115+
.signatureHelp(m1, List(signature), Some(0), 0)
116+
.signatureHelp(m2, List(signature), Some(0), -1)
117+
.signatureHelp(m3, List(signature), Some(0), -1)
118+
.signatureHelp(m4, Nil, Some(0), 0)
119+
}
120+
81121
@Test def unapplyClass: Unit = {
82122
val signature = S("", Nil, List(List(P("", "Int"), P("", "String"))), None)
83123

0 commit comments

Comments
 (0)