Repository: domluna/JuliaFormatter.jl (julia)

Issue:
Two lines starting with macros are formatted as though they are one line
This 

```julia
@doc _unit_docstr("diameterPart", "diameter part", "1/60 rad", "rad")
@unit diameterPartᵃ "diameterPart" DiameterPartᵃ (1radᵃ // 60) false
```

gets formatted to this mess (which even violates the line length)

```julia
@doc _unit_docstr("diameterPart", "diameter part", "1/60 rad", "rad") @unit diameterPartᵃ "diameterPart" DiameterPartᵃ (
    1radᵃ//60
) false
```

Pull request:
fix #682
fix #684
fix #663

Patch:
diff --git a/src/fst.jl b/src/fst.jl
index cc47d8c2b..6e75ffab3 100644
--- a/src/fst.jl
+++ b/src/fst.jl
@@ -493,7 +493,9 @@ function add_node!(
            en.typ === Flatten ||
            en.typ === MacroCall ||
            en.typ === MacroBlock ||
-           en.typ === SEMICOLON
+           en.typ === SEMICOLON ||
+           # arg = @macro foo
+           en.typ === Kw && (en[end].typ === MacroCall || en[end].typ === MacroBlock)
             # don't add trailing comma in these cases
         elseif is_comma(en) && t.typ === TupleN && n_args(t.ref[]) == 1
             # preserve comma
diff --git a/src/styles/default/nest.jl b/src/styles/default/nest.jl
index a16921db7..2a5f29216 100644
--- a/src/styles/default/nest.jl
+++ b/src/styles/default/nest.jl
@@ -98,6 +98,8 @@ function nest!(ds::DefaultStyle, fst::FST, s::State)
         n_call!(style, fst, s)
     elseif fst.typ === MacroCall
         n_macrocall!(style, fst, s)
+    elseif fst.typ === MacroBlock
+        n_block!(style, fst, s)
     elseif fst.typ === RefN
         n_ref!(style, fst, s)
     elseif fst.typ === TypedVcat
@@ -249,7 +251,6 @@ function n_tuple!(ds::DefaultStyle, fst::FST, s::State)
     idx = findlast(n -> n.typ === PLACEHOLDER, nodes)
     has_closer = is_closer(fst[end])
 
-    line_offset = s.line_offset
     if has_closer
         fst[end].indent = fst.indent
     end
@@ -796,7 +797,6 @@ function n_block!(ds::DefaultStyle, fst::FST, s::State; indent = -1)
     line_margin = s.line_offset + length(fst) + fst.extra_margin
     nodes = fst.nodes::Vector
     idx = findfirst(n -> n.typ === PLACEHOLDER, nodes)
-    line_offset = s.line_offset
     has_nl = false
     indent >= 0 && (fst.indent = indent)
 
diff --git a/src/styles/default/pretty.jl b/src/styles/default/pretty.jl
index d2cb40212..0fcdb69f1 100644
--- a/src/styles/default/pretty.jl
+++ b/src/styles/default/pretty.jl
@@ -659,6 +659,9 @@ function p_macrocall(ds::DefaultStyle, cst::CSTParser.EXPR, s::State)
             add_node!(t, n, s, join_lines = true)
             add_node!(t, Placeholder(1), s)
         elseif t.typ === MacroBlock
+            if n.typ === MacroBlock && t[end].typ === WHITESPACE
+                t[end] = Placeholder(length(t[end].val))
+            end
             if has_closer
                 add_node!(t, n, s, join_lines = true)
                 if i < length(cst) - 1 && cst[i+1].head != :parameters


Test patch:
diff --git a/test/issues.jl b/test/issues.jl
index db14c2326..ecab8f71a 100644
--- a/test/issues.jl
+++ b/test/issues.jl
@@ -316,16 +316,26 @@
         end"""
 
         # NOTE: this looks slightly off because we're compensating for escaping quotes
+        # NOTE: updated test in light of fixing #663 which allows breaking on consecutive macrocalls
+        # Meaning "@macro1 @macro2"
+        #
+        # can be
+        #
+        # """
+        # @macro1
+        # @macro2
+        # """
         str = """
         begin
             f() do
-                @info @sprintf \"\"\"
-                Δmass   = %.16e\"\"\" abs(weightedsum(Q) - weightedsum(Qe)) /
-                                   weightedsum(Qe)
+                @info
+                @sprintf \"\"\"
+          Δmass   = %.16e\"\"\" abs(weightedsum(Q) - weightedsum(Qe)) / weightedsum(Qe)
             end
         end"""
         @test fmt(str_, m = 81) == str
-        @test fmt(str, m = 82) == str_
+        @test fmt(str_, m = 82) == str_
+        @test fmt(str, m = 200) == str
     end
 
     @testset "issue #202" begin
@@ -1527,6 +1537,17 @@
         @test fmt(s, 4, 92) == s
     end
 
+    @testset "663" begin
+        s1 = """
+        @macro1 foo @macro2 bar(a=10)
+        """
+        s2 = """
+        @macro1 foo
+        @macro2 bar(a = 10)
+        """
+        @test fmt(s1, 4, 19) == s2
+    end
+
     @testset "667" begin
         s = raw"""
         \"""
@@ -1543,6 +1564,18 @@
         @test fmt(s, format_docstrings = true) == s
     end
 
+    @testset "682" begin
+        str_ = """
+        var = call(a; arg=@macrocall b)"""
+        str = """
+        var =
+            call(
+                a;
+                arg = @macrocall b
+            )"""
+        @test fmt(str_, 4, 1) == str
+    end
+
     @testset "686" begin
         s1 = """
         abc() = begin