Skip to content

Commit 4494a76

Browse files
Fix completions for named tuples (#24169)
fixes scalameta/metals#7764 This fix was partially implemented during the [Scala Tooling Spree](https://scalameta.org/scala-tooling-spree/) Co-authored-by: Jakub Kozłowski <kubukoz@gmail.com>
1 parent 89e617d commit 4494a76

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

compiler/src/dotty/tools/dotc/interactive/Completion.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ object Completion:
187187
Some(qual)
188188
case _ => None
189189

190+
private object NamedTupleSelection:
191+
def unapply(path: List[tpd.Tree])(using Context): Option[tpd.Tree] =
192+
path match
193+
case (tpd.Apply(tpd.Apply(tpd.TypeApply(fun, _), List(qual)), _)) :: _
194+
if fun.symbol.exists && fun.symbol.name == nme.apply &&
195+
fun.symbol.owner.exists && fun.symbol.owner == defn.NamedTupleModule.moduleClass =>
196+
Some(qual)
197+
case _ => None
198+
190199

191200
/** Inspect `path` to determine the offset where the completion result should be inserted. */
192201
def completionOffset(untpdPath: List[untpd.Tree]): Int =
@@ -195,7 +204,7 @@ object Completion:
195204
case _ => 0
196205

197206
/** Handle case when cursor position is inside extension method construct.
198-
* The extension method construct is then desugared into methods, and consturct parameters
207+
* The extension method construct is then desugared into methods, and construct parameters
199208
* are no longer a part of a typed tree, but instead are prepended to method parameters.
200209
*
201210
* @param untpdPath The typed or untyped path to the tree that is being completed
@@ -242,6 +251,7 @@ object Completion:
242251
completer.scopeCompletions.names ++ completer.selectionCompletions(qual)
243252
case tpd.Select(qual, _) :: _ => completer.selectionCompletions(qual)
244253
case (tree: tpd.ImportOrExport) :: _ => completer.directMemberCompletions(tree.expr)
254+
case NamedTupleSelection(qual) => completer.selectionCompletions(qual)
245255
case _ => completer.scopeCompletions.names
246256

247257
interactiv.println(i"""completion info with pos = $pos,

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,28 @@ class CompletionSuite extends BaseCompletionSuite:
20532053
""".stripMargin,
20542054
)
20552055

2056+
@Test def `namedTuple completions-3` =
2057+
check(
2058+
"""|import scala.NamedTuple.*
2059+
|
2060+
|val person = (name = "Jakub", city = "Wrocław")
2061+
|
2062+
|val n = person.@@name""".stripMargin,
2063+
"name: String",
2064+
filter = _ == "name: String"
2065+
)
2066+
2067+
@Test def `namedTuple completions-4` =
2068+
check(
2069+
"""|import scala.NamedTuple.*
2070+
|
2071+
|val person = (name = "Jakub", city = "Wrocław")
2072+
|
2073+
|val n = person.n@@ame""".stripMargin,
2074+
"name: String",
2075+
filter = _ == "name: String"
2076+
)
2077+
20562078
@Test def `Selectable with namedTuple Fields member` =
20572079
check(
20582080
"""|import scala.NamedTuple.*
@@ -2285,7 +2307,7 @@ class CompletionSuite extends BaseCompletionSuite:
22852307
|""".stripMargin,
22862308
"asTerm: Term"
22872309
)
2288-
2310+
22892311
@Test def `derives-no-square-brackets` =
22902312
check(
22912313
"""

0 commit comments

Comments
 (0)