dhall-lang / dhall-haskell

Maintainable configuration files
https://dhall-lang.org/
BSD 3-Clause "New" or "Revised" License
912 stars 213 forks source link

Pretty-printer: escape unprintable characters #2595

Open kukimik opened 3 months ago

kukimik commented 3 months ago

Can we modify the pretty-printer (or at least the dhall format and dhall freeze commands) to unicode-escape unprintable characters? Perhaps this could be switched by an option?

I have some text containing unprintable characters that I want to be visible in the source code, so I use the escaping (e.g. "\u007f"). Unfortunately dhall format replaces it with the unprintable character verbatim.

It would be great if the original form (escaped/unescaped) could be preserved, but that is perhaps too much to ask for.

Gabriella439 commented 3 months ago

Yeah, I think this is a reasonable change

kukimik commented 3 months ago

I've found out that the escaping rules used by the formatter are the same as used by Text/show and are defined here:

https://github.com/dhall-lang/dhall-haskell/blob/main/dhall/src/Dhall/Pretty/Internal.hs#L1871-L1890

Changing the rules for Text/show would need a backward incompatible change to the standard. Moreover, the escaping would probably become slower (the list of unprintable Unicode characters is not fixed, so we'd have to use a Unicode character class instead of a range comparison). Therefore, I think it would be preferable to add separate escaping rules for the formatter and keep Text/show as it is.

On the other hand keeping the formatter in sync with Text/show also has its merits. So I think adding an option (defaulting to the current behaviour, to keep things backward compatible) that would affect the pretty-printer is the way to go.