haskell-language-server icon indicating copy to clipboard operation
haskell-language-server copied to clipboard

Implement code action to add module to cabal file

Open VeryMilkyJoe opened this issue 2 years ago • 2 comments

Related to #3695. Closes #3595.

When a cradle error occurs that the current module is unknown, we now offer code actions for each possible field to add the module to the most likely cabal file.

This PR is split into three commits which group the changes into:

  • changes to ghcide;
  • a cabal file parser package which will be moved out of hls in the future;
  • and changes to the cabal-plugin.

When a cradle error is found to be related to an unknown module, we edit the error by adding information (the module's path, the most likely cabal file to add the module to) such that the cabal plugin can offer code actions for adding the module to a cabal file. For each other-module or exposed-module field in a stanza, we check whether any source directory of the stanza contains the module. If this is the case, we construct a code action which adds the module to the cabal file, while adhering to the cabal file's previous formatting as best as possible.

VeryMilkyJoe avatar Aug 28 '23 09:08 VeryMilkyJoe

I'm not sure that reimplementing Cabal parser is a sustainable approach.

https://github.com/Bodigrim/cabal-add/ inserts build-depends largely re-using Cabal-syntax, and probably inserting modules could be done in a similar fashion.

Bodigrim avatar Mar 11 '24 21:03 Bodigrim

@Bodigrim Yeah, the cabal-add approach feels more viable than the custom parser.

Independently, I believe we should have a third-party parser for cabal files.

fendor avatar Mar 11 '24 21:03 fendor