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 Sep 19, 2023
1 parent d7be734 commit 7760bd6
Show file tree
Hide file tree
Showing 3 changed files with 122 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,22 @@ object NamedArgCompletions:
.getOrElse(baseArgs)
.filterNot(isUselessLiteral)

def isDefaultParam(t: Tree): Boolean = t match
case Ident(name) => name.is(DefaultGetterName) // default args
case Select(_, name) =>
// default args for methods defined in object
name.is(DefaultGetterName)
// for default parameters in not-first parameter list
// eg. def m(fst: Int)(snd: Int)(arg1: Int, arg2: Int = 123) = ???
case Apply(fun, _) => isDefaultParam(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, _) => isDefaultParam(arg)
}
.map {
case (NamedArg(name, _), _) => name
Expand Down
105 changes: 105 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,111 @@ 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),
)

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,
"""|opt = : Option[Int]
|""".stripMargin,
topLines = Some(1),
)

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 7760bd6

Please sign in to comment.