SMLFormat is an autoformatter for the Standard ML programing language. It indents, keeps lines under 80 characters, and applies a set of heuristics to prettify code.
The source code of SMLFormat is formatted with SMLFormat.
- Install the SML/NJ compiler.
- Clone this git repo:
git clone https://github.com/jluningp/smlformat.git SMLFORMAT_DIR
. - Add SMLFormat to your path (replacing SMLFORMAT_DIR with your SMLFormat directory):
$ echo "PATH=\"\$PATH:SMLFORMAT_DIR\"; export PATH" >> ~/.bashrc
$ source ~/.bashrc
- Follow the instructions below for editor installation.
SMLFormat can read from stdin and write to stdout, or it can take files as arguments.
Usage: smlformat [-i inputfile] [-o outputfile]
-i infile read code from file instead of stdin
-o outfile write formatted code to file instead of stdout
-h print this help message
Read from file:
$ echo "fun foo x = 10" > test.sml
$ smlformat -i test.sml
fun foo x = 10
Write to file:
$ echo "fun foo x = 10" | smlformat -o test_output.sml
$ cat test_output.sml
fun foo x = 10
Read and write files:
$ echo "fun foo x = 10" > test.sml
$ smlformat -i test.sml -o test_output.sml
$ cat test_output.sml
fun foo x = 10
Read from stdin and write to stdout:
$ echo "fun foo x = 10" | smlformat
fun foo x = 10
Format a file "in-place":
$ echo "fun foo x = 10" > test.sml
$ smlformat -i test.sml -o test.sml
$ cat test.sml
fun foo x = 10
If an input fails to parse, SMLFormat cannot format it and will return the original input.
$ echo "val x" | smlformat
val x
You can also manually run SMLFormat in the SML/NJ REPL by running
sml -m SMLFORMAT_DIR/sources.cm
- SmlFormat.format "input.sml";
val () = ()
-
- If you want to format on save, uncomment the bottom line of
editors/emacs/smlformat.el
. - Add the following line to your
.emacs
, withSMLFORMAT_DIR
replaced by your SMLFormat directory:
(load "SMLFORMAT_DIR/editors/emacs/smlformat.el")
- Reopen emacs
- Save the file you're working on
M-x smlformat
- If your file doesn't parse when you run SMLFormat, it will not be formatted.
- Install the Neoformat vim plugin
- Copy the files in
editors/vim
into Neoformat's formatters directory1.
cp editors/vim/* ~/.vim/plugged/neoformat/autoload/neoformat/formatters/
- To enable formatting on save, add the following to your .vimrc:
autocmd BufWritePre *.sml,*.sig Neoformat
1 Why is there sml.vim
and lprolog.vim
? Vim thinks .sig
files have filetype lprolog
: Neoformat
- If your file doesn't parse when you run SMLFormat, it will not be formatted.
- Types have too many parentheses around them
- Functor signatures get deleted
- Indentation with constructors + newlines is weird (maybe an extra indent?)