Skip to content

Inlay hints for method chaining pattern #3710

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

Merged
merged 6 commits into from
Mar 24, 2020

Conversation

M-J-Hooper
Copy link
Contributor

This PR adds inlay hints on method call chains:
image

It is not only explicit MethodCalls where this can be helpful. The heuristic used here is that whenever any expression is followed by a new line and then a dot, it resembles a call chain and type information can be #useful.

Changes:

  • A new InlayKind for chaining.
  • New option for disabling this type of hints.
  • Tree traversal rules for identifying the chaining hints.
  • VSCode decorators in the extension layer (and associated types).

Notes:

  • IntelliJ has additional rules and configuration on this topic. Eg. minimum length of chain to start displaying hints and only displaying distinct types in the chain.
  • I am checking for chaining on every ast::Expr in the tree; Are there performance concerns there?

This is my first contribution (to RA and to Rust in general) so would appreciate any feedback.
The only issue I can find the references this feature is #2741.

@lnicola
Copy link
Member

lnicola commented Mar 24, 2020

Thanks for your PR. I'm not the best person to review it, but I didn't see anything out of place.

There's a conflict on editors/code/src/config.ts, can you please rebase this?

Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Looks really great, thank you!

I am checking for chaining on every ast::Expr in the tree; Are there performance concerns there?

IMO, no big concerns, especially after we make them calculated for the current screen only: #3394

It feels like we need another iteration on hints shortening:
image

But that's out of scope of this PR.

Copy link
Contributor

@Veetaha Veetaha left a comment

Choose a reason for hiding this comment

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

Great thanks for the PR. Very clean code, I love it!

toDecoration(hint: ra.InlayHint.ChainingHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
return {
range: conv.asRange(hint.range),
renderOptions: { after: { contentText: ` ${hint.label}` } }
Copy link
Contributor

@Veetaha Veetaha Mar 24, 2020

Choose a reason for hiding this comment

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

This may be controversial, but we might want to add a colon there so that it may remind the proposed type ascription syntax for rust? I'd wait on other people's opinions too though.

Suggested change
renderOptions: { after: { contentText: ` ${hint.label}` } }
renderOptions: { after: { contentText: `: ${hint.label}` } }

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 did think about this, but it seemed to me that the reason for this colon is for this kind of inlay hint to exactly mimic the type syntax in a let statement. In the case of chaining there is no "real" syntax to mimic so I thought the simpler the better. Keen to hear others' opinions though.

Copy link
Member

Choose a reason for hiding this comment

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

Type ascription is supported on nightly.

None: Option<()>;

Copy link
Contributor

@Veetaha Veetaha Mar 25, 2020

Choose a reason for hiding this comment

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

I wonder if you can put a real type ascription on nightly between the chains of the method calls. I have a strong feeling that it will result in a syntax error, but bruh, the inlay hint must not always resemble a valid syntax.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need a : here

Copy link
Member

Choose a reason for hiding this comment

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

error: casts cannot be followed by a method call
 --> src/main.rs:4:13
  |
4 |     let a = vec![]: Vec<u8>.collect::<Vec<u8>>();
  |             ^^^^^^-^^^^^^^^
  |                   |
  |                   help: maybe write a path separator here: `::`
  |

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the way the hint is separated from the expression, but I agree that the text we display should sometimes be replaced with something different than Flatten<Chain<Chain<Once<Map<{unknown}, fn to_string<{unknown}>(&{unknown}) -> String>>, {unknown}>, Once<Option<String>>>>

Copy link
Member

Choose a reason for hiding this comment

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

The ideal hint would be impl Iterator<Item = String> when the type is Flatten<Chain<Chain<Once<Map<{unknown}, fn to_string<{unknown}>(&{unknown}) -> String>>, {unknown}>, Once<Option<String>>>>.

fn main() {
let c = A(B(C))
.into_b() // This is a comment
.into_c();
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I didn't have experience with chaining hints in Intelij, but shouldn't there be a hint for the last method call?
Though this may look weird since it will bias the semicolon and the type may be unnecessary duplicated as there is a variable type hint already.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, the reason I didn't include the final method is because it would be often redundant information. Here, the type hint would show let c: C = .... IntelliJ does the same, I would guess for similar reasons:
image

@M-J-Hooper M-J-Hooper force-pushed the chaining-inlay-hints branch from 2621611 to ab086e8 Compare March 24, 2020 22:51
@M-J-Hooper
Copy link
Contributor Author

M-J-Hooper commented Mar 24, 2020

Thanks for the quick replies and feedback. I've rebased and made some amendments accordingly.

@matklad
Copy link
Member

matklad commented Mar 24, 2020

CI's failing, looks like some test need adjusting

@M-J-Hooper M-J-Hooper force-pushed the chaining-inlay-hints branch from 83c43e9 to 7b35da0 Compare March 24, 2020 23:19
@matklad
Copy link
Member

matklad commented Mar 24, 2020

Excellent PR, and very easy to review, as everything is top notch!

Thanks @M-J-Hooper !

bors r+

And yeah, we really should try to include this into the LSP proper...

@bors
Copy link
Contributor

bors bot commented Mar 24, 2020

@bors bors bot merged commit 6ad1a07 into rust-lang:master Mar 24, 2020
@matklad
Copy link
Member

matklad commented Mar 27, 2020

opened #3750

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants