Skip to content
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

Backport "bugfix: Named args completions with default values" to LTS #20694

Merged
merged 1 commit into from
Jun 20, 2024
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 @@ -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
Loading