fmt icon indicating copy to clipboard operation
fmt copied to clipboard

How to make fmt work with inline images in DrRacket? [enhancement]

Open maueroats opened this issue 1 year ago • 2 comments

Running the simple version of the formatter on code with inline images in DrRacket makes the images disappear, just like copy and pasting would.

It would be nice if there were a way to run the formatter and retain the inline images.

For completeness, here is the reformatter quickscript that I am using (which is obviously the root cause of the error).

(define-script reformat
  #:label "Reformat"
  (λ (selection #:definitions defs)
    (define txt (send defs get-text))  ;; <--- but what else could I do?
    (define new-txt (program-format txt))
    (send defs begin-edit-sequence)
    (send defs erase)
    (send defs insert new-txt)
    (send defs end-edit-sequence)
    #f))

maueroats avatar Sep 05 '24 12:09 maueroats

Filtering out the images and placing them back into the formatted code sounds pretty tricky to me.

But perhaps you can just format only the selected portion of the code? That would just amount to:

(define-script reformat
  #:label "Reformat"
  (λ (selection)
    (program-format selection)))

If the images are only at the top level, it may be possible to split the formatting by blocks and avoid the locations with images, but I haven't dealt much with non-text-only code, so I don't know from the top of my head how to do that.

Metaxal avatar Sep 05 '24 12:09 Metaxal

You'll also need to pass in the column number of the selection as the #:indent argument, so that fmt knows how much indentation to use when adding newlines

jackfirth avatar Sep 06 '24 04:09 jackfirth