Skip to content

Commit

Permalink
bugfix: Named args completions with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk committed Oct 9, 2023
1 parent 4bb3aeb commit d2ccce2
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ trait ArgCompletions { this: MetalsGlobal =>
val editRange: l.Range =
pos.withStart(ident.pos.start).withEnd(pos.start).toLsp
val funPos = apply.fun.pos
val method: Tree = typedTreeAt(funPos)
val method: Tree = typedTreeAt(funPos) match {
case Apply(Block(defParams, app: Apply), _)
if defParams.forall(p => p.isInstanceOf[ValDef]) =>
app
case t => t
}
val methodSym = method.symbol
lazy val methodsParams: List[MethodParams] = {
if (methodSym.isModule) getParamsFromObject(methodSym)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,24 @@ object NamedArgCompletions:
.getOrElse(baseArgs)
.filterNot(isUselessLiteral)

@tailrec
def isDefaultArg(t: Tree): Boolean = t match
// default args
case Ident(name) => name.is(DefaultGetterName)
// default args for methods defined in object
case Select(_, name) =>
name.is(DefaultGetterName)
// default args in not-first parameter list
// eg. def m(fst: Int)(snd: Int)(arg1: Int, arg2: Int = 123) = ???
case Apply(fun, _) => isDefaultArg(fun)
case _ => false

val isNamed: Set[Name] = args.iterator
.zip(baseParams.iterator)
// filter out synthesized args and default arg getters
.filterNot {
case (arg, _) if arg.symbol.denot.is(Flags.Synthetic) => true
case (Ident(name), _) => name.is(DefaultGetterName) // default args
case (Select(Ident(_), name), _) =>
name.is(DefaultGetterName) // default args for apply method
case _ => false
case (arg, _) => isDefaultArg(arg)
}
.map {
case (NamedArg(name, _), _) => name
Expand Down
115 changes: 115 additions & 0 deletions tests/cross/src/test/scala/tests/pc/CompletionArgSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,121 @@ class CompletionArgSuite extends BaseCompletionSuite {
"",
)

check(
"default-args",
"""|object Main {
| def foo() = {
| def deployment(
| fst: String,
| snd: Int = 1,
| ): Option[Int] = ???
| val abc = deployment(@@)
| }
|}
|""".stripMargin,
"""|fst = : String
|snd = : Int
|""".stripMargin,
topLines = Some(2),
)

check(
"default-args2",
"""|object Main {
| def deployment(
| fst: String,
| snd: Int = 1,
| ): Option[Int] = ???
| val abc = deployment(@@)
|}
|""".stripMargin,
"""|fst = : String
|snd = : Int
|""".stripMargin,
topLines = Some(2),
)

check(
"default-args3",
"""|object Main {
| def deployment(str: String)(
| fst: String,
| snd: Int = 1,
| ): Option[Int] = ???
| val abc = deployment("str")(
| @@
| )
|}
|""".stripMargin,
"""|fst = : String
|snd = : Int
|""".stripMargin,
topLines = Some(2),
)

check(
"default-args4",
"""|object Main {
| def deployment(str: String)(opt: Option[Int])(
| fst: String,
| snd: Int = 1,
| ): Option[Int] = ???
| val abc = deployment("str")(None)(
| @@
| )
|}
|""".stripMargin,
"""|fst = : String
|snd = : Int
|""".stripMargin,
topLines = Some(2),
)

// NOTE: In Scala 3.3.1 the tree for this test changed, which allowed easy fix
// For previous Scala 3 versions it shows wrong completions
check(
"default-args5",
"""|object Main {
| def deployment(str: String)(opt: Option[Int] = None)(
| fst: String,
| snd: Int = 1,
| ): Option[Int] = ???
| val abc = deployment("str")(
| @@
| )
|}
|""".stripMargin,
"""|abc: (String, Int) => Option[Int]
|""".stripMargin,
topLines = Some(1),
compat = Map(
">=3.3.1" ->
"""|opt = : Option[Int]
|""".stripMargin,
"2" ->
"""|opt = : Option[Int]
|""".stripMargin,
),
)

check(
"default-args6".tag(IgnoreScala2),
"""|object Main {
| def deployment(using str: String)(
| fst: String,
| snd: Int = 1,
| ): Option[Int] = ???
| val abc = deployment(using "str")(
| @@
| )
|}
|""".stripMargin,
"""|fst = : String
|snd = : Int
|""".stripMargin,
topLines = Some(2),
)

checkSnippet( // see: https://github.com/scalameta/metals/issues/2400
"explicit-dollar",
"""
Expand Down

0 comments on commit d2ccce2

Please sign in to comment.