Skip to content

Commit c20279c

Browse files
committed
bugfix: [Scala 3] Adjust changes for the most recent compiler version
To account for scala/scala3#15517
1 parent 5bd656f commit c20279c

File tree

4 files changed

+91
-39
lines changed

4 files changed

+91
-39
lines changed

mtags/src/main/scala-3/scala/meta/internal/pc/SignatureHelpProvider.scala

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ import dotty.tools.dotc.util.Signatures
2020
import dotty.tools.dotc.util.Signatures.Signature
2121
import dotty.tools.dotc.util.SourcePosition
2222
import org.eclipse.{lsp4j as l}
23+
import dotty.tools.dotc.ast.Trees.AppliedTypeTree
24+
import dotty.tools.dotc.ast.Trees.TypeApply
2325

2426
object SignatureHelpProvider:
2527

28+
private val versionSupportsTypeParams =
29+
SemVer.isCompatibleVersion("3.2.1", BuildInfo.scalaCompilerVersion)
30+
2631
def signatureHelp(
2732
driver: InteractiveDriver,
2833
params: OffsetParams,
@@ -59,23 +64,38 @@ object SignatureHelpProvider:
5964

6065
}
6166

67+
/* Versions prior to 3.2.1 did not support type parameters
68+
* so we need to skip them.
69+
*/
70+
val adjustedParamN =
71+
if versionSupportsTypeParams then paramN
72+
else
73+
val adjusted =
74+
signatureInfos.lift(callableN).map(_.tparams.size).getOrElse(0)
75+
paramN + adjusted
6276
new l.SignatureHelp(
6377
signatureInfos.map(signatureToSignatureInformation).asJava,
6478
callableN,
65-
paramN
79+
adjustedParamN
6680
)
6781
end signatureHelp
6882

69-
private def isTuple(tree: tpd.Tree)(using Context): Boolean =
70-
ctx.definitions.isTupleClass(tree.symbol.owner.companionClass)
83+
private def isValid(tree: tpd.Tree)(using Context): Boolean =
84+
ctx.definitions.isTupleNType(tree.tpe) || ctx.definitions.isFunctionType(
85+
tree.tpe
86+
)
7187

