Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type annotation on code selection #3059

Closed
barambani opened this issue Dec 11, 2019 · 6 comments · Fixed by #3060
Closed

Type annotation on code selection #3059

barambani opened this issue Dec 11, 2019 · 6 comments · Fixed by #3060
Assignees
Labels
feature-request Used for feature requests

Comments

@barambani
Copy link

Is your feature request related to a problem? Please describe.
It would be nice to be able to inspect the type of a code selection (not necessarily only a single word like it is now).

Describe the solution you'd like
Something similar to what Intellij does when hitting Ctrl+Shift+p on selected code. For example, in a snippet like

final def flatMap[C](f: B => Read[A, C]): Read[A, C] = 
    Read.instance(a => read(a).flatMap(f(_).read(a)))

selecting in the editor the text

f(_).read(a)

it would be nice to be able to query for its type.

Describe alternatives you've considered
The only alternative I know about is using Intellij

Search terms:
snippet type annotation selection

@gabro
Copy link
Member

gabro commented Dec 11, 2019

As far as I know, this is technically doable (@olafurpg correct me if I'm wrong), however, LSP limits the hover request to single words so this would require a custom extension.

@barambani
Copy link
Author

I would be happy to try and see if I can help in moving this on. I love this feature and would mean I could move completely to metals at least for Scala. Do you think this is something that can be taken by someone not familiar with the design and the code ? In case, any pointer, documentation or design discussion would help a lot. Thanks.

@kubukoz
Copy link
Contributor

kubukoz commented Feb 6, 2020

Could this be just a code action? If the code highlighted isn't a complete block (e.g. you highlighted just .read(a), we would not show that action, or show a warning when it's selected. The type could show up in a window, or a text decoration, worksheet-style.

@reibitto
Copy link

reibitto commented May 4, 2020

I use this feature in IntelliJ a lot. It doesn't initiate on hover in IntelliJ either. Being able to trigger a code action with a shortcut that shows a tooltip with the type information would be how I envisioned this as well.

I find this especially useful if you have a long method chain and want to see the intermediate type. Like in cases like this:

But not automatically overlayed like this (I have that feature off). I usually highlight and press a shortcut to see that info.

@reibitto
Copy link

reibitto commented May 5, 2020

Hmm, so this feature actually seems closer to done that I originally thought. Sorry, I'm trying the newer Metals for the first time and still discovering things.

If you hover over a function, Metals does seem to take the context into account and show the type. Sometimes it's under "Expression type:" and sometimes it's shown at the end of the method signature's return type. It's a little hard to visually parse though since it shows up in different places depending on the situation. It would be nice to have a separate command that cuts out the rest of the information. A pure "Show type" command rather than bringing up scaladoc and all that. It would result in a much smaller tooltip with only the information you're querying about.

Besides that though, is it possible to bring up the tooltip without using the mouse right now? I'm unable to find it. Just being able to do that would be a big win in the meantime I think.

@olafurpg
Copy link
Member

olafurpg commented May 5, 2020

@reibitto you can register a shortcut for the "show hover" command in VS Code

Screenshot 2020-05-05 at 10 36 32

The LSP spec only supports "hover" requests at an offset position instead of a range position. The LSP spec also doesn't distinguish between "show me type + scaladoc" and "show me type only" requests. Metals can easily respond to any combination of request (range/offset positions, scaladoc/type-only), we're only missing the ability in the editor clients (VS Code, Vim, ..) to send these requests.

@tgodzik tgodzik transferred this issue from scalameta/metals-feature-requests Aug 21, 2021
@tgodzik tgodzik added the feature-request Used for feature requests label Aug 21, 2021
@dos65 dos65 added this to the Metals v0.10.8 milestone Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Used for feature requests
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants