rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Feat: extracted method from trait impl is placed in existing impl

Open TiddoLangerak opened this issue 3 years ago • 3 comments

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_after as 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.

TiddoLangerak avatar Aug 10 '22 10:08 TiddoLangerak

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?

DorianListens avatar Aug 13 '22 17:08 DorianListens

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.

TiddoLangerak avatar Aug 13 '22 18:08 TiddoLangerak

One of the files in this isn't formatted with latest stable rustfmt it seems

Veykril avatar Sep 12 '22 12:09 Veykril

@bors r+

Veykril avatar Nov 07 '22 11:11 Veykril

:pushpin: Commit f24fbc20274962860e15e1160bfdaade543092bf has been approved by Veykril

It is now in the queue for this repository.

bors avatar Nov 07 '22 11:11 bors

:hourglass: Testing commit f24fbc20274962860e15e1160bfdaade543092bf with merge d3d380656555302fba8b9ba7b3d9a8c7a89d314f...

bors avatar Nov 07 '22 11:11 bors

:sunny: Test successful - checks-actions Approved by: Veykril Pushing d3d380656555302fba8b9ba7b3d9a8c7a89d314f to master...

bors avatar Nov 07 '22 11:11 bors