Repository: domluna/JuliaFormatter.jl (julia)

Issue:
Strange formatting
This is the output of JuliaFormatter v0.2.2:

```julia
a = f() do _
    begin
        while true
            aaaa, bbbbbbbb =     let x = y
                    @spawn begin
                        acc = acc′′
                        for _ in _
                            a
                            b
                            ccc = dddd(ee, fff, gggggggggggg)
                        end
                        return
                    end
                end |> ffff
        end
    end
end
```

Note that there is a strangely large space before `let x = y` and the indentation of `@spawn begin ... end` is incorrect.

More strangely, this formatting disappears with some subtle changes.  For example, reducing the width of the identifier `dddd` by one yields a "correct" formatting:


```julia
a = f() do _
    begin
        while true
            aaaa, bbbbbbbb = let x = y
                @spawn begin
                    acc = acc′′
                    for _ in _
                        a
                        b
                        ccc = ddd(ee, fff, gggggggggggg)
                    end
                    return
                end
            end |> ffff
        end
    end
end
```

Pull request:
fixes #166

Patch:
diff --git a/src/nest.jl b/src/nest.jl
index 26e801837..6de1f9737 100644
--- a/src/nest.jl
+++ b/src/nest.jl
@@ -79,6 +79,7 @@ function dedent!(fst::FST, s::State)
     end
     fst.typ === CSTParser.ConditionalOpCall && return
     fst.typ === CSTParser.StringH && return
+    fst.typ === CSTParser.BinaryOpCall && return
 
     # dedent
     fst.indent -= s.indent_size
diff --git a/src/pretty.jl b/src/pretty.jl
index a3c24aff5..eca0d14f9 100644
--- a/src/pretty.jl
+++ b/src/pretty.jl
@@ -698,19 +698,21 @@ function p_macrocall(cst::CSTParser.EXPR, s::State)
             add_node!(t, n, s, join_lines = true)
             add_node!(t, Placeholder(1), s)
         elseif a.fullspan - a.span > 0
-            if has_closer && i < length(cst) - 1
+            if has_closer
                 add_node!(t, n, s, join_lines = true)
-                if cst[i+1].typ !== CSTParser.Parameters
+                if cst[i+1].typ !== CSTParser.Parameters && i < length(cst) - 1
                     add_node!(t, Whitespace(1), s)
                 end
-            elseif !has_closer && i < length(cst)
-                add_node!(t, n, s, join_lines = true)
-                add_node!(t, Whitespace(1), s)
             else
-                add_node!(t, n, s, join_lines = true)
+                add_node!(t, n, s, join_lines = true, max_padding = 0)
+                i < length(cst) && add_node!(t, Whitespace(1), s)
             end
         else
-            add_node!(t, n, s, join_lines = true)
+            if has_closer
+                add_node!(t, n, s, join_lines = true)
+            else
+                add_node!(t, n, s, join_lines = true, max_padding = 0)
+            end
         end
     end
     t


Test patch:
diff --git a/test/files/Docs.jl b/test/files/Docs.jl
index 6f9eb0043..cdcd38fda 100644
--- a/test/files/Docs.jl
+++ b/test/files/Docs.jl
@@ -220,8 +220,8 @@ function doc!(__module__::Module, b::Binding, str::DocStr, @nospecialize sig = U
         # We allow for docstrings to be updated, but print a warning since it is possible
         # that over-writing a docstring *may* have been accidental.  The warning
         # is suppressed for symbols in Main, for interactive use (#23011).
-        __module__ == Main ||
-        @warn "Replacing docs for `$b :: $sig` in module `$(__module__)`"
+        __module__ ==
+        Main || @warn "Replacing docs for `$b :: $sig` in module `$(__module__)`"
     else
         # The ordering of docstrings for each Binding is defined by the order in which they
         # are initially added. Replacing a specific docstring does not change it's ordering.
diff --git a/test/runtests.jl b/test/runtests.jl
index caa004c60..4e81fb7d1 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -937,6 +937,21 @@ end
         @test fmt(str, 4, 1) == str
     end
 
+    @testset "macro block" begin
+        str = raw"""
+        @spawn begin
+            acc = acc′′
+            for _ in _
+                a
+                b
+                ccc = dddd(ee, fff, gggggggggggg)
+            end
+            return
+        end"""
+        t = run_pretty(str, 80)
+        @test length(t) == 41
+    end
+
     @testset "begin" begin
         str = """
         begin