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

mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29305 241
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New

We're in Beta

About us Mission Statement