Stop adding code in format/lint, use code actions instead
In this video I demonstrate how our formatter acts differently compared what formatting typically does:
The user does manual work, removes the front sides of all relations. The user has set the formatter to be run on save. When we run it, we do a thing that a formatter should not do: we add the relations back because we forgot to remove the back-relations before saving/formatting.
What a formatter typically should do:
- Align code
- Ordering
- Spacing
Whatever we want to do additionally, like missing relation fields, should be happen through a code action. This would then require the user to go on top of a relation field, and if we notice it doesn't have the other side available, we'd be showing a light bulb in the editor through the language server protocol, and offering code actions for the user.
Example:
The user has only defined one forward relation field, and presses the code action quick key on top of the relation attribute, listing available code actions:
- first being add a back-relation 1:n, so the back-relation is an array.
- second is adding a back-relation 1:1
We can also do this from the other side, with different options:
- Add an array back-relation to the other side, make a m:n relation
- Add a singular forward field
If we remove the "fix" part from "format" and only add it to the language-server then users won't be able to use it like they did with npx prisma format like we sometimes recommend to do.
We would probably need a "fix" CLI command?
Yep. Formatting is not supposed to be adding code, just formatting it.
And instead of fixing, we should guide our users to utilize their editors instead of magic commands. And provide them the best tools to autofill missing things in the schema.
We can also treat the format in the extension separate from the existing behavior that prisma format in the CLI has.
I'd be in favor of splitting the current thing into a format command and a fix command. In the editor, the fix part can be code actions.
