scalameta / metals-feature-requests

Issue tracker for Metals feature requests
37 stars 4 forks source link

Autocomplete for elements of a NamedTuple #397

Open t9dupuy opened 2 weeks ago

t9dupuy commented 2 weeks ago

Is your feature request related to a problem? Please describe.

Elements of a NamedTuple do not appear in the suggested members/methods dropdown.

Describe the solution you'd like

The elements of a NamedTuple should be suggested for autocompletion just like the members of a class or the elements of a regular tuple (_1, _2...)

For example:

val nt = (integer = 0, string = "hello")

both integer and string should be suggested when writing nt.

Describe alternatives you've considered

n/a

Additional context

No response

Search terms

namedtuple

dragove commented 6 days ago

I've noticed that the type of named tuple is odd. when I ask the type of named tuple in the following variable x

val x = (name = "Dove", age = 233)

it shows

Expression type:
(name : String, age : Int)
Symbol signature:
val x: (name : String, age : Int)

but if I ask the type of x in following context

println(x.name) // note: the .name should be here to reproduce this behavior

it shows

Expression type:
Elem[(String, Int), 0]
Symbol signature:
extension [N <: Tuple, V <: Tuple](x: V) inline def apply(n: Int): Elem[V, n.type]

I don't know if it is expected or a bug.

t9dupuy commented 5 days ago

I believe this is expected. Dot notation (.name) on namedtuples is just a syntactic trick desugarized by the compiler to a method call (here .apply(0)).

Implementation: https://github.com/scala/scala3/blob/f539112a9fa70a5574545a30457261b69c6e844d/library/src/scala/NamedTuple.scala#L144

tgodzik commented 5 days ago

Looks like that could be improved though also. I think it's just a missing case.