# Code formatting with source locations?

**URL:** <https://racket.discourse.group/t/code-formatting-with-source-locations/2459>\
**Category:** General\
**Created:** [November 4, 2023, 11:48pm UTC](https://racket.discourse.group/t/code-formatting-with-source-locations/2459 "2023-11-04T23:48:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [November 4, 2023, 11:48pm UTC](https://racket.discourse.group/t/code-formatting-with-source-locations/2459/1 "2023-11-04T23:48:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [November 4, 2023, 11:59pm UTC](https://racket.discourse.group/t/code-formatting-with-source-locations/2459/2 "2023-11-04T23:59:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [November 5, 2023, 12:00am UTC](https://racket.discourse.group/t/code-formatting-with-source-locations/2459/3 "2023-11-05T00:00:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greghendershott](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/greghendershott/32/98_2.png) [@greghendershott](https://racket.discourse.group/u/greghendershott)\
**Post date:** [November 6, 2023, 4:31pm UTC](https://racket.discourse.group/t/code-formatting-with-source-locations/2459/4 "2023-11-06T16:31:38Z")

</div>

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.
