gleam-lang / gleam

⭐️ A friendly language for building type-safe, scalable systems!
https://gleam.run
Apache License 2.0
17.6k stars 735 forks source link

LSP: Show module docs when hovering a module #3451

Open kwando opened 2 months ago

kwando commented 2 months ago

I use the "View on hexdocs" link all the time but it doesn't work on modules 🙃

// show docs on hover here would be nice
import  mymodule

// show docs on hover my module here would be nice
mymodule.do_something()

There is bigger fish to fry, but still :)

lpil commented 2 months ago

Good idea, thank you.

Baumple commented 2 months ago

Hey i would like to try myself on this issue :D. I found that you could get the module information via the compiler instance through its module map but the ast.documentation field from the returned module is empty. I think its because its never set in the gleam_core::parse::Parser::parse_module function.

But im not really sure if thats actually the case or if overlooked something and its set somewhere else?

Baumple commented 1 month ago

What I tried so far (https://github.com/Baumple/gleam/commit/f2ec2d0007aaa30a0db040e116b9b0d567d629f1) is to add another field to the Parser structs which holds the module documentation and i updated the Token::CommentModule item to also hold the contents of the comment (similar to how its done with Token::CommentDoc) so the contents get picked up by the Parser::next_tok function.

This works for local module imports so far.

Is this approach somewhat okay or do you have improvement ideas haha?

Frank-III commented 1 month ago

I found that you could get the module information via the compiler instance through its module map but the ast.documentation field from the returned module is empty. I think its because its never set in the gleam_core::parse::Parser::parse_module function.

I think you should check out the return type from parse_module which contains UntypedModule and ModuleExtra. The documentation for the module is just the module_comments field in the ModuleExtra struct.

This works for local modules so far.

For local modules, you have access to the compiled_modules in the LspProjectCompiler which contains the documentation already, which is fairly straightforward to implement.

I don't have a good idea of how to do that for imported modules

Baumple commented 1 month ago

Thank you for the response :D. Yess i saw that, but the module_comments field in ModuleExtra is a vector of SrcSpan and im not sure how to resolve them to the actual documentation from there on. Is there a function or a way to do it?

Im already using the field with the compiled modules but they themselves do not have a documentation field. Only their ast field has a documentation field but its empty.

Baumple commented 1 month ago

I worked on module imports a bit more and the language server is now able to show the hexdocs link when hovering an import of a "foreign" module :).

It doesn't do the actual documentation yet.

I havent found a proper way to get the module documentation but there must be some way since it gets picked up when building the documentation so i must be missing something..

Regarding calling module functions: I dont really know how to implement this since there is no variant to match on and it seems like the module is part of the entire function call Expression. Is there a way to "differentiate" between the two or is it maybe necessary to introduce new variants?

(https://github.com/Baumple/gleam/commit/817cdd4a5bca2f9bf2d2764633bfe66a6bdf2e62)