Skip to content

internal: use ControlFlow in "extract function" assist #10309

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

Merged
merged 5 commits into from
Oct 14, 2021

Conversation

dzvon
Copy link
Contributor

@dzvon dzvon commented Sep 22, 2021

Fixes #10272

@Veykril
Copy link
Member

Veykril commented Sep 22, 2021

There is one slight problem here, ControlFlow isn't in the prelude so this needs to add an import to the module in this case. See the replace_qualified_name_with_use assist for an example on how to do this, it uses ImportScope::find_insert_use_container_with_macros and insert_use to do the job.

For this to then also work in unit tests you will need to add a //- minicore: try directive to the top of the relevant tests fixtures. Look at some other tests in the extract function module which should already be using minicore for some things to see what I mean.

@lnicola
Copy link
Member

lnicola commented Oct 7, 2021

@dzvon do you need help with @Veykril's remark?

@dzvon
Copy link
Contributor Author

dzvon commented Oct 8, 2021

I need help.

In replace_qualified_name_with_use, the variable scope is built as this.

    let scope = ImportScope::find_insert_use_container_with_macros(path.syntax(), &ctx.sema)?;

But how should I do in this case? What arguments should I pass into find_insert_use_container_with_macros?

I'm sorry, I have many concepts that are not clear to me :(. Thank you for your time and suggestion.

@Veykril
Copy link
Member

Veykril commented Oct 8, 2021

No problem, you can always ask when something's not clear.

The arguments passed to to find_insert_use_container_with_macros are the node from where we should look for a module in its ancestors and the semantics.
So in this case you can just pass the node variable defined on line 70 in the extract_function file, that should work fine for this use case as that's a node in the module we are doing our edits in.

@Veykril
Copy link
Member

Veykril commented Oct 8, 2021

Ah right use insertion logic requires mutable node edits. So this is a bit more tricky to get the edit out of as extract_function works with text based edits currently, something we probably want to change first.

I can take a look at doing that first, then you can rebase your changes on top of that if you'd like.

@dzvon dzvon force-pushed the use-controlflow-in-extract-func branch from 77bbc8d to 5818358 Compare October 13, 2021 01:10
@dzvon
Copy link
Contributor Author

dzvon commented Oct 13, 2021

@Veykril Thanks so much for your explanation. I've finished the import job. Could you give it a review again if you have time? :)

@dzvon dzvon requested a review from Veykril October 13, 2021 01:51
@Veykril
Copy link
Member

Veykril commented Oct 14, 2021

Lgtm thanks!
bors r+

@bors
Copy link
Contributor

bors bot commented Oct 14, 2021

@bors bors bot merged commit 641fa37 into rust-lang:master Oct 14, 2021
@Veykril Veykril changed the title use ControlFlow in "extract function" assist internal: use ControlFlow in "extract function" assist Oct 15, 2021
bors added a commit that referenced this pull request Dec 7, 2023
fix: bug in extract_function.rs

There is a little bug in extract_function: It appends `use path::to::ControlFlow;` if the function created contains string "ControlFlow".

 A case below (also in the test named `does_not_import_control_flow` which will fail in the original code)

<img width="322" alt="image" src="https://github.com/rust-lang/rust-analyzer/assets/53432474/4b80bb58-0cfd-4d56-b64c-d9649eed336e">
<img width="391" alt="image" src="https://github.com/rust-lang/rust-analyzer/assets/53432474/3d7262f4-8a4c-44ea-822d-304b8b23fe28">

Now I have changed the condition determining whether adding import statement. Only when the new function body contains ControlFlow::Break or ControlFlow::Continue can the import statement be added.

Last related PR: #10309
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use std::ops::ControlFlow in ‘extract function’ assist
3 participants