How to setup a formatter - kdaisho/Blog GitHub Wiki
If you've had trouble finding a formatter that works well with Haskell, you're not alone. After some searching, I settled on fourmolu.
Requirements:
- cabal
1. Installation
cabal install fourmolu
This command may take some time to complete. Be patient while the installation process runs.
Once installed, you can format your Haskell files by running:
fourmolu -i .
This command formats all .hs files within the current directory and its subdirectories.
Running this command from the terminal can be tedious, so let's set it up in VSCode.
2. Configuring VSCode
Unfortunately, there isn't an extension specifically for fourmolu, but we can set it up manually.
Configure a Task
Follow these steps:
Press Cmd + Shift + P
and select "Tasks: Configure Task".
Choose to create a task from a template, which will create a tasks.json file under .vscode/
.
Replace the contents of tasks.json
with the following:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "fourmolu",
"type": "shell",
"command": "fourmolu",
"args": ["-i", "."],
"problemMatcher": []
}
]
}
The above configuration represents the bash command fourmolu -i .
.
Set up a Hotkey
- Press
Cmd + K, Cmd + S
to open the Keyboard Shortcuts editor. - Search for "Tasks: Run Task", click "+" to add a new hotkey.
- Add your desired hotkey combination. For example, I have set
Cmd + h
.
Usage (formatting .hs files)
- Press
Cmd + h
(in my case) to open the dropdown. - Select "fourmolu" from the dropdown or press Enter. This will format all .hs files in the current directory and its subdirectories.