haskell-language-server icon indicating copy to clipboard operation
haskell-language-server copied to clipboard

Type information for arbitrary portions of code

Open jacg opened this issue 6 years ago • 13 comments

ghcide gives me information on most alphanumeric symbols (and numeric literals) but there are two features that I'm used to from dante and/or intero:

  • type information on operators
  • type information on arbitrary selected expressions

Do these features exist, and have I simply not found them?

If not, any hints on how to implement them?

jacg avatar Sep 14 '19 21:09 jacg

I'm able to see type information on operators. 2019-09-14-221120_723x423_scrot

mitchellwrosen avatar Sep 15 '19 02:09 mitchellwrosen

Operators should definitely work, given that it seems to work in some cases, it would be good if you could provide a specific test case where it fails.

Getting type information of a range is a bit more tricky. It is obviously more complex to implement but there is a second issue here: LSP hover does only give us a position not a range. I’ve taken a quick look at the spec and I also couldn’t find any other method that we could use here either.

cocreature avatar Sep 16 '19 10:09 cocreature

Operators should definitely work

Sorry, yes, they do (it's just that in my configuration symbols' types were being shown spontaneously, while it took a bit more effort for symbols).

jacg avatar Sep 18 '19 21:09 jacg

I guess that the bug label isn't appropriate here, as everything that is expected to work (information about symbols) seems to work.

jacg avatar Oct 07 '19 08:10 jacg

The type of arbitrary expression can be extracted indirectly in hls, using the eval plugin, that includes :t. Not sure if it is possible right now to translate in the hover, using the lsp protocol as pointed by @cocreature. So we should wait for some spec update to implement it.

@jacg does the workaround using eval works for you?

jneira avatar Sep 09 '20 12:09 jneira

That question should be addressed by me nearly twin @jacg :) Cheers,

JacoboDominguez avatar Sep 09 '20 20:09 JacoboDominguez

FYI https://github.com/microsoft/language-server-protocol/issues/377

alanz avatar Sep 11 '20 22:09 alanz

The type of arbitrary expression can be extracted indirectly in hls, using the eval plugin, that includes :t.

@jneira Could you please give some more advice on how to install/ configure?

normenmueller avatar Nov 22 '20 11:11 normenmueller

@normenmueller you have to install haskell-language-server which includes by default the eval plugin (you can see it in action here ).

Then you can write -- >>> in the code to evaluate expressions in the module context:

-- >>> :t fooExe
fooExe :: [Char]
fooExe = "hi"
  • pressing a link Evaluate
-- >>> :t fooExe
-- fooExe :: [Char]
fooExe :: [Char]
fooExe = "hi"

You can write arbitrary expressions:

-- >>> :t "hi"
fooExe :: [Char]
fooExe = "hi"
-- >>> :t "hi"
-- "hi" :: [Char]
fooExe :: [Char]
fooExe = "hi"

jneira avatar Nov 22 '20 21:11 jneira

@alanz commented in the issue originally in hls:

I guess someone needs to actually make a PR for microsoft/language-server-protocol#377

jneira avatar Jan 19 '21 07:01 jneira

“Type information on arbitrary selected expressions” is the killer feature of any Haskell IDE.

vim-hdevtools has that. Actually, what it has works a little bit differently:

  1. Press the F1 key and it will select the token which the cursor is on, and tell you the type of the token.
  2. Press the F1 key again, and it will expand the selection to the next outer subexpression and tell you the type of that.

... and you can repeat step 2 as many times as you like.

It would be great if we could have that feature again with HLS.

jamesdbrock avatar Jun 24 '21 04:06 jamesdbrock

As @jamesdbrock gently pointed in #1973, this feature used to work in haskell-ide-engine (see the pr in the vscode extension which added the feature here) Maybe it worths investigate how was done in hie, althoug it seems it used ghcmod underneath, a deprecated library that should not be used.

jneira avatar Jun 24 '21 09:06 jneira

I found this comment explaining a bit on how they implemented this in rust-analyzer: https://github.com/microsoft/language-server-protocol/issues/377#issuecomment-889898650 -> maybe this is useful to guide the possible implementation efforts?

Martinsos avatar Feb 11 '22 10:02 Martinsos

@alanz @mpickering does anyone have knowledge whether this is difficult in ghcide itself (getting type of arbitrary expression), ignoring the issues with LSP and clients?

Maybe we can make some step by step progress.

hasufell avatar Jan 04 '23 05:01 hasufell

For type information, I bet we can hack something together using HIE files and hiedb.

fendor avatar Jan 04 '23 10:01 fendor