-
-
Notifications
You must be signed in to change notification settings - Fork 808
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
"Extract Variable" doesn't properly work with use
and case
#4269
Comments
Oh dear! Thank you |
I would like to take a look at this. I've also noticed that it's reproducible in blocks. |
Thank you |
I'm going to assume that (if the code action is allowed in this position) this selection pub fn main(result: Result(Int, String)) {
case result {
Ok(value) -> value + 1
^^^^^^^^^
Error(_) -> panic
}
} Should extract variable in the following way pub fn main(result: Result(Int, String)) {
case result {
Ok(value) -> {
let int = value + 1
int
}
Error(_) -> panic
}
} Currently, "extract variable" is not allowed top level statements. Perhaps the first statemet in a block (or the right hand side of the case clause) should also be considered a top level statement, and thus, not get extracted. Does it still makes sense to offer the code action that creates the block (i. e. to start a refactor)? If we allow that, maybe creating an "add braces to arm expression" like the one in Rust is also a possibility. |
That would be a good code action to have, please open an issue for it |
Then, should we still offer the code action for these example? pub fn main(result) {
case result {
Ok(value) -> value + 1
^^^^^^^^^
Error(_) -> panic
}
} I ask, because the code action is not offered (on purpose) when selecting the whole right-hand side of the assignment in such cases for assignments, like in pub fn main() {
let v = value + 1
^^^^^^^^^
let w = 1
^
v + w
} I would suggest that this selection should not be allowed inside case clauses either when selecting the whole expression case result {
Ok(value) -> value + 1
^^^^^^^^^
Error(_) -> panic
} But the following should (and wrap it in the block and all that) case result {
Ok(value) -> 2 * value + 1
^^^^^^^^^
Error(_) -> panic
} |
I think the most useful thing would be to always offer it, and to wrap the expression in a block if-need-be. |
The "extract variable" code action is offered in a couple of places where it shouldn't be. Inside
use
callbacks:Which transforms into:
And also inside a
case
body:Which becomes:
In both of these cases, a variable is referenced outside of the scope which it is created in, producing invalid Gleam code.
The text was updated successfully, but these errors were encountered: