-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add number separator related assists #3683
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
Conversation
This add 2 assists * Remove separators that remove all '_' from a number literal * Separate number by thousands/bytes/nibbles with separator
| r#"fn f() { let x = <|>0b001_0101_000_101_010; }"#, | ||
| r#"fn f() { let x = <|>0b00101010_00101010; }"#, | ||
| ) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a lot of tests :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure all the combinations are useful in the end they are essentially the same tests applied to all supported representations, can remove some if needed :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I think it might make sense to remove some of the boilerplate here with a table driven test:
for (before, after) in [("0b001_0101_000_101_010", "0b00101010_00101010") ... ]
Veetaha
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very nice!
From PR suggestion Co-Authored-By: Veetaha <[email protected]>
matklad
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't set the cursor position explicitly
Yeah, I think most of the assists shouldn't actually specify cursor position. We currently over-specify it, because our bulit-in diffing is imprecise and because (I think) VS Code used to not be so good at determining the cursor position afterwards.
To be clear, there are absolutelly assists where we want to place cursor smarter than the default (add derive, for example), but most of the boring transformations should be fine as is.
| r#"fn f() { let x = <|>0b001_0101_000_101_010; }"#, | ||
| r#"fn f() { let x = <|>0b00101010_00101010; }"#, | ||
| ) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I think it might make sense to remove some of the boilerplate here with a table driven test:
for (before, after) in [("0b001_0101_000_101_010", "0b00101010_00101010") ... ]
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| struct NumberLiteral { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty and general-purpose utility, so I think it's should live in ra_synax. I think the right API would be as follows:
- add
pub struct IntNumber(SyntaxToken);totokens.rs - suffix/preffix methods could return
Option<&str>then. I think ideally we might want evenfn suffix_range() -> Option<TextRange>,fn suffix_text() -> Option<&str>, but its ok to not add anything beyond this PR. - ideally, duplicaiton with
LiteralKindshould be removed. Perhaps it could return an enum of tokens, instead of a lowered enum? But that, again, is probably out of scope of this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main theme of the API we want is that, as long as possible, we should just give "views" into existing syntax nodes and tokens, rather than converting them into a POD rust types. Sort of the opposite of what you'd do in a traditional compiler.
| } | ||
| } | ||
|
|
||
| pub(crate) struct AssistVec<'a> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, this API looks good to me!
| str.replace("_", "") | ||
| } | ||
|
|
||
| pub(crate) fn remove_digit_separators(ctx: AssistCtx) -> Option<Assist> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put pub(crate) functions on top, and add "doc comments" to them (see other assists). These comments then go into auto-generated docs. No need to document every assist, but at least a single example would be useful.
|
ping @vbfox , in case this just accidently fell of from your radar :) |
|
closing due to inactivity, but feel free to reopen! |
10998: Add number representation assists r=Veykril a=errx Reimplemented assists from this PR #3683 with current APIs.   I've decided not to add options about size of the groups so behaviour is similar to clippy's. Minimal number length is also taken from clippy. Co-authored-by: Oleg Matrokhin <[email protected]>
tell people how to set miri flags fixes rust-lang/miri#3677
This PR proposes 2 new assists, inspired by the identical ones in jetbrains C# IDEs
I also did a few technical changes around the assist code:
add_assiststhat has the same syntax as adding a group but without assigning a group to the returned assists.find_covering_node_at_offsetto avoid cases where the selection start inside a number literal but ends further (like at the end of the file) triggering the assist.AssistHandlerimplementations that can return multiple assistsRemarks/Questions:
TextEditAFAIK), I suspect there is a diff algorithm applied somewhere to do a best guess of the cursor position. If specifying the cursor position is needed I'd prefer to PR an a similar feature in rust-analyzer than implementing it manually in analyzers.'_'character between the prefix and the digits for cases where there is a prefix (binary, hex) like0x_FF00_FF00I don't have an opinion about it but if anyone has a preference...Note: I plan to add also PR the conversion between bases at some point.