fmt icon indicating copy to clipboard operation
fmt copied to clipboard

Partial formatting

Open jackfirth opened this issue 1 year ago • 2 comments

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.)

jackfirth avatar May 23 '24 01:05 jackfirth

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

sorawee avatar May 23 '24 06:05 sorawee

In the case of Resyntax? Definitely.

jackfirth avatar May 23 '24 06:05 jackfirth

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:

  • Extract that range of the string using substring
  • Determine the initial number of columns at start by counting how many characters are between it and the closes preceding newline (or the start of the string)
  • Call program-format on the substring with #:indent set to the number of leading columns from the previous step

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.

jackfirth avatar Aug 21 '24 20:08 jackfirth