tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
944 stars 83 forks source link

(Manage? Match?) .cabal info of file to format, from directory path of project, while using --stdin-input-file, when the name of file is not available. #1055

Closed 0rphee closed 1 year ago

0rphee commented 1 year ago

(I posted this issue originally in the fourmolu repo: https://github.com/fourmolu/fourmolu/issues/353, but I was redirected here)

In order to auto-format my files with my editor, fourmolu needs to support stdin input, which it does. Nevertheless, to correctly format from stdin, fourmolu needs to be run with the path to the file that is being fed through stdin: fourmolu --stdin-input-file /Main.hs. However, unfortunately my editor doesn't support any means to extract the name of the file to be formatted, and use it with the formatter at the moment (or for that matter, any other command). I tried passing the value of $(pwd) and though fourmolu can find the .cabal file, because in that file it doesn't find the directory as a source file, it fails to account for GHC extensions when formatting from stdin.

Now, because fourmolu knows which extensions are on/off through the cabal info, I thought that perhaps, in cases where the name of the file cannot be given to fourmolu directly, it would be possible to parse the module name, and through it, find a matching source file name in the cabal info. (I understand that it may not be a reasonable option).

A less accurate way to "solve" this issue, could be, enable all extensions mentioned anywhere in the .cabal file...

Here are some (maybe) relevant lines regarding the found .cabal file, but without a valid file name in it. fourmolu: https://github.com/fourmolu/fourmolu/blob/47a40bb54f929bdd0fa1dd8d4ff4403bc4c26d72/app/Main.hs#L165C7-L174

ormolu: https://github.com/tweag/ormolu/blob/cb93ebded6203d7696c080e608d2e6fe48bdac12/app/Main.hs#L92C13-L101C38

Anyways, thank you in advance for any help!

amesgen commented 1 year ago

This seems to be the upstream issue to add the relevant feature to helix: https://github.com/helix-editor/helix/issues/3596

The issue lists other formatters (like prettier) that would also benefit from this feature, so it seems simpler to implement this in helix than to patch all formatters that have a feature like this. We could reconsider this if helix does not want to implement this feature, but right now, it doesn't seem like it, e.g. there already as a PR implementing that feature with recent activity: https://github.com/helix-editor/helix/pull/5626

As another alternative, you could use Ormolu via HLS which should provide Ormolu (≥0.5.3.0) with the relevant context, cf. https://github.com/haskell/haskell-language-server/pull/3449.

Does this make sense?

0rphee commented 1 year ago

That's fair, thanks! I didn't see those issues. I will try formatting using hls in the meantime :)