Skip to content
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

Feat request: extract code (not only function) to file (resolving needed imports) #331

Open
danielo515 opened this issue Jul 20, 2022 · 3 comments
Labels

Comments

@danielo515
Copy link

Hello.
First, thanks for this plugin, seems to be what I need, albeit I am not yet sure.
For some weird reason, there is no tool when you want to extract a bunch of code to a new file. I'm not talking about a single function, but a group of functions, variables, etc.

I am trying to use Block refactor, but it is always failing everything I tried with typescript. I first tried to refactor a constant, asks me for the file name, then something I don't know what it is, and then fails.
I just tried within a function body, and that seems to be understood as a block, and, to my surprise, rather than extracting the entire function, only the function body is extracted. Am I misunderstood the usage of the plugin? I want to be able to

  • Extract a bunch of functions to a new file, and in the process update all imports
  • Extract a bunch of variables and things to a new file, and in the process update all imports

Not sure if it is worth explaining the errors I'm getting if I didn't understood the purpose of the plugin properly.
Regaards

@pranavrao145
Copy link
Collaborator

Hi @danielo515

Extract block extracts everything in the current block scope to another function (the block scope is typically the entire function body, though sometimes it changes). Right now, the functionality you're looking for (extracting entire functions themselves to different files, while updating imports) is not supported. The closest thing that exists right now would be "Extract Block/Function to File", which extracts block of code to a function, and puts that function in another file.

Hopefully that clears up why it didn't work in this case. While we don't support that feature now, we can keep this issue open so that we can track it and perhaps implement it in the future. If you'd like to try making a contribution as well, feel free to do so, and we can review it for you!

@danielo515
Copy link
Author

Hello @pranavrao145 , thank you for your answer

Right now, the functionality you're looking for (extracting entire functions themselves to different files, while updating imports) is not supported.

That is not exactly what I wanted. What I want is a bit more advanced than that. I wanted to be able to select a visual region, which may include constants, variables, function declarations etc, and extract those into a separate file, adding only the requried exports. So for example, if there is a constant used only by the function that I am moving to a new file, then only the function will need to be re-imported. But even without that functionality, and if it ended importing all the things that have been moved that will still be valuable to me.
So I think I can summarise as refactoring to a separate file all the nodes that are under my current visual selection. Does that make sense?

@pranavrao145
Copy link
Collaborator

Hi @danielo515, sorry for the late reply - but yes, that makes sense. Sadly, we don't have support for that right now, but I will label this as a feature request so we can work on it later on!

@TheLeoP TheLeoP changed the title Extract block fails for typescript Feat request: extract code (not only function) to file (resolving needed imports) Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants