Skip to content

Conversation

@IceSentry
Copy link
Contributor

@IceSentry IceSentry commented Apr 8, 2020

This will now hide value, pat, rhs and other. These words were selected from the std because they are used in commonly used functions with only a single param and are obvious by their use.

It will also hide the hint if the passed param starts or end with the param_name. Maybe we could also split on '_' and check if one of the string is the param_name.

I think it would be good to also hide bytes if the type is [u8; n] but I'm not sure how to get the param type signature.

Closes #3900

This will now hide "value", "pat", "rhs" and "other"
These words were selected from the std because they are used in common functions with only a single param and are obvious by their use.
I think it would be good to also hide "bytes" if the type is `[u8; n]` but I'm not sure how to get the param type signature

It will also hide the hint if the passed param starts or end with the param_name
@IceSentry IceSentry marked this pull request as ready for review April 8, 2020 21:52
@IceSentry
Copy link
Contributor Author

I added a way to ignore &mut and & in the argument_name, but I'm not sure if there is a more efficient way to check this.

) -> bool {
let argument_string = argument.syntax().to_string();
if param_name.is_empty() || argument_string.ends_with(param_name) {
let argument_string = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, this is better expressed as if let ast::RefExpr(argument) = argument.

Probably better expressed as a helper function:

fn remove_reference(exr: ast::Expr) -> ast::Expr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, I tried to make a function that strips the Ref part, but I ended up with a token which I didn't know how to convert back to an ast::Expr. Instead I made a fn is_argument_similar_to_param(argument, param_name) -> bool. It makes the intent pretty explicit, but it's less reusable.

"predicate" | "value" | "pat" | "rhs" | "other" => true,
_ => false,
};
if parameters_len == 1 && (param_name.len() == 1 || is_obvious_param_name) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's extract the (param_name.len() == 1 || is_obvious_param_name) into a

fn is_obvious_param(param: &str) -> bool

function?

@@ -1,4 +1,4 @@
//! FIXME: write short doc here
//! This module defines multiple types of inlay hints and their visibility
Copy link
Contributor

Choose a reason for hiding this comment

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

😍


fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool {
let argument_string = if let ast::Expr::RefExpr(ref_expr) = argument {
ref_expr.syntax().last_token().expect("RefExpr should have a last_token").to_string()
Copy link
Contributor

@matklad matklad Apr 9, 2020

Choose a reason for hiding this comment

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

This expect can in theory fire. I suggest doing this:

fn remove_ref(expr: ast::Expr) -> ast::Expr {
    if let ast::Expr::RefExpr(ref_expr) = &expr {
        if let Some(inner) = ref_expr.expr() {
            return expr;
        }
    }
    expr
}

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 didn't realize there was a .expr() on the RefExpr. I'll fix that!

@matklad
Copy link
Contributor

matklad commented Apr 9, 2020

bors r+

Thanks!

bors bot added a commit that referenced this pull request Apr 9, 2020
3901: Add more heuristics for hiding obvious param hints r=matklad a=IceSentry

This will now hide `value`, `pat`, `rhs` and `other`. These words were selected from the std because they are used in commonly used functions with only a single param and are obvious by their use.

It will also hide the hint if the passed param **starts** or end with the param_name. Maybe we could also split on '_' and check if one of the string is the param_name.

I think it would be good to also hide `bytes` if the type is `[u8; n]` but I'm not sure how to get the param type signature.

Closes #3900 



Co-authored-by: IceSentry <c.giguere42@gmail.com>
@bors
Copy link
Contributor

bors bot commented Apr 9, 2020

Build failed

@lnicola
Copy link
Member

lnicola commented Apr 9, 2020

I think the test failure is spurious. Can someone retry this?

@edwin0cheng
Copy link
Contributor

bors retry

@bors
Copy link
Contributor

bors bot commented Apr 9, 2020

@bors bors bot merged commit 9635d8b into rust-lang:master Apr 9, 2020
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.

[Feature request] Hide obvious parameter name inlay hints

4 participants