Skip to content

Add inlay hints for by-name parameters #23283

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
May 30, 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 @@ -116,6 +116,29 @@ class PcInlayHintsProvider(
InlayHintKind.Type,
)
.addDefinition(adjustedPos.start)
case ByNameParameters(byNameParams) =>
def adjustByNameParameterPos(pos: SourcePosition): SourcePosition =
val adjusted = adjustPos(pos)
val start = text.indexWhere(!_.isWhitespace, adjusted.start)
val end = text.lastIndexWhere(!_.isWhitespace, adjusted.end - 1)

val startsWithBrace = text.lift(start).contains('{')
val endsWithBrace = text.lift(end).contains('}')

if startsWithBrace && endsWithBrace then
adjusted.withStart(start + 1)
else
adjusted

byNameParams.foldLeft(inlayHints) {
case (ih, pos) =>
val adjusted = adjustByNameParameterPos(pos)
ih.add(
adjusted.startPos.toLsp,
List(LabelPart("=> ")),
InlayHintKind.Parameter
)
}
case _ => inlayHints

private def toLabelParts(
Expand Down Expand Up @@ -388,3 +411,28 @@ object InferredType:
index >= 0 && index < afterDef.size && afterDef(index) == '@'

end InferredType

object ByNameParameters:
def unapply(tree: Tree)(using params: InlayHintsParams, ctx: Context): Option[List[SourcePosition]] =
def shouldSkipSelect(sel: Select) =
isForComprehensionMethod(sel) || sel.symbol.name == nme.unapply

if (params.byNameParameters()){
tree match
case Apply(TypeApply(sel: Select, _), _) if shouldSkipSelect(sel) =>
None
case Apply(sel: Select, _) if shouldSkipSelect(sel) =>
None
case Apply(fun, args) =>
val funTp = fun.typeOpt.widenTermRefExpr
val params = funTp.paramInfoss.flatten
Some(
args
.zip(params)
.collect {
case (tree, param) if param.isByName => tree.sourcePos
}
)
case _ => None
} else None
end ByNameParameters
Original file line number Diff line number Diff line change
Expand Up @@ -611,17 +611,17 @@ class InlayHintsSuite extends BaseInlayHintsSuite {
|class DemoSpec {
| import ScalatestMock._
|
| /*StringTestOps<<(6:17)>>(*/"foo"/*)*/ should {
| /*StringTestOps<<(6:17)>>(*/"checkThing1"/*)*/ in {
| /*StringTestOps<<(6:17)>>(*/"foo"/*)*/ should {/*=> */
| /*StringTestOps<<(6:17)>>(*/"checkThing1"/*)*/ in {/*=> */
| checkThing1[String]/*(using instancesString<<(10:15)>>)*/
| }/*(using here<<(5:15)>>)*/
| /*StringTestOps<<(6:17)>>(*/"checkThing2"/*)*/ in {
| /*StringTestOps<<(6:17)>>(*/"checkThing2"/*)*/ in {/*=> */
| checkThing2[String]/*(using instancesString<<(10:15)>>, instancesString<<(10:15)>>)*/
| }/*(using here<<(5:15)>>)*/
| }/*(using subjectRegistrationFunction<<(3:15)>>)*/
|
| /*StringTestOps<<(6:17)>>(*/"bar"/*)*/ should {
| /*StringTestOps<<(6:17)>>(*/"checkThing1"/*)*/ in {
| /*StringTestOps<<(6:17)>>(*/"bar"/*)*/ should {/*=> */
| /*StringTestOps<<(6:17)>>(*/"checkThing1"/*)*/ in {/*=> */
| checkThing1[String]/*(using instancesString<<(10:15)>>)*/
| }/*(using here<<(5:15)>>)*/
| }/*(using subjectRegistrationFunction<<(3:15)>>)*/
Expand Down Expand Up @@ -1075,4 +1075,113 @@ class InlayHintsSuite extends BaseInlayHintsSuite {
| val x: (path: String, num: Int, line: Int) = test
|""".stripMargin
)

@Test def `by-name-regular` =
check(
"""|object Main:
| def foo(x: => Int, y: Int, z: => Int)(w: Int, v: => Int): Unit = ()
| foo(1, 2, 3)(4, 5)
|""".stripMargin,
"""|object Main:
| def foo(x: => Int, y: Int, z: => Int)(w: Int, v: => Int): Unit = ()
| foo(/*=> */1, 2, /*=> */3)(4, /*=> */5)
|""".stripMargin
)

@Test def `by-name-block` =
check(
"""|object Main:
| def Future[A](arg: => A): A = arg
|
| Future(1 + 2)
| Future {
| 1 + 2
| }
| Future {
| val x = 1
| val y = 2
| x + y
| }
| Some(Option(2)
| .getOrElse {
| List(1,2)
| .headOption
| })
|""".stripMargin,
"""|object Main:
| def Future[A](arg: => A): A = arg
|
| Future/*[Int<<scala/Int#>>]*/(/*=> */1 + 2)
| Future/*[Int<<scala/Int#>>]*/ {/*=> */
| 1 + 2
| }
| Future/*[Int<<scala/Int#>>]*/ {/*=> */
| val x/*: Int<<scala/Int#>>*/ = 1
| val y/*: Int<<scala/Int#>>*/ = 2
| x + y
| }
| Some/*[Int<<scala/Int#>> | Option<<scala/Option#>>[Int<<scala/Int#>>]]*/(Option/*[Int<<scala/Int#>>]*/(2)
| .getOrElse/*[Int<<scala/Int#>> | Option<<scala/Option#>>[Int<<scala/Int#>>]]*/ {/*=> */
| List/*[Int<<scala/Int#>>]*/(1,2)
| .headOption
| })
|""".stripMargin
)

@Test def `by-name-for-comprehension` =
check(
"""|object Main:
| case class Test[A](v: A):
| def flatMap(f: => (A => Test[Int])): Test[Int] = f(v)
| def map(f: => (A => Int)): Test[Int] = Test(f(v))
|
| def main(args: Array[String]): Unit =
| val result: Test[Int] = for {
| a <- Test(10)
| b <- Test(20)
| } yield a + b
|
|""".stripMargin,
"""|object Main:
| case class Test[A](v: A):
| def flatMap(f: => (A => Test[Int])): Test[Int] = f(v)
| def map(f: => (A => Int)): Test[Int] = Test/*[Int<<scala/Int#>>]*/(f(v))
|
| def main(args: Array[String]): Unit =
| val result: Test[Int] = for {
| a <- Test/*[Int<<scala/Int#>>]*/(10)
| b <- Test/*[Int<<scala/Int#>>]*/(20)
| } yield a + b
|
|""".stripMargin,
)

@Test def `by-name-for-comprehension-generic` =
check(
"""|object Main:
| case class Test[A](v: A):
| def flatMap[B](f: => (A => Test[B])): Test[B] = f(v)
| def map[B](f: => (A => B)): Test[B] = Test(f(v))
|
| def main(args: Array[String]): Unit =
| val result: Test[Int] = for {
| a <- Test(10)
| b <- Test(20)
| } yield a + b
|
|""".stripMargin,
"""|object Main:
| case class Test[A](v: A):
| def flatMap[B](f: => (A => Test[B])): Test[B] = f(v)
| def map[B](f: => (A => B)): Test[B] = Test/*[B<<(4:13)>>]*/(f(v))
|
| def main(args: Array[String]): Unit =
| val result: Test[Int] = for {
| a <- Test/*[Int<<scala/Int#>>]*/(10)
| b <- Test/*[Int<<scala/Int#>>]*/(20)
| } yield a + b
|
|""".stripMargin,
)

}
Loading