Code formatting with source locations?

A bug report from @LiberalArtist about the printing of fractions in the stepper has me thinking; the way that the stepper renders code is very fragile, and I'm wondering whether recent work on formatting would make it possible to do a better job.

Specifically, the central challenge that is (or at least was) unique to the stepper was that of identifying where to apply the highlighting. More specifically: I have a syntax object or s-expression I want to format, and there are certain elements of it that should be highlighted. Is there currently a formatting engine that allows me to ask after a block of text is highlighted at what positions certain elements were placed?

I'm thinking that @sorawee and @greghendershott might know a lot more than me about this? Besides of course @mflatt and @robby of course....

I should add for context that the current code (starting at line 137 in stepper/mred-extensions.rkt) uses the pretty-print-hook and the pretty-print-size-hook to capture beginnings and endings of highlight regions, but this requires replacing highlighted regions with special placeholders, and there's other fragile bits around snips and things, and the whole thing seems pretty suboptimal.

Oh and of course @mike almost certainly knows more about certain formatting issues in the stepper than I do!

Unfortunately I have hardly any real experience with racket/gui, and only took a brief look through mred-extensions.rkt, so I probably don't have a lot to contribute here. But since no one better has, yet:

It sounds like the status quo use of pretty-print with all its hooks is a good "formatting engine" (assuming s-expr langs) to build on -- but you want more information, a mapping from the old to new positions?

Could your print-hook record the "old" span of the value it is printing, under some unique ID, and your pre/post hooks record the "new" span?

Or to put this another way, could your post-hook update something like your existing highlight-table (enhanced with "old" positions), and that table is your mapping between old and new positions?

If I follow your code correctly (?), this might nudge your format-sexp to become format-syntax, i.e. you always want srcloc for the "old" positions; idk if that's a problem.