Rescript formatting

I’m a bit lost with source formatting.

As I understand, formatting (old refmt) is embedded into the compiler (bsc).

I can only see a -format filename option right now: is it possible to use stdin to feed bsc with text to be formatted ? I suppose the answer is no, because I can’t find the -i option to specify if the code is an interface or not. Is it on the roadmap ?
also I don’t find the option to set the number of columns.

1 Like

The formatter is part of the bsc compiler. It currently recognises ReScript .res and .resi files automatically based on the file extension. You don’t need to supply an -i arg, it’s parsed from the extension.

The VSCode extension has an example on how to print code from the editor’s buffer:

It unfortunately requires a tempfile. Enhancing the format command with input from stdin would make this easier. The width arg is currently not exposed.

You confirm my worries.
I hope it’ll be reconsidered, because that will be a regression for all IntelliJ users.
I created this issue:
I see that column width issue is already created:

1 Like

So, how do u guys do formatting?


I wrote a script that reads from stdin, writes that to a temporarily file, formats it with bsc -format, then prints the result to stdout. I added it to my path, then setup my editor to call it on format.

As a bonus, it also will check for bsc in node_modules before checking for a system-wide installation (assuming you editor calls the format command from your project root)


trap 'rm -f "$TMPFILE"' EXIT

TMPFILE=$(mktemp "refmt.XXXXXXXXXX.res" --tmpdir=/tmp)
echo "$(</dev/stdin)" > "$TMPFILE"

if test -f "$BSC"; then
  eval $FORMAT_CMD

if command -v bsc &> /dev/null
  bsc -format "$TMPFILE"
1 Like

Note in the master, we support in place formatting like this:

bsc -o output.res -format input.res # the flag order matters

Cool! The text editor is use (kakoune) which (to my understanding) requires the formatting command to read from stdin and print to stdout, then it will handle replacing the buffer contents. I’m not sure if in-place formatting would work in this case, because it would cause required the buffer to reload to get the formatted version.

In any case, good to know!

we are going to integrate this with rescript format subcommand
see this