[Proposal] Extend the mix formatter to work with eex files.

222 views
Skip to first unread message

Adam Lancaster

unread,
May 30, 2020, 8:39:05 AM5/30/20
to elixir-lang-core
Hello

It would be really great if the mix formatter could be configured to work with .eex templates. Would that be a bananas amount of work?

Best

Adam

Allen Madsen

unread,
May 30, 2020, 9:48:50 AM5/30/20
to elixir-l...@googlegroups.com
Yea, I believe this has come up before. The problem is .eex can be embedded into anything: YAML, HTML, JSON, etc. So, in order for the formatter to be able to format .eex files, it would 1) have to know what the surrounding data is and 2) have a formatter for that specific tool in the language.

The first problem could be solvable by some heuristic, but could only solve the problem for the types of files known. It's basically a problem with infinite surface area. The second problem would mean the code base of core would need to include some understanding of every surrounding format. So, because of those reasons, I'd expect something like this to be rejected.

However, if the goal were to make the formatter extensible so that formatting of files other than .ex and .exs can be handled by a third party plugin, that might be more applicable for inclusion into core. That would require some proposed design for how such a thing might work though.  

--
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/3c85260b-9435-42fb-9298-c7f79b3b5f7f%40googlegroups.com.

Adam Lancaster

unread,
May 30, 2020, 10:26:07 AM5/30/20
to elixir-l...@googlegroups.com
Thanks that’s interesting context.

I agree supporting every format would be a lot of work. I would also have mixed feelings about making the formatter extensible if it would mean a wealth of community owned formatters, as that would defeat some of the purpose of having an opinionated formatter.

However I think you could make the formatter extensible in architecture, and allow official extensions to be built one at a time. Like a html formatter that works only on .html.eex files and .html.leex files would add a lot of value on its own, even if YAML and JSON are never supported.

Best

Adam




Wojtek Mach

unread,
May 30, 2020, 11:30:08 AM5/30/20
to elixir-l...@googlegroups.com
In my opinion formatting non-Elixir code is definitely out of the scope of project. Instead of adding different backends to `mix format`, I’d do the opposite: find a formatter that supports multiple backends and add `mix format` as one of them.

I thought the feature request was about formatting Elixir code _inside_ EEx templates and just, i.e. we leave the surrounding code as is, but format the inner Elixir code. That to me seems far more reasonable.

Here’s a proof-of-concept for that idea: https://gist.github.com/wojtekmach/a1c1648b8015dbb67d35b89f2e743266

    before:
    <p><%= bar( 1,  2 ) %></p>

    after:
    <p><%= bar(1, 2) %></p>

If this looks like something worth pursuing, let me know!

Adam Lancaster

unread,
May 30, 2020, 11:38:16 AM5/30/20
to elixir-l...@googlegroups.com
Yea that makes way more sense. If the formatter can ignore the stuff it’s being injected in that would for sure be cool. Then the usual formatters for html etc can probably be used anyway. 


Max Veytsman

unread,
Jul 26, 2021, 6:33:06 PM7/26/21
to elixir-lang-core
Reviving this as there has been a new development - HEEx (https://github.com/phoenixframework/phoenix_live_view/pull/1440)

Phoenix LiveView is moving to HEEx templates which unlike EEx are HTML only, and includes an HTML tokenizer & parser. 

Allen had the following concern:
> The problem is .eex can be embedded into anything: YAML, HTML, JSON, etc.

Adding support for HEEx templates makes the problem tractable - we only need to be formatting HTML, which is already parsed for us by the HTMLEngine that powers HEEx.

It would also have the added benefit of formatting code in sigils, e.g, 

def render(assigns) do
  ~H"""
  <div>
     Format this HTML plz
  </div> 
  """
end

José Valim

unread,
Jul 27, 2021, 1:53:04 AM7/27/21
to elixir-l...@googlegroups.com
To make sure we are all on the same page, supporting EEx in mix format has two parts:

1. Having a formatter that works on EEx files

2. Adding hooks to "mix format" that allows custom formatting. In particular, we need to hook into certain extensions and into sigils

Step 1 is a huge amount of work per previous conversation.

Step 2 is definitely doable and pull requests to add said hooks are welcome.

Reply all
Reply to author
Forward
0 new messages