-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Migrate reorder_fields
Assist to Use SyntaxFactory
#18495
Migrate reorder_fields
Assist to Use SyntaxFactory
#18495
Conversation
fields: impl Iterator<Item = T>, | ||
sorted_fields: impl IntoIterator<Item = T>, | ||
) { | ||
fields.zip(sorted_fields).for_each(|(field, sorted_field)| { | ||
ted::replace(field.syntax(), sorted_field.syntax().clone_for_update()) | ||
editor.replace(field.syntax(), sorted_field.syntax().clone_for_update()) |
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.
We need to eliminate this clone_for_update
using SyntaxFactory
. It looks like we will need to add constructors for record_expr_field
and record_pat_field
. For now, I would inline this replace
function so you don't need to worry about how to abstract it over the two cases. We can try to clean it up afterwards.
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.
Inlined the editor.replace()
calls, but the clone_for_update()
calls are still needed. Removing them causes the assist to fail tests with an ‘immutable tree’ error
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.
Removing them causes the assist to fail tests with an ‘immutable tree’ error
Right, we do still need a mutable node, but my understanding is that we want to hide this under SyntaxFactory
so it is abstracted away and so that we track the mappings, hence my suggestion of adding those constructors. That said, in these cases where we do just want to clone, those constructors might not be very convenient to use. @DropDemBits can you check my understanding here?
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.
@darichey Generally we'd like to hide the details of creating new nodes under SyntaxFactory
. In this case though, we're picking nodes from the original syntax tree so we wouldn't need to go through a SyntaxFactory
.
SyntaxEditor
should handle transform any node in an edit that reference the original syntax tree into their mutable equivalents, but seems like I forgot this case for the new nodes in replace edits 😅. For now it should be okay to use clone_for_update
and add a fixme, and then we can remove those fixmes once SyntaxEditor
is fixed.
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.
Got it, thank you! Sorry about that, @tareknaser, you can revert inlining replace
and just add a FIXME for the clone_for_update :)
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.
All set 5c41c20
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.
Sorry for the churn, this should be good after this!
fn replace<T: AstNode + PartialEq>( | ||
fields: impl Iterator<Item = T>, | ||
sorted_fields: impl IntoIterator<Item = T>, | ||
) { | ||
fields.zip(sorted_fields).for_each(|(field, sorted_field)| { | ||
ted::replace(field.syntax(), sorted_field.syntax().clone_for_update()) | ||
}); | ||
} | ||
|
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.
So just bring this back and add like
// FIXME: remove clone_for_update when SyntaxEditor handles it for us
editor.replace(field.syntax(), sorted_field.syntax().clone_for_update());
Signed-off-by: Tarek <tareknaser360@gmail.com>
bef15fc
to
5c41c20
Compare
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.
Awesome!
This PR is part of #15710 and #18285