nwolverson / purescript-language-server

MIT License
183 stars 41 forks source link

Add command for generating an optic for a type #171

Open JordanMartinez opened 2 years ago

JordanMartinez commented 2 years ago

I've created purescript-tidy-codgen-lens, which can generate a Lens.purs file for each type declaration in a file, as well as the record label lenses. However, this program works on entire files.

Could part of this program be extracted into a plugin, so that if I highlight a given type, and run the 'Generate Optic' command, it would generate an optic just for that type?

nwolverson commented 2 years ago

Seems like a candidate for a code lens (no pun intended). Source selection/running commands is something that requires client-side support so for refactoring/codegen type actions something that can be provided via LSP directly (code lens or info-type code action) should be preferred.

JordanMartinez commented 2 years ago

Ok, so what are the first steps towards implementing this?

JordanMartinez commented 2 years ago

I've looked through this codebase a bit. Seems like I'd need to add a file at src/LanguageServer/IdePurescript/CodeLens/OpticsGeneration.purs

A few questions I'll need to address when implementing this:

nwolverson commented 2 years ago

In general - at a glance it looks like purescript-tidy-codgen-lens is using the CST parser, which should integrate well here, the parsed input should already be available in the state for a lens to find the appropriate candidate & work from.

I don't really have an opinion on options/1st 2 points, other than concern about potential explosion of LSP config just for 1 thing. If being slightly opinionated cuts down on config that always seems like a good tradeoff to me.

On formatting, my initial take is however it is printed, the user can run their formatter over this as they wish. If purescript-tidy-codegen is the easiest way to construct the output, that seems like a good place to start.

Imports, we do a whole bunch of import management already, it would be nice if this could just work - but it doesn't seem worth worth worrying about up front

JordanMartinez commented 2 years ago

I think the initial version of this should only generate a lens or prism for the type in question and not care about record labels. That will likely solve 80% of the problem most people have. The rest is other niceties that can be added after the fact.