#web-framework #view #formatter #macro #cli #format #format-file

app leptosfmt

view macro formatter CLI for the Leptos web framework

17 releases

0.1.18 Dec 22, 2023
0.1.17 Oct 10, 2023
0.1.14 Sep 8, 2023
0.1.13 Aug 27, 2023
0.1.4 Mar 27, 2023

#1401 in Web programming

Download history 418/week @ 2024-01-28 494/week @ 2024-02-04 344/week @ 2024-02-11 405/week @ 2024-02-18 380/week @ 2024-02-25 571/week @ 2024-03-03 348/week @ 2024-03-10 403/week @ 2024-03-17 475/week @ 2024-03-24 436/week @ 2024-03-31 375/week @ 2024-04-07 359/week @ 2024-04-14 371/week @ 2024-04-21 200/week @ 2024-04-28 439/week @ 2024-05-05 456/week @ 2024-05-12

1,522 downloads per month




crates.io build security discord

A formatter for the leptos view! macro

All notable changes are documented in: CHANGELOG.md


cargo install leptosfmt

or for trying out unreleased features:

cargo install --git https://github.com/bram209/leptosfmt.git


Usage: leptosfmt [OPTIONS] [INPUT_PATTERNS]...

  [INPUT_PATTERNS]...  A space separated list of file, directory or glob

  -m, --max-width <MAX_WIDTH>      Maximum width of each line
  -t, --tab-spaces <TAB_SPACES>    Number of spaces per tab
  -c, --config-file <CONFIG_FILE>  Configuration file
  -s, --stdin                      Format stdin and write to stdout
  -r, --rustfmt                    Format with rustfmt after formatting with leptosfmt (requires stdin)
  -q, --quiet                      
      --check                      Check if the file is correctly formatted. Exit with code 1 if not
  -h, --help                       Print help
  -V, --version                    Print version

Using with Rust Analyzer

You can set the rust-analyzer.rustfmt.overrideCommand setting.

  "rust-analyzer.rustfmt.overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"]

And you must configure rustfmt to use the correct edition, place a rustfmt.toml file in the root of your project:

edition = "2021"
# (optional) other config...

Note: For VSCode users, I recommend to use workpsace settings (CMD + shift + p -> Open workspace settings), so that you can only configure leptosfmt for workpsaces that are using leptos. For Neovim users, I recommend using neoconf.nvim for managing project-local LSP configuration.


You can configure all settings through a leptosfmt.toml file.

max_width = 100 # Maximum width of each line
tab_spaces = 4 # Number of spaces per tab
indentation_style = "Auto" # "Tabs", "Spaces" or "Auto"
newline_style = "Auto" # "Unix", "Windows" or "Auto"
attr_value_brace_style = "WhenRequired" # "Always", "AlwaysUnlessLit", "WhenRequired" or "Preserve"

To see what each setting does, the see configuration docs


Single file

Format a specific file by name

leptosfmt ./examples/counter/src/lib.rs

Current directory

Format all .rs files within the current directory

leptosfmt .


Format all .rs files within the examples directory

leptosfmt ./examples


Format all .rs files ending with _test.rs within the examples directory

leptosfmt ./examples/**/*_test.rs

A note on non-doc comments

Currently this formatter does not support non-doc comments in code blocks. It uses a fork of prettyplease for formatting rust code, and prettyplease does not support this. I would like to not diverge this fork too much (so I can easily keep in sync with upstream), therefore I didn't add non-doc comment support in my prettyplease fork for now. This means that you can use non-doc comments throughout your view macro, as long as they don't reside within code blocks.

Pretty-printer algorithm

The pretty-printer is based on Philip Karlton’s Mesa pretty-printer, as described in the appendix to Derek C. Oppen, “Pretty Printing” (1979), Stanford Computer Science Department STAN-CS-79-770, http://i.stanford.edu/pub/cstr/reports/cs/tr/79/770/CS-TR-79-770.pdf. This algorithm's implementation is taken from prettyplease which is adapted from rustc_ast_pretty.

The algorithm takes from an input stream of length n and an output device with margin width m, the algorithm requires time O(n) and space O(m). The algorithm is described in terms of two parallel processes; the first scans the input stream to determine the space required to print logical blocks of tokens; the second uses this information to decide where to break lines of text; the two processes communicate by means of a buffer of size o(m). The algorithm does not wait for the entire stream to be input, but begins printing as soon as it has received a linefull of input.


~143K SLoC