A code formatter for the Motoko programming language.
Dfinity made a more complete and better maintained version of this in typescript.
Please use https://github.com/dfinity/prettier-plugin-motoko or, for VS Code the Motoko extension.
The Visual Studio Code plugin is available through the marketplace as motoko-format
(unpublished, see https://gitlab.com/f0i/motoko-format-vscode for source!).
This will add the motoko formatter.
Use the > Format Document
command to run it.
On the first run, it might ask which formatter to use, select f0i.motoko-format
.
It is also recommended to install the Motoko language support extension. This will provide additional features such as syntax highlighting.
- Run the VS Code command (View » Command Palette)
> Preferences:
Open Settings (JSON)
. - Add the following section for motoko inside the
settings.json
:
{
/* other settings */
"[motoko]": {
"editor.defaultFormatter": "f0i.motoko-format",
"editor.formatOnSave": true,
},
/* other settings */
}
Now .mo
files should get automatically formatted whenever you save them.
The current version can be used as a plugin for dprint.
Follow the steps below to install dprint and set it to use motoko-format.
- Download the dprint installer: installer.exe.
- Run it and follow the instructions.
- Skip the next section and continue with Configure your project below.
- Run the following command in a terminal:
curl -fsSL https://dprint.dev/install.sh | sh
- Continue with Configure your project below.
Create a dprint.json
file in the root of your project directory and insert the following content:
{
"includes": ["**/*.{mo}"],
"excludes": [],
"plugins": [
"https://gitlab.com/f0i/motoko-format/-/jobs/artifacts/main/raw/release/dprint_plugin_motoko.wasm?job=release&file=plugin.wasm"
]
}
If everything is working so far, you should be able to format a file with this command in a terminal:
dprint fmt -- path/to/file.mo
Run dprint clear-cache
in a terminal.
Dprint keeps a local copy of the Motoko plugin.
Running dprint clear-cache
will delete the local copy.
On the next run, dprint will automatically download the new version of the plugin file.
Please don't hesitate to create an Issue if you run into any problem. I'm happy and thankful to know any problem with the formatter, plugin, documentation or anything else related to this formatter.
or
- When formatting in VS Code, nothing happens
- Check if dprint can be executed in a terminal
dprint fmt -- path/to/file.mo
- If that is working, check if dprint is enabled in VS code
- Please create an issue to document all problems
- Check if dprint can be executed in a terminal
- Formatter is generating unexpected output
- Please crate an issue containing the input code and the expected output
There are several other options available to install dprint https://dprint.dev/install/ to get install instructions for your platform.