rescript-lang / rescript-vscode

Official VSCode plugin for ReScript
MIT License
328 stars 56 forks source link

Does auto-completion on pipe operator only worked for type t in Module? #792

Closed Mng12345 closed 1 year ago

Mng12345 commented 1 year ago

See the gif screen-shot below: mnggiflab-compressed-from-screen-recorder-2023_07_0417_44_03 The example code is here:

type m = {
  name: string,
  age: int,
}

module M1 = {
  type t = m

  let getName = (m: t): string => {
    m.name
  }
}

module M2 = {
  type t = m

  type m = m
  let getAge = (m: t): int => {
    m.age
  }

  let getAge2 = (m: m): int => {
    m.age
  }
}

type m2 = M2.m

type m3 = M2.t

let m1: m = {
  age: 1,
  name: "m",
}

let m2: m2 = {
  age: 1,
  name: "m",
}

let m3: m3 = {
  age: 1,
  name: "m",
}

envrionment info: rescript v10.1.2 rescript-vscode v1.18.0 I have debugged the analysis, the auto completion for m-> walked into this switch, the variable pathRev is an empty list, is auto completion for normal type (not type t of a module) not considered for now? image

zth commented 1 year ago

This is about resolving type aliases. When resolving type aliases, we stop at t specifically because we interpret that as an intention that t is the main type of a module.

For types that aren't aliases, you'll get completions from the full module.

Mng12345 commented 1 year ago

So, if a type t alias some type m, then the auto-completion of a variable a(assume it's type is annotated with m) should be considered the same as type t. This form of auto-completion is usable in other languages like Scala (this is called implicit class, i think we can make the same effect in ide). I think this is very useful, you can just define a type m, and alias it in two or three and more different module if the business need, then you can get the auto-completions of all the function of different modules. This feature would be very cool!

Mng12345 commented 1 year ago

Another point i think is also very useful is auto completion of aliased module, if a type t is defined in module LongPath.M, and you aliased the module LongPath.M in current scope with module M, the auto-completion of variable a with type LongPath.M.t should be replaced by module M not LongPath.M. The module System of ReScript and OCaml is very flexible, may be the ide editor limited it's power as the Short aliased module would not trigger auto completion for now.

fhammerschmidt commented 1 year ago

I think aliased record types should expand as well, except if they are private. Currently, I copy a lot of type definitions over to have autocomplete. This does not feel right.

zth commented 1 year ago

Can you write a few examples of what that means and what the trade offs would be?

fhammerschmidt commented 1 year ago

I created a failing test here: https://github.com/rescript-lang/rescript-vscode/pull/834 The weird thing is, it should work according to https://github.com/rescript-lang/rescript-vscode/issues/557, but doesn't

zth commented 1 year ago

Sorry but I still don't understand the problem. That failing test is for a hover, but here you're talking about autocomplete. And this issue is about pipe autocompletion specifically. Could you clarify exactly what the problem(s) are and how they relate to this issue (pipe completion)?

Mng12345 commented 1 year ago

@zth Hi, i think @fhammerschmidt means that when we define a type a which is aliased to a type t, t is defined in a module, the pipe completion of variables with type t are worked, but not worked with type a.

fhammerschmidt commented 1 year ago

Sorry for hijacking this issue, and for using hover and autocomplete synonymous. I tested autocomplete and it works for aliased records (I thought it does not), but not hover.

Mng12345 commented 1 year ago

Wow! This is worked now!!! image