/fantomas

FSharp source code formatter

Primary LanguageF#OtherNOASSERTION

Fantomas

Fantomas logo

F# source code formatter, inspired by scalariform for Scala, ocp-indent for OCaml and PythonTidy for Python.

Build Status Github Actions Build Status AppVeyor Join the chat at https://gitter.im/fsprojects/fantomas

How to use

Command line tool / API

Use this command to install Fantomas as a .NET 5 SDK global tool:

dotnet tool install -g fantomas-tool

For detailed guidelines, please read Fantomas: How to use.

FAKE build system

Fantomas can be easily integrated with FAKE build system.
Check out the sample.

JetBrains Rider

The resharper-fsharp uses fantomas under the hood to format the source code. No need for any additional plugins.

Using the latest version inside Rider

For technical reasons Rider cannot always use the latest version of Fantomas found on NuGet. As a workaround you could install fantomas-tool locally with dotnet tool install fantomas-tool and configure it as an External tool.

Rider external tool window

Rider action window

This will have an impact on your editing experiencing in Rider, the external change to the file by the command line application might trigger more internal logic inside Rider than necessary. It could be noticeable in regards to the default formatting experience.

Visual Studio Code

The recommended way to use Fantomas is by using the Ionide plugin. Fantomas is integrated in FSAutoComplete which is the language server used by Ionide.

Alternatively, you can install the fantomas-fmt extension.

Visual Studio

The F# Formatting extension sets up Fantomas as the default formatter for F# files, configurable from Visual Studio's options.

Visual Studio for Mac

Install fantomas-tool locally with dotnet tool install fantomas-tool and configure it as an [External tool]

VS Mac external tool window

Online

Try the Fantomas online.

Early builds

Every once in a while an alpha or beta version is published to https://www.nuget.org/. Our previous MyGet feed is now deprecated.

Benchmarks

Some figures can be found at https://fsprojects.github.io/fantomas/
We use BenchmarkDotNet to collect data for each build on the master branch.

Purpose

This project aims at formatting F# source files based on a given configuration. Fantomas will ensure correct indentation and consistent spacing between elements in the source files. We assume that the source files are parsable by F# compiler before feeding into the tool. Fantomas follows two F# style guides: the F# code formatting guidelines from Microsoft by default and the G-Research F# code formatting guidelines via various settings.

Use cases

The project is developed with the following use cases in mind:

  • Reformatting an unfamiliar code base. It gives readability when you are not the one originally writing the code. To illustrate, the following example

    type Type
        = TyLam of Type * Type
        | TyVar of string
        | TyCon of string * Type list
        with override this.ToString () =
                match this with
                | TyLam (t1, t2) -> sprintf "(%s -> %s)" (t1.ToString()) (t2.ToString())
                | TyVar a -> a
                | TyCon (s, ts) -> s

    will be rewritten to

    type Type =
        | TyLam of Type * Type
        | TyVar of string
        | TyCon of string * Type list
        override this.ToString() =
            match this with
            | TyLam(t1, t2) -> sprintf "(%s -> %s)" (t1.ToString()) (t2.ToString())
            | TyVar a -> a
            | TyCon(s, ts) -> s
  • Converting from verbose syntax to light syntax. Feeding a source file in verbose mode, Fantomas will format it appropriately in light mode. This might be helpful for code generation since generating verbose source files is much easier. For example, this code fragment

    let Multiple9x9 () =
        for i in 1 .. 9 do
            printf "\n";
            for j in 1 .. 9 do
                let k = i * j in
                printf "%d x %d = %2d " i j k;
            done;
        done;;
    Multiple9x9 ();;

    is reformulated to

    let Multiple9x9() =
        for i in 1..9 do
            printf "\n"
            for j in 1..9 do
                let k = i * j
                printf "%d x %d = %2d " i j k
    
    Multiple9x9()
  • Formatting F# signatures, especially those generated by F# compiler and F# Interactive.

For more complex examples, please take a look at F# outputs of 20 language shootout programs and 10 CodeReview.SE source files.

Why the name "Fantomas"?

There are a few reasons to choose the name as such. First, it starts with an "F" just like many other F# projects. Second, Fantomas is my favourite character in the literature. Finally, Fantomas has the same Greek root as "phantom"; coincidentally F# ASTs and formatting rules are so mysterious to be handled correctly.

Contributing Guidelines

See the Contribution Guidelines.

Credits

We would like to gratefully thank the following persons for their contributions.

License

The library and tool are available under Apache 2.0 license. For more information see the License file.