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

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement