PID_ONE

PID_ONE

How to make whitespace changes via the AST with mix format?

I’m trying to write a couple of mix format plugins for my project. So far I’ve been able to achieve most of what I want (e.g. rewriting function calls) via manipulating the AST except for one that I’m trying to write that involves comments and whitespace modifications.

Specifically, I’d like this plugin to be able to insert blank lines at arbitrary points in the file it’s formatting. For example, I’d like it to be able to insert a comment or then insert some number of blank lines between that comment and the module definition or to insert blank lines between function definitions.

defmodule MyModule do
...

would become

# This is my comment

defmodule MyModule do

and

def function_one(foo) do
...
end
def function_two(bar) do
...
end

would become

def function_one(foo) do
...
end

def function_two(bar) do
...
end

Is this possible through manipulating the AST? And if so, how?

So far I haven’t been able to by changing the line metadata of the AST nodes. However, reading through some of the Styler codebase they are at least manipulating multiline functions through the metadata. To me that seems essentially the same so it seems like I am missing something.

However, from my testing it also seems like information about whitespace gets lost/ignored in the translation to/from AST. For example, this is an AST from my plugin generated from Code.string_to_quoted_with_comments!/2 and despite changing all the line metadata this information is lost going from AST back to string (to be written to the file):

iex(1)> new_ast = {:defmodule, [do: [line: 36], end: [line: 40], line: 36], [{:__aliases__, [last: [line: 36], line: 36], [:PlaygroundSimple]}, [{{:__block__, [line: 36], [:do]}, {:def, [do: [line: 37], end: [line: 39], line: 37], [{:test, [line: 37], nil}, [{{:__block__, [line: 37], [:do]}, {:__block__, [delimiter: "\"", line: 38], ["Hello, World"]}}]]}}]]}
{:defmodule, [do: [line: 36], end: [line: 40], line: 36],
 [
   {:__aliases__, [last: [line: 36], line: 36], [:PlaygroundSimple]},
   [
     {{:__block__, [line: 36], [:do]},
      {:def, [do: [line: 37], end: [line: 39], line: 37],
       [
         {:test, [line: 37], nil},
         [
           {{:__block__, [line: 37], [:do]},
            {:__block__, [delimiter: "\"", line: 38], ["Hello, World"]}}
         ]
       ]}}
   ]
 ]}

iex(3)> Code.quoted_to_algebra(new_ast) |> Inspect.Algebra.format(120)
["defmodule", " ", "PlaygroundSimple", " do", "\n  ", "def", " ", "test", " do",
 "\n    ", "\"", "Hello, World", "\"", "\n  ", "end", "\n", "end"]

If that is the case is there a way around the AST not being able to manipulate whitespace that is recommended? Using regex/string manipulation seems like an alternative but not a particularly robust one.

First Post!

hst337

hst337

hexdocs.pm/sourceror

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement