[Proposal] Embedded Elixir Expressions in Formatter Plugins

46 views
Skip to first unread message

Jonatan Männchen

unread,
Jan 21, 2022, 4:20:08 PM1/21/22
to elixir-lang-core
Hi All,

I would like to propose the ability to format embedded elixir expressions inside of files / sigils being formatted by a formatter plugin.

Situation at the Moment

Formatter Plugins that format files which contain elixir expressions inside do not currently have a way to call the formatter again on the nested expression.

Example Usage

When using both surface and cldr_messages, one could write a file like this:

<div> {gettext(~M"It's my cat's {year, selectordinal, one {#st} two {#nd} few {#rd} other {#th}} birthday!")} </div>

The HTML should be formatted by the surface formatter, the M sigil by the cldr messages formatter.
Since the surface formatter is currently using Code.format_string!/2, the cldr formatter is not applied. Reported to surface formatter as a bug here: https://github.com/surface-ui/surface_formatter/issues/60

I expect to see similar questions in formatter plugins like heex_formatter.

Possible Solution

It would be nice if there was an API, that would allow passing embedded elixir expressions back into the formatter.

One way to achieve that, would be to pass a function to Mix.Tasks.Format.format/2 into opts.

Thanks,
Jony

José Valim

unread,
Jan 21, 2022, 4:44:12 PM1/21/22
to elixir-lang-core
One option is to process the sigils formatter option early, so we pass this option to the Surface formatter (which can then pass it to format_to_string). In a nutshell, move this code to some place earlier in the stack. A PR is welcome! Feel free to ping if you have any questions!

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/5b6b6721-c16e-480f-be95-8ef1a46dbb50n%40googlegroups.com.

Jonatan Männchen

unread,
Jan 21, 2022, 4:47:03 PM1/21/22
to elixir-l...@googlegroups.com
I’ll work on a PR and will let you know if I have any questions.

Thanks 😊 


You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/jFswzSUjsP4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2BQ7VwMr0%2BE2eVcFMpmbsaA3Vr5Ue3OOZbrEJErmRW_UA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages