Skip to content

f0i/motoko-format

Repository files navigation

Motoko Formatter

A code formatter for the Motoko programming language.

⚠️ Deprecated

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.

VS Code 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.

> Format Document

On the first run, it might ask which formatter to use, select f0i.motoko-format.

f0i.motoko-format

It is also recommended to install the Motoko language support extension. This will provide additional features such as syntax highlighting.

Optional: Format on save

  • 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.

Installation as CLI

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.

Windows

Linux, Mac or WSL

  • Run the following command in a terminal:
curl -fsSL https://dprint.dev/install.sh | sh

Configure your project

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

Update

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.

Trouble shooting

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.

New GitHub Issue

or

New GitLab Issue

  • 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
  • Formatter is generating unexpected output
    • Please crate an issue containing the input code and the expected output

Advanced installation

There are several other options available to install dprint https://dprint.dev/install/ to get install instructions for your platform.

Development References

Syntax description in motoko repo

Motoko grammar.txt

Motoko style guide

pest.rs

Calculator parser example

dprint (rust alternative for prettier)