-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Support inlay hint type hint location #21505
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
[ty] Support inlay hint type hint location #21505
Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
|
|
There are a lot of false positives (random targets) here.
I think this is a good test but probably not a good solution. Interested to see people's thoughts. |
|
Thanks to work on this. It feels wrong to me to render the type to a string, and then trying to reparse the information. I think we should either:
We do have a similar problem for showing go to targets on hover. It's slightly different than inlay but we would want to collect all types, resolve their definitions, and render the definitions in a separate list. That's why I think a visitor approach where one implementation writes to a normal formatter, another that creates the inlay hint parts, and a third that writes to a normal formatter and collects the go to definition targets seems best? But curious to hear what @Gankra thinks. She worked on this code more than I |
For reference, we have a
|
|
I think we would want something more specific to rendering types. I very much doubt that we need separate visitor methods for each type. We're more interested in getting "hook-in-points" to know when we enter a new type and when we exit it, as well as having a way to write the label for the current type. |
I agree It also feels wrong to duplicate display logic from Could we make changes in Meaning we can get all the information we need from the intermediate struct. |
I think this would work but has two downsides:
The approach I would try is to define a new trait: pub trait DisplayTypeFormatter<'db> {
fn enter_type(&mut self, ty: Type<'db>) {}
fn exit_type(&mut self) {}
fn write_str(&mut self, part: &str);
}I think we can even implement impl std::fmt::Write for dyn DisplayTypeFormatter<'_> {
fn write_str(&mut self, text: &str) -> std::fmt::Result<()> {
DisplayFormatter::write_str(self, text);
Ok(())
}
}The implementation to formatting the type to a string is: struct DisplayToString(String);
impl DisplayTypeFormatter<'_> for DisplayToString {
fn write_str(&mut self, part: &str) {
self.0.push_str(part);
}
}Extracting the inlay is a bit tricky. It will require maintaining a stack of inlay parts where you push to the stack when entering a new type and popping from the stack when exiting (the stack is your scratch pad and you move them to a finalized state once finished. The hover implementation wraps a Note: I haven't tried any of this, so this might not work or require more methods or different signatures but I think this is worth exploring. |
|
Oh how serendipitous, I was just thinking about implementing this. We already have the visitor pattern in our display code, it's used by signature_help. I would absolutely implement this feature by expanding upon this design: ruff/crates/ty_python_semantic/src/types/display.rs Lines 1099 to 1109 in 192c37d
ruff/crates/ty_python_semantic/src/types/display.rs Lines 1155 to 1160 in 192c37d
|
|
Cool, I'll close this for now and let you do your thing, I'm happy to link it up to the inlay hints if you don't get to that. I will also think about a better way to snapshot the targets. |
Summary
Resolves astral-sh/ty#1078.
Screencast_20251117_212227.webm.
To find valid identifiers in the display string we do the following.
Test Plan
Our current snapshots for inlay hint locations are very hard to understand and frankly arent useful at all.