-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
x/tools/gopls: code action to "Add switch cases for [enum|type]" #65411
Comments
Nice idea! It should also support type switches: such a feature would be helpful to me to as I frequently write functions that switch over ast.Node or types.Type or types.Object. I suggest the precise criteria should be:
Do you want to take a crack at it yourself? |
Agree with all your points! I do I think offering it when there's a default case could potentially be useful as well, but I guess it's probably better to do a first implementation without that and add it later if people think it's useful. I'm happy to take a stab at this but I'm gonna need some hand-holding. I played around a bit trying to do this but I'm 100% sure it wasn't the right way to do it and also it fell short when the type wasn't declared on the same file that the switch was in. Edit: So I was having another look now and managed to make it work for types in other packages, but I'm still sure this is not anywhere near the correct way of doing this. I'll take any pointers you can give me and will take some more time to try and make type switches work. |
Most of the computation will be done in the domain of go/types: you'll need to identify the type of the switch operand (a Type switches will be similar except that you are iterating over types, not constants, and testing compatibility with the switch operand using types.AssignableTo. You should test whether each Send a CL when you're ready, even if just to ask for clarification. |
Cool! That was very helpful! I think I got something more or less working, will share a CL soon! |
gopls version
v0.14.2
go env
What did you do?
Consider the following snippet:
(I realise switching on
at
in that example doesn't make sense, just doing that to illustrate the use-case)What did you see happen?
N/A
What did you expect to see?
It would be nice if gopls offered completing the missing switch arms as a code action.
I'm thinking this could be offered only when there's a finite set of cases, so it would work for defined types such as the example above, but not on basic types such as string, int, etc.
Is there an appetite to implement something like this? Or is it intentionally not supported?
Editor and settings
No response
Logs
No response
The text was updated successfully, but these errors were encountered: