Skip to content

Commit a115c8d

Browse files
authored
Handle default arguments in named parameters for inlay hints (#23641)
Handle default arguments in named parameters for inlay hints
1 parent 902d9d9 commit a115c8d

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import dotty.tools.dotc.core.Contexts.Context
1919
import dotty.tools.dotc.core.Flags
2020
import dotty.tools.dotc.core.NameOps.fieldName
2121
import dotty.tools.dotc.core.Names.Name
22+
import dotty.tools.dotc.core.NameKinds.DefaultGetterName
2223
import dotty.tools.dotc.core.StdNames.*
2324
import dotty.tools.dotc.core.Symbols.*
2425
import dotty.tools.dotc.core.Types.*
@@ -457,6 +458,13 @@ object Parameters:
457458
case TypeApply(fun, _) => getUnderlyingFun(fun)
458459
case t => t
459460

461+
@tailrec
462+
def isDefaultArg(arg: Tree): Boolean = arg match
463+
case Ident(name) => name.is(DefaultGetterName)
464+
case Select(_, name) => name.is(DefaultGetterName)
465+
case Apply(fun, _) => isDefaultArg(fun)
466+
case _ => false
467+
460468
if (params.namedParameters() || params.byNameParameters()) then
461469
tree match
462470
case Apply(fun, args) if isRealApply(fun) =>
@@ -467,15 +475,16 @@ object Parameters:
467475
val funTp = fun.typeOpt.widenTermRefExpr
468476
val paramNames = funTp.paramNamess.flatten
469477
val paramInfos = funTp.paramInfoss.flatten
478+
470479
Some(
471-
// Check if the function is an infix function or the underlying function is an infix function
472480
isInfixFun(fun, args) || underlyingFun.isInfix,
473481
(
474482
args
475483
.zip(paramNames)
476484
.zip(paramInfos)
477485
.collect {
478-
case ((arg, paramName), paramInfo) if !arg.span.isZeroExtent => (paramName.fieldName, arg.sourcePos, paramInfo.isByName)
486+
case ((arg, paramName), paramInfo) if !arg.span.isZeroExtent && !isDefaultArg(arg) =>
487+
(paramName.fieldName, arg.sourcePos, paramInfo.isByName)
479488
}
480489
)
481490
)

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,4 +1277,61 @@ class InlayHintsSuite extends BaseInlayHintsSuite {
12771277
|}
12781278
|""".stripMargin
12791279
)
1280+
1281+
@Test def `default-parameter` =
1282+
check(
1283+
"""|object Main {
1284+
| def foo(a: Int, b: Int = 2) = a + b
1285+
| val x = foo(1)
1286+
|}
1287+
|""".stripMargin,
1288+
"""|object Main {
1289+
| def foo(a: Int, b: Int = 2)/*: Int<<scala/Int#>>*/ = a + b
1290+
| val x/*: Int<<scala/Int#>>*/ = foo(/*a = */1)
1291+
|}
1292+
|""".stripMargin
1293+
)
1294+
1295+
@Test def `default-parameter-2` =
1296+
check(
1297+
"""|object Main {
1298+
| def foo(a: Int = 10, b: Int = 2) = a + b
1299+
| val x = foo(b = 1)
1300+
|}
1301+
|""".stripMargin,
1302+
"""|object Main {
1303+
| def foo(a: Int = 10, b: Int = 2)/*: Int<<scala/Int#>>*/ = a + b
1304+
| val x/*: Int<<scala/Int#>>*/ = foo(b = 1)
1305+
|}
1306+
|""".stripMargin
1307+
)
1308+
1309+
@Test def `default-parameter-3` =
1310+
check(
1311+
"""|object Main {
1312+
| def foo(a: Int, b: Int = 2, c: Int) = a + b + c
1313+
| val x = foo(a = 1, c = 2)
1314+
|}
1315+
|""".stripMargin,
1316+
"""|object Main {
1317+
| def foo(a: Int, b: Int = 2, c: Int)/*: Int<<scala/Int#>>*/ = a + b + c
1318+
| val x/*: Int<<scala/Int#>>*/ = foo(a = 1, c = 2)
1319+
|}
1320+
|""".stripMargin
1321+
)
1322+
1323+
@Test def `default-parameter-4` =
1324+
check(
1325+
"""|object Main {
1326+
| def foo(a: Int, b: Int = 2, c: Int) = a + b + c
1327+
| val x = foo(1, 2, 3)
1328+
|}
1329+
|""".stripMargin,
1330+
"""|object Main {
1331+
| def foo(a: Int, b: Int = 2, c: Int)/*: Int<<scala/Int#>>*/ = a + b + c
1332+
| val x/*: Int<<scala/Int#>>*/ = foo(/*a = */1, /*b = */2, /*c = */3)
1333+
|}
1334+
|""".stripMargin
1335+
)
1336+
12801337
}

0 commit comments

Comments
 (0)