Feat: extracted method from trait impl is placed in existing impl
Before
https://user-images.githubusercontent.com/1759192/183872883-3b0eafd2-d1dc-440e-9e66-38e3372f8b64.mp4
After
https://user-images.githubusercontent.com/1759192/183875769-87f34c7d-52f0-4dfc-9766-f591ee738ebb.mp4
Previously, when triggering a method extraction from within an impl trait block, then this would always create a new impl block for the struct, even if there already is one. Now, if there is already an existing trait-less impl block, then it'll put the extracted method in there.
Caveats:
- It currently requires the target impl block to be non-empty. This limitation is because the current architecture takes a
node_to_insert_afteras reference for where to insert the extracted function. An empty impl block doesn't have such a reference node, since it's empty. It seems that supporting this requires a much larger and more complex change. - This is my first contribution in rust, so apologies for any beginner mistakes.
Nice improvement! One question on the expected behavior after this change: what happens when the original impl block is in a different file than the impl Trait for Struct block? Should it place the method in the original impl block in the other file, or create a new impl block in the same file the trait implementation is in?
In this PR, I only considered impl blocks in the same file, as the expected behaviour is quite clear here. So currently, if the original impl block is in a different file, then it creates a new block in the same file.
Once we go across file boundaries it'll get more fuzzy I think, we might have multiple blocks to consider in different files, or they might be in external crates. I'm not quite sure what the "correct" behaviour should be there, hence I left that alone.
One of the files in this isn't formatted with latest stable rustfmt it seems
@bors r+
:pushpin: Commit f24fbc20274962860e15e1160bfdaade543092bf has been approved by Veykril
It is now in the queue for this repository.
:hourglass: Testing commit f24fbc20274962860e15e1160bfdaade543092bf with merge d3d380656555302fba8b9ba7b3d9a8c7a89d314f...
:sunny: Test successful - checks-actions Approved by: Veykril Pushing d3d380656555302fba8b9ba7b3d9a8c7a89d314f to master...