7288
private def notCurrentApply(
7389
tree: tpd.Tree,
7490
pos: SourcePosition
7591
)(using Context): Boolean =
7692
tree match
7793
case unapply: tpd.UnApply =>
78-
unapply.fun.span.contains(pos.span) || isTuple(unapply)
94+
unapply.fun.span.contains(pos.span) || isValid(unapply)
95+
case typeTree @ AppliedTypeTree(fun, _) =>
96+
fun.span.contains(pos.span) || isValid(typeTree)
97+
case typeApply @ TypeApply(fun, _) =>
98+
fun.span.contains(pos.span) || isValid(typeApply)
7999
case appl: tpd.GenericApply =>
80100
/* find first apply that the cursor is located in arguments and not at function name
81101
* for example in:
@@ -128,23 +148,27 @@ object SignatureHelpProvider:
128148
private def signatureToSignatureInformation(
129149
signature: Signatures.Signature
130150
): l.SignatureInformation =
151+
val tparams = signature.tparams.map(Signatures.Param("", _))
131152
val paramInfoss =
132-
signature.paramss.map(_.map(paramToParameterInformation))
133-
val paramLists = signature.paramss
134-
.map { paramList =>
135-
val labels = paramList.map(_.show)
136-
val prefix = if paramList.exists(_.isImplicit) then "using " else ""
137-
labels.mkString(prefix, ", ", "")
138-
}
139-
.mkString("(", ")(", ")")
153+
(tparams ::: signature.paramss.flatten).map(paramToParameterInformation)
154+
val paramLists =
155+
if signature.paramss.forall(_.isEmpty) && tparams.nonEmpty then ""
156+
else
157+
signature.paramss
158+
.map { paramList =>
159+
val labels = paramList.map(_.show)
160+
val prefix = if paramList.exists(_.isImplicit) then "using " else ""
161+
labels.mkString(prefix, ", ", "")
162+
}
163+
.mkString("(", ")(", ")")
140164
val tparamsLabel =
141165
if signature.tparams.isEmpty then ""
142166
else signature.tparams.mkString("[", ", ", "]")
143167
val returnTypeLabel = signature.returnType.map(t => s": $t").getOrElse("")
144168
val label = s"${signature.name}$tparamsLabel$paramLists$returnTypeLabel"
145169
val documentation = signature.doc.map(markupContent)
146170
val sig = new l.SignatureInformation(label)
147-
sig.setParameters(paramInfoss.flatten.asJava)
171+
sig.setParameters(paramInfoss.asJava)
148172
documentation.foreach(sig.setDocumentation(_))
149173
sig
150174
end signatureToSignatureInformation

tests/cross/src/test/scala/tests/pc/SignatureHelpDocSuite.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,7 @@ class SignatureHelpDocSuite extends BaseSignatureHelpSuite {
226226
)
227227

228228
checkDoc(
229-
// https://github.com/lampepfl/dotty/issues/15244
230-
"curry4".tag(IgnoreScala3),
229+
"curry4".tag(IgnoreScalaVersion.for3LessThan("3.2.0-RC1")),
231230
"""
232231
|object a {
233232
| def curry(a: Int, b: Int)(c: Int) = a

tests/cross/src/test/scala/tests/pc/SignatureHelpPatternSuite.scala

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ class SignatureHelpPatternSuite extends BaseSignatureHelpSuite {
4141
)
4242

4343
check(
44-
// https://github.com/lampepfl/dotty/issues/15248
45-
"generic2".tag(IgnoreScala3),
44+
"generic2".tag(IgnoreScalaVersion.for3LessThan("3.2.0-RC1")),
4645
"""
4746
|case class Two[T](a: T, b: T)
4847
|object Main {
@@ -53,12 +52,17 @@ class SignatureHelpPatternSuite extends BaseSignatureHelpSuite {
5352
|""".stripMargin,
5453
"""|(Any, Any)
5554
| ^^^
56-
|""".stripMargin
55+
|""".stripMargin,
56+
compat = Map(
57+
"3" ->
58+
"""|(a: T, b: T)
59+
| ^^^^
60+
|""".stripMargin
61+
)
5762
)
5863

5964
check(
60-
// https://github.com/lampepfl/dotty/issues/15248
61-
"generic3".tag(IgnoreScala3),
65+
"generic3".tag(IgnoreScalaVersion.for3LessThan("3.2.0-RC1")),
6266
"""
6367
|case class HKT[C[_], T](a: C[T])
6468
|object Main {
@@ -69,7 +73,13 @@ class SignatureHelpPatternSuite extends BaseSignatureHelpSuite {
6973
|""".stripMargin,
7074
"""|(Any)
7175
| ^^^
72-
|""".stripMargin
76+
|""".stripMargin,
77+
compat = Map(
78+
"3" ->
79+
"""|(a: C[T])
80+
| ^^^^^^^
81+
|""".stripMargin
82+
)
7383
)
7484

7585
check(
@@ -298,8 +308,7 @@ class SignatureHelpPatternSuite extends BaseSignatureHelpSuite {
298308
)
299309

300310
check(
301-
// https://github.com/lampepfl/dotty/issues/15248
302-
"pat6".tag(IgnoreScala3),
311+
"pat6".tag(IgnoreScalaVersion.for3LessThan("3.2.0-RC1")),
303312
"""
304313
|object OpenBrowserCommand {
305314
| def unapply(command: String): Option[Option[Int]] = {
@@ -313,7 +322,13 @@ class SignatureHelpPatternSuite extends BaseSignatureHelpSuite {
313322
""".stripMargin,
314323
"""|(Option[A])
315324
| ^^^^^^^^^
316-
|""".stripMargin
325+
|""".stripMargin,
326+
compat = Map(
327+
"3" ->
328+
"""|(Option[Int])
329+
| ^^^^^^^^^^^
330+
|""".stripMargin
331+
)
317332
)
318333

319334
check(

tests/cross/src/test/scala/tests/pc/SignatureHelpSuite.scala

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,10 @@ class SignatureHelpSuite extends BaseSignatureHelpSuite {
255255
)
256256
check(
257257
// https://github.com/lampepfl/dotty/issues/15244
258-
"vararg".tag(
259-
IgnoreScalaVersion.forLaterThan("3.2.0-RC1-bin-20220519-ee9cc8f-NIGHTLY")
260-
),
258+
"vararg",
259+
// .tag(
260+
// IgnoreScalaVersion.forLaterThan("3.2.0-RC1-bin-20220519-ee9cc8f-NIGHTLY")
261+
// ),
261262
"""
262263
|object a {
263264
| List(1, 2@@
@@ -274,8 +275,7 @@ class SignatureHelpSuite extends BaseSignatureHelpSuite {
274275
)
275276

276277
check(
277-
// https://github.com/lampepfl/dotty/issues/15249
278-
"tparam".tag(IgnoreScala3),
278+
"tparam".tag(IgnoreScalaVersion.for3LessThan("3.2.1")),
279279
"""
280280
|object a {
281281
| identity[I@@]
@@ -287,8 +287,7 @@ class SignatureHelpSuite extends BaseSignatureHelpSuite {
287287
)
288288

289289
check(
290-
// https://github.com/lampepfl/dotty/issues/15249
291-
"tparam2".tag(IgnoreScala3),
290+
"tparam2".tag(IgnoreScalaVersion.for3LessThan("3.2.1")),
292291
"""
293292
|object a {
294293
| Option.empty[I@@]
@@ -300,7 +299,7 @@ class SignatureHelpSuite extends BaseSignatureHelpSuite {
300299
)
301300

302301
check(
303-
"tparam3".tag(IgnoreScala3),
302+
"tparam3".tag(IgnoreScalaVersion.for3LessThan("3.2.1")),
304303
"""
305304
|object a {
306305
| Option[I@@]
@@ -312,7 +311,7 @@ class SignatureHelpSuite extends BaseSignatureHelpSuite {
312311
)
313312

314313
check(
315-
"tparam4".tag(IgnoreScala3),
314+
"tparam4".tag(IgnoreScalaVersion.for3LessThan("3.2.1")),
316315
"""
317316
|object a {
318317
| Map.empty[I@@]
@@ -325,7 +324,10 @@ class SignatureHelpSuite extends BaseSignatureHelpSuite {
325324
"2.11" ->
326325
"""|empty[A, B]: Map[A,B]
327326
| ^
328-
|""".stripMargin
327+
|""".stripMargin,
328+
"3" -> """|empty[K, V]: Map[K, V]
329+
| ^
330+
|""".stripMargin
329331
)
330332
)
331333

@@ -694,7 +696,7 @@ class SignatureHelpSuite extends BaseSignatureHelpSuite {
694696
)
695697

696698
check(
697-
"evidence".tag(IgnoreScala3),
699+
"evidence".tag(IgnoreScalaVersion.for3LessThan("3.2.1")),
698700
"""
699701
|object a {
700702
| Array.empty[@@]
@@ -729,27 +731,39 @@ class SignatureHelpSuite extends BaseSignatureHelpSuite {
729731
)
730732

731733
check(
732-
"type".tag(IgnoreScala3),
734+
"type".tag(IgnoreScalaVersion.for3LessThan("3.2.1")),
733735
"""
734736
|object a {
735737
| val x: Map[Int, Stri@@ng]
736738
|}
737739
""".stripMargin,
738740
"""|Map[A, B]: Map
739741
| ^
740-
| """.stripMargin
742+
| """.stripMargin,
743+
compat = Map(
744+
"3" ->
745+
"""|Map[K, V]: Map
746+
| ^
747+
| """.stripMargin
748+
)
741749
)
742750

743751
check(
744-
"type1".tag(IgnoreScala3),
752+
"type1".tag(IgnoreScalaVersion.for3LessThan("3.2.1")),
745753
"""
746754
|object a {
747755
| val x: Map[Int, Stri@@]
748756
|}
749757
""".stripMargin,
750758
"""|Map[A, B]: Map
751759
| ^
752-
| """.stripMargin
760+
| """.stripMargin,
761+
compat = Map(
762+
"3" ->
763+
"""|Map[K, V]: Map
764+
| ^
765+
| """.stripMargin
766+
)
753767
)
754768

755769
check(

0 commit comments

Comments
 (0)