sorawee / fmt

A code formatter for Racket
Other
72 stars 6 forks source link

Partial formatting #54

Closed jackfirth closed 4 days ago

jackfirth commented 3 months ago

I want to use fmt in Resyntax to format the refactored output code produced by Resyntax. However, Resyntax needs to avoid formatting an entire file if it only changes a few lines in the file. On the Resyntax side, I can compute a range set representing the exact character positions I want to format in the file. Can fmt be extended to support specifying what parts of a file to format? Ideally (for me) by adding a #:ranges keyword argument to program-format that accepts a range-set? of natural numbers representing character positions within the file.

For context, Resyntax avoids touching modified lines because its suggestions are turned into GitHub pull request comments. Automated tools can't leave comments on unchanged lines in a pull request. (Or if they can, I haven't figured out how.)

sorawee commented 3 months ago

Can #:ranges be guaranteed to contain a series of well-formed expressions? If so, yes, this is totally doable!

jackfirth commented 3 months ago

In the case of Resyntax? Definitely.

jackfirth commented 4 days ago

Actually I was able to make this work entirely by using the existing #:indent argument. To format a single [start, end) range I did the following:

Worked like a charm. This probably wouldn't work if fmt supported something like file-level comments telling fmt about custom settings, but it works well enough for my purposes.