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 2, 2023
1 parent 09ea77e commit 6f43fee
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 4 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,111 @@ class CompletionArgSuite extends BaseCompletionSuite:
""
)

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

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

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

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

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


// @Test def `explicit-dollar` =
// checkSnippet( // see: https://github.com/scalameta/metals/issues/2400
// """
Expand Down

0 comments on commit 6f43fee

Please sign in to comment.