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

[Feature request] Create file from function

Open namse opened this issue 2 years ago • 2 comments

For javascript & typescript, vscode supports the action "Move to a new file" when I press ctrl+. on function.

image

Then it generate the js file(or ts file if I run that action in .ts file) with the filename which is the same with function name.

I've been doing a lot these days splitting large `impl MyStruct' into multiple files. It's not terribly difficult, but creating one file, moving code around, and erasing existing code is sometimes very annoying. Although it is not an important feature, I made this feature request to increase usability.

namse avatar Sep 15 '22 07:09 namse

There is a bit of precedent for something like this for modules. I'd rather see programmers be able to move arbitrary sections of code to a new file. This would also probably have to do some other things as well:

  • Update local references to the moved items to point at the new module.
  • pub use any previously pub items so as to not break any external references to those items.

There is also the tricky bit of naming that new module... are we able to get user input in the life cycle of an assist?

Really, this would look something like a combined form of Extract module -> Extract module to file. Except that workflow appears to have some weird behavior like:

  • Making everything in the new module pub(crate) even when it has no references.
  • Not pub use ing any previously pub items.

mdx97 avatar Sep 16 '22 01:09 mdx97

I'd say this is pretty much a duplicate of #2178.

flodiebold avatar Sep 16 '22 09:09 flodiebold

We already have an "extract module" assist that shows up when a function (or other item) is selected, and an "Extract module to file" assist. Since that combination accomplishes the same thing, and we generally prefer general, composable assists, this seems better than having a special case for functions.

Hence, closing.

jonas-schievink avatar Jan 30 '23 17:01 jonas-schievink