I want to make my code look consistent and neat. I don’t have any personal stylistic preferences at the moment – I want to pick a widely accepted style and stick with it. I’m used to tools like Rust’s rustfmt, which have a well-defined style with good defaults.
What formatters does Haskell have that are similar to them? ormolu and fourmolu look like good options with sensible defaults.
They are. ormolu has sensible default and AFAIK It doesn’t allow for configuration (hence, a single source of “true format”). fourmolu is just a ormolu fork which allow you to customize some attributes like how-many-space-per-tab, let binding should be one-line or splitted, etc…
I prefer fourmolu because I am the kind of guy who wants to waste time configuring meaningless things but probably ormolu fits you better from your description
We use ormolu at work, and it’s okay enough. I dislike many things about its style, but I like not having to worry about style. If I had my time again, I’d probably look more closely at fourmolu.
We don’t currently have a concise explanation anywhere, but the main ones are: four-space indentation (the original raison d’etre), leading commas, and simpler import/export lists.
I’m also a big fan of stylish-haskell. stylish-haskell does a great job formatting the tedious, entirely systematic parts of the code (mostly import statements), without trying anything tricky with expressions and definitions.
I’ve actually been pretty happy with opinionated formatters in other languages (Rust, Python, etc), but I found that Haskell’s base expression grammar is so minimalist and flexible that automatic formatting routinely gets in the way of writing readable code. Using my own judgement for new lines and alignment in expressions and relying on stylish-haskell for imports and records has been the perfect combination for me. It also helps that Emacs’s default indentation suggestions are pretty solid
Hello, I’m new to Haskell. Is there an actively maintained formatter that does vertical alignment of =, ::, -> etc? I’m aware of the arguments against vertical alignment, but as a single developer, I prefer this style.
Ormolu and Fourmolu don’t seem to have vertical alignment. I believe stylish-haskell does, but when I tried to install it with cabal install stylish-haskell, the installation failed. My setup is Windows 11 + VSCode + Haskell extension + ghc-9.4.7. Thank you!
Ah yes. I did have an old version of cabal. I’ve updated it with choco install cabal. And I was able to successfully run cabal install stylish-haskell. I’m able to format files with stylish-haskell -i myfile.hs.
For some reason, it doesn’t work automatically on save even though I have the stylish-haskell VSCode extension installed, my Haskell extension is set to the stylish-haskell formatter, and C:\cabal\bin is in my PATH. But hopefully, I’ll figure that out soon. Thanks for your help!
I want to limit my columns to e.g. 100 chars. It’s cool to see fourmoulu has this! (I’ve been using Ormolu and limiting columns for Haskell code manually so far. This will save me a lot of time!)
But what about comments? Currently I use the “rewrap” vscode extension to limit column length for comments, but it doesn’t work in certain cases within Haskell code.
Are there any Haskell code formatters out there that also deal with comments? (With support for bulleted/numbered lists etc. like “rewrap”)
Fourmolu doesn’t currently have any support for wrapping comments, but I’m sure we’d merge a PR implementing it as an option. Please consider at least opening a GitHub issue to track this.
I’m pretty sure none of the other formatters have this feature either.