language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

Stop adding code in format/lint, use code actions instead

Open pimeys opened this issue 4 years ago • 5 comments

In this video I demonstrate how our formatter acts differently compared what formatting typically does:

Watch the video

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

pimeys avatar Dec 01 '21 15:12 pimeys

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?

Jolg42 avatar Dec 01 '21 15:12 Jolg42

Yep. Formatting is not supposed to be adding code, just formatting it.

pimeys avatar Dec 01 '21 15:12 pimeys

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.

pimeys avatar Dec 01 '21 15:12 pimeys

We can also treat the format in the extension separate from the existing behavior that prisma format in the CLI has.

janpio avatar Dec 01 '21 15:12 janpio

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.

tomhoule avatar Jan 07 '22 11:01 tomhoule