-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add an IDE refactor to convert switch syntaxes #52798
Comments
We already have a refactoring for some structures of switches. For example, given a file containing int f(int i) {
switch (i) {
case 0:
return 0;
case 1:
return 1;
default:
return 2;
}
} Positioning the cursor in the The current refactorings are, however, somewhat limited in the sense that they will only be offered when the code matches a pattern we know how to convert (without changing the semantics or introducing errors). If you remove the Are there other code patterns that the refactorings should support? |
I've tried multiple time using assists to see if one was available, but never saw one. So either my dart version is somehow out of date, or the current one is probably too limited. One issue is that it's unclear to me what exactly causes the assist to not show up. In that sense, the discoverability of the feature is poor, and it may be missing important use cases. I'll see if I can make a compilation of the cases I'd need. |
Do you see the assist using the code I posted above?
Thanks, that would be very helpful. |
I do see it. Although surprisingly, it appears after the "convert to if chain" refactor, which is likely way less useful overall. Although I would say, in the case of a missing default you described earlier: IMO the refactor should still be offered. Users of pattern-matching very much desire compilation errors. I would treat an "unhandled case" error differently than a "missing variable identifier" error. Alternatively, the refactor could generate a throwing default case: // TODO handle default
_ => throw UnimplementedError(), Or if the switch could be exhaustive, it could rely on the "add missing cases" fix to generate all the missing cases. I would assume that that's what people most likely want. Worst-case scenario, they can |
Is this refactor dependent in any way on the installed packages? I tried this in a different project from the one where I tried earlier, and the refactoring doesn't show up. |
Not to the best of my knowledge. It is dependent on the language version (we won't suggest converting a switch statement into a switch expression in a library that's pre-3.0), but I don't see anything in the code that would suggest any other reason for it not showing up (other than the already stated possibility that the code doesn't match the heuristics used to decide how to do the conversion. |
There's something fishy going on then. Because in Freezed on the branch "ast", I've been using pattern-matching. Yet when copy-pasting the code in my project, the assist fails to appear. I'm not really sure what causes this. |
Hey @rrousselGit. I did found this only today and it is relatively old. Have you been able to reproduce any cases that the assist doesn't cover? I also found #52958 that is opened but as I stated there:
|
I think that's fixed now |
Currently, a
switch
can be written in two different ways:An expression:
A statement:
It is quite common to want to switch between one or the other. Such as for example, initially writing the
=>
syntax. But refactoring to thecase
syntax because some cases need more than one expressionBut although the syntaxes look similar, there are tons of small differences. This makes the refactoring very painful.
Could a quick-fix be added to automate this?
The text was updated successfully, but these errors were encountered: