gleam icon indicating copy to clipboard operation
gleam copied to clipboard

[WIP] Update LSP hover action to return labelled definition

Open manuraj17 opened this issue 1 year ago • 2 comments

Relates to https://github.com/gleam-lang/gleam/issues/2653

Updating the LSP hover action to return the full function head, as per the documentation.

The planned format is as below

<fn-accessor-type> fn <function-name>(<label> <argument-name>: <type>, ..) -> <return-type> 

Eg: For a function like below

fn greet(firstname name:String) { io.println("Hello " <> name) }

The generated string would be

fn greet(firstname name: String) -> Nil 

Current implementation uses pretty print and existing Fn Type enum. Since labels are not part of the type system, we are going ahead with simple string interpolation for now.

P.S : This is the first time working with the codebase and such a big Rust repo, hence please feel free to suggest changes and improvements.

P.P.S: I am trying to test it out in local hence kept the [WIP] tag.

manuraj17 avatar Mar 19 '24 21:03 manuraj17

Considering the sampe like below

import gleam/io
import gleam/string

fn greet(n name: String) -> String {
  string.append(name, "hello")
}

pub fn main() {
  io.println(greet("Tom"))
}

Some of my findings (being new to the language)

  • Hovering on main() our Lsp identifies the keyword as a function and it works fine
  • Hovering on greet on this line io.println(greet("Tom")) , it get idenfitied as expression here

Will need to identify how to handle that case there. Plus, I am also trying to understand how to quickly identify if the expression in this method here

Tried defining something like below but seems to not to work

    /// Returns `true` if the typed expr is [`Fn`].
    ///
    /// [`Fn`]: TypedExpr::Fn
    #[must_use]
    pub fn is_function(&self) -> bool {
        matches!(self, Self::Fn { .. })
    }

cc @lpil

manuraj17 avatar Mar 23 '24 11:03 manuraj17

The case you are probably looking for there is a Self::Call not a Self::Fn fn is a function definition whereas call is when the function is actually being used

Acepie avatar Mar 30 '24 14:03 Acepie