sorawee / fmt

A code formatter for Racket
Other
75 stars 7 forks source link

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

Open maueroats opened 2 months ago

maueroats commented 2 months ago

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))
Metaxal commented 2 months ago

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.

jackfirth commented 2 months ago

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