diff --git a/presentation-compiler/src/main/dotty/tools/pc/PcInlayHintsProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/PcInlayHintsProvider.scala index b22b3ae8b7f4..29396a5c0d32 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/PcInlayHintsProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/PcInlayHintsProvider.scala @@ -19,6 +19,7 @@ import dotty.tools.dotc.core.Contexts.Context import dotty.tools.dotc.core.Flags import dotty.tools.dotc.core.NameOps.fieldName import dotty.tools.dotc.core.Names.Name +import dotty.tools.dotc.core.NameKinds.DefaultGetterName import dotty.tools.dotc.core.StdNames.* import dotty.tools.dotc.core.Symbols.* import dotty.tools.dotc.core.Types.* @@ -457,6 +458,13 @@ object Parameters: case TypeApply(fun, _) => getUnderlyingFun(fun) case t => t + @tailrec + def isDefaultArg(arg: Tree): Boolean = arg match + case Ident(name) => name.is(DefaultGetterName) + case Select(_, name) => name.is(DefaultGetterName) + case Apply(fun, _) => isDefaultArg(fun) + case _ => false + if (params.namedParameters() || params.byNameParameters()) then tree match case Apply(fun, args) if isRealApply(fun) => @@ -467,15 +475,16 @@ object Parameters: val funTp = fun.typeOpt.widenTermRefExpr val paramNames = funTp.paramNamess.flatten val paramInfos = funTp.paramInfoss.flatten + Some( - // Check if the function is an infix function or the underlying function is an infix function isInfixFun(fun, args) || underlyingFun.isInfix, ( args .zip(paramNames) .zip(paramInfos) .collect { - case ((arg, paramName), paramInfo) if !arg.span.isZeroExtent => (paramName.fieldName, arg.sourcePos, paramInfo.isByName) + case ((arg, paramName), paramInfo) if !arg.span.isZeroExtent && !isDefaultArg(arg) => + (paramName.fieldName, arg.sourcePos, paramInfo.isByName) } ) ) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/inlayHints/InlayHintsSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/inlayHints/InlayHintsSuite.scala index bb79c19f5823..d9c10080581f 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/inlayHints/InlayHintsSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/inlayHints/InlayHintsSuite.scala @@ -1277,4 +1277,61 @@ class InlayHintsSuite extends BaseInlayHintsSuite { |} |""".stripMargin ) + + @Test def `default-parameter` = + check( + """|object Main { + | def foo(a: Int, b: Int = 2) = a + b + | val x = foo(1) + |} + |""".stripMargin, + """|object Main { + | def foo(a: Int, b: Int = 2)/*: Int<>*/ = a + b + | val x/*: Int<>*/ = foo(/*a = */1) + |} + |""".stripMargin + ) + + @Test def `default-parameter-2` = + check( + """|object Main { + | def foo(a: Int = 10, b: Int = 2) = a + b + | val x = foo(b = 1) + |} + |""".stripMargin, + """|object Main { + | def foo(a: Int = 10, b: Int = 2)/*: Int<>*/ = a + b + | val x/*: Int<>*/ = foo(b = 1) + |} + |""".stripMargin + ) + + @Test def `default-parameter-3` = + check( + """|object Main { + | def foo(a: Int, b: Int = 2, c: Int) = a + b + c + | val x = foo(a = 1, c = 2) + |} + |""".stripMargin, + """|object Main { + | def foo(a: Int, b: Int = 2, c: Int)/*: Int<>*/ = a + b + c + | val x/*: Int<>*/ = foo(a = 1, c = 2) + |} + |""".stripMargin + ) + + @Test def `default-parameter-4` = + check( + """|object Main { + | def foo(a: Int, b: Int = 2, c: Int) = a + b + c + | val x = foo(1, 2, 3) + |} + |""".stripMargin, + """|object Main { + | def foo(a: Int, b: Int = 2, c: Int)/*: Int<>*/ = a + b + c + | val x/*: Int<>*/ = foo(/*a = */1, /*b = */2, /*c = */3) + |} + |""".stripMargin + ) + }