the-sett / elm-syntax-dsl / Elm.Pretty

Elm.Pretty is a pretty printer for Elm syntax trees. It makes use of the-sett/elm-pretty-printer to best fit the code to a given page width in characters.

It aims to output code that is fully stable with respect to elm-format in the sense that running elm-format on the output should have no effect at all. The advantage of this is that if generated code moves to being edited by hand, there will not be a large white-space only diff created when elm-format is applied.

To print the Doc created by the pretty functions, the-sett/elm-pretty-printer is used:

import Elm.Pretty
import Pretty


-- Fit to a page width of 120 characters
elmAsString =
    Elm.Pretty.prepareLayout 120 someFile
        |> Pretty.pretty 120

Use the Pretty.Renderer module to consume the Tags when printing to create fancy outputs such as HTML or console colors for syntax highlighting:

-- Fit to a column width of 120 characters
elmAsHtmlWithHighlighting =
    Elm.Pretty.prepareLayout 120 someFile
        |> Pretty.Renderer.pretty htmlRenderer 120

There is also a helper pretty function in this module that can go straight to a String, for convenience:

-- Fit to a page width of 120 characters
elmAsString =
    Elm.Pretty.pretty 120 someFile

Syntax highlighting tags.


type Tag
    = KeywordTag
    | CommentTag
    | OperatorTag
    | TypeTag
    | StatementTag
    | SignatureTag
    | LiteralTag
    | NumberTag

Elm syntax tags. The pretty print Doc Tag is parameterized over this set of tags, which describe which part of the Elm syntax the various parts of the Doc are for.

Pretty prints an entire Elm file.

prepareLayout : Basics.Int -> Elm.CodeGen.File -> Pretty.Doc Tag

Prepares a file of Elm code for layout by the pretty printer.

Note that the Doc type returned by this is a Pretty.Doc. This can be printed to a string by the the-sett/elm-pretty-printer package.

These Doc based functions are exposed in case you want to pretty print some Elm inside something else with the pretty printer, or do more fancy outputs with syntax highlighting.

The pretty function can be used to go directly from a File to a String, if that is more convenient.

pretty : Basics.Int -> Elm.CodeGen.File -> String

Prints a file of Elm code to the given page width, making use of the pretty printer.

Pretty printing snippets of Elm.

prettyModule : Elm.Syntax.Module.Module -> Pretty.Doc Tag

Pretty prints a module definition.

prettyImports : List Elm.Syntax.Import.Import -> Pretty.Doc Tag

Pretty prints a list of import statements.

The list will be de-duplicated and sorted.

prettyExposing : Elm.Syntax.Exposing.Exposing -> Pretty.Doc Tag

Pretty prints the contents of an exposing statement, as found on a module or import statement.

The exposed values will be de-duplicated and sorted.

prettyDeclaration : Basics.Int -> Elm.CodeGen.Declaration -> Pretty.Doc Tag

Pretty prints a single top-level declaration.

prettyFun : Elm.Syntax.Expression.Function -> Pretty.Doc Tag

Pretty prints an Elm function, which may include documentation and a signature too.

prettyTypeAlias : Elm.Syntax.TypeAlias.TypeAlias -> Pretty.Doc Tag

Pretty prints a type alias definition, which may include documentation too.

prettyCustomType : Elm.Syntax.Type.Type -> Pretty.Doc Tag

Pretty prints a custom type declaration, which may include documentation too.

prettyPortDeclaration : Elm.Syntax.Signature.Signature -> Pretty.Doc Tag

Pretty prints a port declaration.

prettyDestructuring : Elm.Syntax.Pattern.Pattern -> Elm.Syntax.Expression.Expression -> Pretty.Doc Tag

Pretty prints a destructuring declaration.

prettySignature : Elm.Syntax.Signature.Signature -> Pretty.Doc Tag

Pretty prints a type signature.

prettyPattern : Elm.Syntax.Pattern.Pattern -> Pretty.Doc Tag

Pretty prints a pattern.

prettyExpression : Elm.Syntax.Expression.Expression -> Pretty.Doc Tag

Pretty prints an expression.

prettyTypeAnnotation : Elm.Syntax.TypeAnnotation.TypeAnnotation -> Pretty.Doc Tag

Pretty prints a type annotation.