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

feat: Show substitution where hovering over generic things #18707

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ChayimFriedman2
Copy link
Contributor

There are few things to note in the implementation:

First, this is a best-effort implementation. Mainly, type aliases may not be shown (due to their eager nature it's harder) and partial pathes (aka. hovering over Struct in Struct::method) are not supported at all.

Second, we only need to show substitutions in expression and pattern position, because in type position all generic arguments always have to be written explicitly.

Closes #18688.

I chose a different approach than outlined in that issue. Instead of substituting the generic arguments inline, I display them separately. This is for two reasons: first, it is harder to inline them. Second, inlining them is a lossy process. For example, it makes where bounds less decipherable, and also it will mean we'll have to switch to show the hir-ty version of the definitions (instead of the hir-def version), which has other implications, e.g. some types are not shown well in hir-ty, and in general hir-def is more like what the programmer wrote, which I think is important for the familiarity of hover.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 17, 2024
@Veykril
Copy link
Member

Veykril commented Dec 18, 2024

Not reviewed yet, just dropping related issues #15010, #3588 and #13394 (this one has a couple comments with this as the topic in them)

@ChayimFriedman2
Copy link
Contributor Author

The most relevant I think are #13394, where this resolves the first request (into() works fine, I tried, and it's quite useful indeed), and #3588, which is basically the same request. This does not really contributes to fixing #15010 as this does not put substitution in type position at all (although it might be easier to do with this PR).

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(review for the actual presentation of things follows in a bit, will read up on the issues again first)

One general concern I have is whether this has any perf impact for things that do not care about the substs, that is mainly search and other stuff that does bulk resolution like syntax highlighting (this one you can easily bench via the corresponding integrated benchmark)

That aside, very nice, I've been looking forward to having this for quite some time :)

crates/hir-ty/src/infer.rs Outdated Show resolved Hide resolved
crates/hir/src/lib.rs Show resolved Hide resolved
///
/// This can take three values: `null` means "unlimited", the string `"hide"` means to not show generic substitutions at all, and a number means to limit them to X characters.
///
/// The default is 20 characters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a very low limit, 20 works for inlay hints as they are meant to be concise even if it means losing information. The main goal of hovering something is seeing information though so I feel like we should bump this higher.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took into account that there may be multiple generic parameters. When browsing with this feature on in the rust-analyzer repository, 20 was enough to see interesting things and not see uninteresting things (e.g. impl Fn, iterators etc.).

Copy link
Member

@Veykril Veykril Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw I wrote this thinking you'd be substituting the signature that is being shown not listing the substitutions separately in which case this limit is a lot more reasonable now 😅

@ChayimFriedman2
Copy link
Contributor Author

One general concern I have is whether this has any perf impact for things that do not care about the substs, that is mainly search and other stuff that does bulk resolution like syntax highlighting (this one you can easily bench via the corresponding integrated benchmark)

I was also worried about this, but the nice thing is that it turned out that this information is almost always readily available. The only things we do that we didn't do before are querying for the trait environment (which should be done once then cached), and just a tiny bit of AST juggling and hashmap lookups for the inference type of result. Which should have a marginal impact.

There are few things to note in the implementation:

First, this is a best-effort implementation. Mainly, type aliases may not be shown (due to their eager nature it's harder) and partial pathes (aka. hovering over `Struct` in `Struct::method`) are not supported at all.

Second, we only need to show substitutions in expression and pattern position, because in type position all generic arguments always have to be written explicitly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Concrete types on hover
3 participants