kokolegorille
Configure formatter to ignore some part of code
Hello everyone, I have a noob question about formatter…
I have this code
@attack_array [
0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,2,0,0,0, # 0-19
0,0,0,5,0,0,5,0,0,0,0,0,2,0,0,0,0,0,5,0, # 20-39
0,0,0,5,0,0,0,0,2,0,0,0,0,5,0,0,0,0,0,0, # 40-59
5,0,0,0,2,0,0,0,5,0,0,0,0,0,0,0,0,5,0,0, # 60-79
2,0,0,5,0,0,0,0,0,0,0,0,0,0,5,6,2,6,5,0, # 80-99
0,0,0,0,0,0,0,0,0,0,6,4,1,4,6,0,0,0,0,0, # 100-119
0,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,0,0,0,0, # 120-139
0,0,6,3,1,3,6,0,0,0,0,0,0,0,0,0,0,0,5,6, # 140-159
2,6,5,0,0,0,0,0,0,0,0,0,0,5,0,0,2,0,0,5, # 160-179
0,0,0,0,0,0,0,0,5,0,0,0,2,0,0,0,5,0,0,0, # 180-199
0,0,0,5,0,0,0,0,2,0,0,0,0,5,0,0,0,0,5,0, # 200-219
0,0,0,0,2,0,0,0,0,0,5,0,0,5,0,0,0,0,0,0, # 220-239
2,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0 # 240-256
]
@delta_array [
0, 0, 0, 0, 0, 0, 0, 0, 0, -17, 0, 0, 0, 0, 0, 0, -16, 0, 0, 0,
0, 0, 0, -15, 0, 0, -17, 0, 0, 0, 0, 0, -16, 0, 0, 0, 0, 0, -15, 0,
0, 0, 0, -17, 0, 0, 0, 0, -16, 0, 0, 0, 0, -15, 0, 0, 0, 0, 0, 0,
-17, 0, 0, 0, -16, 0, 0, 0, -15, 0, 0, 0, 0, 0, 0, 0, 0, -17, 0, 0,
-16, 0, 0, -15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -17, -33, -16, -31, -15, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -18, -17, -16, -15, -14, 0, 0, 0, 0, 0,
0, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 14, 15, 16, 17, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 31,
16, 33, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 16, 0, 0, 17,
0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 16, 0, 0, 0, 17, 0, 0, 0,
0, 0, 0, 15, 0, 0, 0, 0, 16, 0, 0, 0, 0, 17, 0, 0, 0, 0, 15, 0,
0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 17, 0, 0, 15, 0, 0, 0, 0, 0, 0,
16, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
I would like obviously to keep this structure, but when I run formatter, it will output each number on a single line. I have been looking at mix format — Mix v1.20.2 but could not find a way to preserve format. Is there a way to tell formatter to ignore constants?
Thanks for taking time.
Marked As Solved
blatyo
There aren’t any exclusion rules in the formatter. The closest you could achieve is not formatting the file at all. In the .formatter.exs, you can specify an inputs pattern to match files to format. Make the pattern not match that file to have it not formatted.
@aptinio’s suggestion isn’t a bad one. When I have data like this, I typically prefer to put it in a file that is conducive to the format. For me, that’s typically JSON and CSV. You can make the elixir compiler aware that you depend on a file at compile time by using @external_resource [1].
Also Liked
aptinio
Only way I could think of is to enter the list as a string using heredoc and eval it with Code.eval_string. But I haven’t tried it. I hope that helps.
kokolegorille
Using @aptinio suggestion I now have this after format…
@attack_array Code.eval_string("""
[
0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,2,0,0,0, # 0-19
0,0,0,5,0,0,5,0,0,0,0,0,2,0,0,0,0,0,5,0, # 20-39
0,0,0,5,0,0,0,0,2,0,0,0,0,5,0,0,0,0,0,0, # 40-59
5,0,0,0,2,0,0,0,5,0,0,0,0,0,0,0,0,5,0,0, # 60-79
2,0,0,5,0,0,0,0,0,0,0,0,0,0,5,6,2,6,5,0, # 80-99
0,0,0,0,0,0,0,0,0,0,6,4,1,4,6,0,0,0,0,0, # 100-119
0,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,0,0,0,0, # 120-139
0,0,6,3,1,3,6,0,0,0,0,0,0,0,0,0,0,0,5,6, # 140-159
2,6,5,0,0,0,0,0,0,0,0,0,0,5,0,0,2,0,0,5, # 160-179
0,0,0,0,0,0,0,0,5,0,0,0,2,0,0,0,5,0,0,0, # 180-199
0,0,0,5,0,0,0,0,2,0,0,0,0,5,0,0,0,0,5,0, # 200-219
0,0,0,0,2,0,0,0,0,0,5,0,0,5,0,0,0,0,0,0, # 220-239
2,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0 # 240-256
]
""") |> elem(0)
This is good enough for me, thank You both for your advices ![]()
aptinio
I’m glad to hear it worked. I’m a stickler for code formatting but I feel this edge case is a valid reason to work around the formatter.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









