Rescript-compiler: Reformat source with stdin

Created on 28 Nov 2020  路  14Comments  路  Source: rescript-lang/rescript-compiler

The only option to format a rescript source is to have a file on disk and use bsc -format <filename>.
With refmt, I can use stdin and specify if code is from interface or implementation.

In IntelliJ all the work is done on virtual files that are memory only representation of files, it's important to be able to send the updated code to a bsc process through input stream without to have to create a temporary file on disk.
This is a regression from reasonMl refmt binary.

I hope you can add that functionality in the roadmap.

Most helpful comment

What's the downside of creating a temporary file? the performance impact should be very small

As an editor plugin author, it's much easier to call a code formatter reliably by using stdin and stdout, with errors/warnings going to stderr. No need to worry about file permissions and unique filenames; race conditions; or cleaning up on failure. Performance-wise it indeed doesn't make a significant difference.

The Emacs format-all package supports 50 formatters, all using stdin/stdout/stderr, and because the formatter interface is so simple the package is reliable and maintenance is easy.

All 14 comments

without to have to create a temporary file on disk.

What's the downside of creating a temporary file? the performance impact should be very small

I honestly not ready to maintain that type of code, so I'm genuinely interested in some insight of the roadmap.

hi @giraud bsc is the core of the compiler, in general, we want to make it small and non-essential features are generally not included in bsc, in the future we may provide some format utilities in bsb, e.g,bsb fmt to format the whole project

Thank you for the answer.
Although I still hope it can change in future.

What's the downside of creating a temporary file? the performance impact should be very small

As an editor plugin author, it's much easier to call a code formatter reliably by using stdin and stdout, with errors/warnings going to stderr. No need to worry about file permissions and unique filenames; race conditions; or cleaning up on failure. Performance-wise it indeed doesn't make a significant difference.

The Emacs format-all package supports 50 formatters, all using stdin/stdout/stderr, and because the formatter interface is so simple the package is reliable and maintenance is easy.

Hi, we will think about it once we finish the CLI clean up in next release

we are going to implement this in rescript format subcommand

@giraud would this work for you?

bucklescript$./rescript format -stdin
let u = 3 + 
2 + 
1 /*comment */
let u = 3 + 2 + 1 /* comment */

@bobzhang that should be good. Is there a flag to specify if it's an interface or not ?

@giraud there are two options, either we ask user to provide a filename, rescript format -stdin input.res or provide two flags rescript format -stdin-impl/-stdin-intf

My vote goes to rescript format -stdin input.res.

If there's a flag to specify whether it's an interface or implementation file, that flag could be useful even without -stdin, to override the detection that is based on the filename extension.

-stdin-impl/-stdin-intf seem less standard/common than -stdin + filename

thanks for the feedback, we end up with such CLI:

rescript format -stdin [.res|.resi]
rescript$./rescript format -stdin .resi
let x : int =>     int    
let x: int => int

landed in #5060

fixed in master

Was this page helpful?
0 / 5 - 0 ratings