# JuliaFormatter.jl Alignment Help

**URL:** <https://discourse.julialang.org/t/juliaformatter-jl-alignment-help/65313>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 26, 2021, 3:38pm UTC](https://discourse.julialang.org/t/juliaformatter-jl-alignment-help/65313 "2021-07-26T15:38:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [July 26, 2021, 3:38pm UTC](https://discourse.julialang.org/t/juliaformatter-jl-alignment-help/65313/1 "2021-07-26T15:38:07Z")

</div>

I’ve been experimenting with `JuliaFormatter v0.15.1` with `1.6.2` and I’ve had some trouble using the alignment flags. I have not changed any default settings or defined a `JuliaFormatter.toml` file.

Here’s the contents of my test file `formattest.jl`

```julia
struct Test
    a::Any
    ab::Any
    cde::Any
end

function test(abcd, edfg; qwert = 2)
    a = 2
    ab = 3+a
    abcd+2 - qwert+ab
end

abe = 1
bc = 2
qwerty = 3

long_variable = 1
other_var = 2

```

which I’m running like

> julia\> format\_file(“formattest.jl”, align\_assignment=true, align\_struct\_field=true)

I know the formatter is running because it fixes the spacing around the `+` operators, but it does not align any of the spacing around `=` or in the type definition.

Am I missing a step or misunderstanding the intended behavior?

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [July 26, 2021, 8:23pm UTC](https://discourse.julialang.org/t/juliaformatter-jl-alignment-help/65313/2 "2021-07-26T20:23:51Z")

</div>

So it looks like you might need to give the formatter a hint for this to work. For example in the type definition, if I align the first two, the third will get corrected. This doesn’t always seem to work, however, but seems to depend sometimes on the length of the variable… maybe?

I believe this section is trying to explain the rule, but I’m having trouble understanding the “heuristic.”  
[https://domluna.github.io/JuliaFormatter.jl/stable/custom\_alignment/#Custom-Alignment](https://domluna.github.io/JuliaFormatter.jl/stable/custom_alignment/#Custom-Alignment)

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 26, 2021, 10:28pm UTC](https://discourse.julialang.org/t/juliaformatter-jl-alignment-help/65313/3 "2021-07-26T22:28:20Z")

</div>

It’s a heuristic. Sometimes it works and sometimes it doesn’t. Figuring out a good alignment is something you can easily do visually (helped because you know the context of the code), but it’s hard to do via code.

> If the `align_assignment` option is enabled the formatter will detect that `var2` is aligned to `variable1` AND `var2` has several whitespaces (\>1) prior to `=` . Since `var3` , `var4` , and `var5` are part of the same code block (no comments or newlines separating code) they will also be aligned.

This seems to explain things. `long_variable` doesn’t get formatted with `qwerty` because there is a space, and `bc` doesn’t get formatted with `abe` because there are not \> 1 whitespaces between `bc` and `=`.

It seems the heuristic is really a “make an attempt manually and it will clean things up for you” rather than “automatically align everything without me doing anything.”

---

<div class="post-metadata">

**Author:** ![domluna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domluna/32/18536_2.png) [@domluna](https://discourse.julialang.org/u/domluna)\
**Post date:** [July 27, 2021, 1:26am UTC](https://discourse.julialang.org/t/juliaformatter-jl-alignment-help/65313/4 "2021-07-27T01:26:42Z")

</div>

odow is right, it’s based on heuristics so you have to nudge it in the right direction by manually adding whitespace. If you find a case where it should align but doesn’t feel free to post an issue.
