Skip to content

Handle default arguments in named parameters for inlay hints #23641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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) =>
Expand All @@ -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)
}
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<<scala/Int#>>*/ = a + b
| val x/*: Int<<scala/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<<scala/Int#>>*/ = a + b
| val x/*: Int<<scala/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<<scala/Int#>>*/ = a + b + c
| val x/*: Int<<scala/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<<scala/Int#>>*/ = a + b + c
| val x/*: Int<<scala/Int#>>*/ = foo(/*a = */1, /*b = */2, /*c = */3)
|}
|""".stripMargin
)

}
Loading