nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: domluna/JuliaFormatter.jl
Pull request #36 (2019-08-20 01:34:22)

add TopLevel node; only nest Quote node if using quote keyword


Patch:
diff --git a/src/pretty.jl b/src/pretty.jl
index 168f644e..80fa9fc0 100644
--- a/src/pretty.jl
+++ b/src/pretty.jl
@@ -198,6 +198,8 @@ function pretty(x::CSTParser.EXPR, s::State)
         return p_if(x, s)
     elseif x.typ === CSTParser.Try
         return p_try(x, s)
+    elseif x.typ === CSTParser.TopLevel
+        return p_toplevel(x, s)
     elseif x.typ === CSTParser.Begin
         return p_begin(x, s)
     elseif x.typ === CSTParser.Quote
@@ -703,6 +705,20 @@ function p_vardef(x, s)
     t
 end
 
+# TopLevel
+function p_toplevel(x, s)
+    t = PTree(x, nspaces(s))
+    for a in x.args
+        if a.kind === Tokens.NOTHING
+            s.offset += a.fullspan
+            continue
+        end
+        add_node!(t, pretty(a, s), max_padding = s.indent_size)
+        add_node!(t, Semicolon())
+    end
+    t
+end
+
 # Begin
 function p_begin(x, s)
     t = PTree(x, nspaces(s))
@@ -1006,7 +1022,7 @@ block_type(x::CSTParser.EXPR) =
     x.typ === CSTParser.If ||
     x.typ === CSTParser.Do ||
     x.typ === CSTParser.Begin ||
-    x.typ === CSTParser.Quote ||
+    (x.typ === CSTParser.Quote && x.args[1].kind === Tokens.QUOTE) ||
     x.typ === CSTParser.Try ||
     x.typ === CSTParser.For || x.typ === CSTParser.While || x.typ === CSTParser.Let
 


Test patch:
diff --git a/test/runtests.jl b/test/runtests.jl
index d714b75b..c498a33b 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -35,6 +35,20 @@ end
         @test fmt(str) == str
     end
 
+    @testset "toplevel" begin
+        str = """
+
+        hello = "string";
+
+        a = 10;
+
+
+        c = 50;"""
+        @test fmt(str) == str
+        t = run_pretty(str, 80)
+        @test length(t) == 17
+    end
+
     @testset "for = vs in normalization" begin
         str = """
         for i = 1:n
@@ -208,6 +222,9 @@ end
             end"""
         @test fmt(str_) == str
 
+        str = """foo() = :(Union{})"""
+        @test fmt(str) == str
+
         str_ = """foo() = for i=1:10 body end"""
         str = """
         foo() =
Repository: domluna/JuliaFormatter.jl
Pull request #64 (2019-09-12 04:36:37)

return text as is if there's no source code
solves #54 


Patch:
diff --git a/src/JuliaFormatter.jl b/src/JuliaFormatter.jl
index 303ce077..00e6a28b 100644
--- a/src/JuliaFormatter.jl
+++ b/src/JuliaFormatter.jl
@@ -127,6 +127,9 @@ function format_text(text::AbstractString; indent::Integer = 4, margin::Integer
     x, ps = CSTParser.parse(CSTParser.ParseState(text), true)
     ps.errored && error("Parsing error for input $text")
 
+    # no actual code
+    x.args[1].kind === Tokens.NOTHING && length(x) == 1 && return text
+
     d = Document(text)
     # If "nofmt" occurs in a comment on line 1 do not format
     occursin("nofmt", get(d.comments, 1, "")) && (return text)


Test patch:
diff --git a/test/runtests.jl b/test/runtests.jl
index efc8cbc0..856599dd 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -35,6 +35,20 @@ end
         @test fmt("") == ""
         @test fmt("a") == "a"
         @test fmt("a  #foo") == "a  #foo"
+        @test fmt("#foo") == "#foo"
+
+        str = """
+        #=
+        Hello, world!
+        =#"""
+        @test fmt(str) == str
+
+        str = """
+        #=
+        Hello, world!
+        =#
+        a"""
+        @test fmt(str) == str
     end
 
     @testset "nofmt" begin
Repository: domluna/JuliaFormatter.jl
Pull request #86 (2019-10-03 05:14:23)

remove trailing comma in params if not nested


Patch:
diff --git a/src/JuliaFormatter.jl b/src/JuliaFormatter.jl
index e5a4ffd9..0527ee8f 100644
--- a/src/JuliaFormatter.jl
+++ b/src/JuliaFormatter.jl
@@ -141,7 +141,7 @@ code as another string. The formatting options are:
 - `margin` which defaults to 92 columns
 
 """
-function format_text(text::AbstractString; indent::Integer = 4, margin::Integer = 92,)
+function format_text(text::AbstractString; indent::Integer = 4, margin::Integer = 92)
     if isempty(text)
         return text
     end
diff --git a/src/nest.jl b/src/nest.jl
index f46d1efe..7f48a5c1 100644
--- a/src/nest.jl
+++ b/src/nest.jl
@@ -178,9 +178,8 @@ function n_tuple!(x, s; extra_width = 0)
     line_width = s.line_offset + length(x) + extra_width
     idx = findlast(n -> n.typ === PLACEHOLDER, x.nodes)
     opener = is_opener(x.nodes[1])
-    # @info "ENTERING" x.typ s.line_offset length(x) extra_width
+    # @info "ENTERING" idx x.typ s.line_offset length(x) extra_width
     if idx !== nothing && (line_width > s.margin || x.force_nest)
-        # @debug "ENTERING" x.indent s.line_offset x.typ
         if opener
             x.nodes[end].indent = x.indent
         end
diff --git a/src/pretty.jl b/src/pretty.jl
index f1b36ecf..3ab10bc4 100644
--- a/src/pretty.jl
+++ b/src/pretty.jl
@@ -1332,12 +1332,7 @@ function p_call(x, s)
 
     for (i, a) in enumerate(x.args[3:end])
         if i + 2 == length(x) && multi_arg
-            pn = x.args[i+1]
-            if pn.typ !== CSTParser.Parameters
-                add_node!(t, TrailingComma(), s)
-            elseif pn.typ === CSTParser.Parameters && !CSTParser.is_comma(pn.args[end])
-                add_node!(t, TrailingComma(), s)
-            end
+            add_node!(t, TrailingComma(), s)
             add_node!(t, Placeholder(0), s)
             add_node!(t, pretty(a, s), s, join_lines = true)
         elseif CSTParser.is_comma(a) && i < length(x) - 3 && !is_punc(x.args[i+3])
@@ -1474,7 +1469,9 @@ function p_params(x, s)
     t = PTree(x, nspaces(s))
     for (i, a) in enumerate(x)
         n = pretty(a, s)
-        if CSTParser.is_comma(a) && i < length(x) && !is_punc(x.args[i+1])
+        if i == length(x) && CSTParser.is_comma(a)
+            # do nothing
+        elseif CSTParser.is_comma(a) && i < length(x) && !is_punc(x.args[i+1])
             add_node!(t, n, s, join_lines = true)
             add_node!(t, Placeholder(1), s)
         else


Test patch:
diff --git a/test/runtests.jl b/test/runtests.jl
index 515143d9..2e59dbda 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -434,6 +434,7 @@ end
         @test fmt("func(a  ,b; c)") == "func(a, b; c)"
         @test fmt("func(a=1,b; c=1)") == "func(a = 1, b; c = 1)"
         @test fmt("func(; c = 1)", 4, 1) == "func(; c = 1)"
+        @test fmt("func(; c = 1,)") == "func(; c = 1)"
     end
 
     @testset "begin" begin
Repository: domluna/JuliaFormatter.jl
Pull request #88 (2019-10-04 05:15:47)

add a leading zero to float literals
fixes #88 


Patch:
diff --git a/src/pretty.jl b/src/pretty.jl
index 3ab10bc4..de2a02f1 100644
--- a/src/pretty.jl
+++ b/src/pretty.jl
@@ -444,6 +444,8 @@ function p_literal(x, s)
         if x.kind === Tokens.FLOAT && x.val[end] == '.'
             # If a floating point ends in `.`, add trailing zero.
             val *= '0'
+        elseif x.kind === Tokens.FLOAT && x.val[1] == '.'
+            val = '0' * val
         end
         s.offset += x.fullspan
         return PTree(x, loc[1], loc[1], val)


Test patch:
diff --git a/test/runtests.jl b/test/runtests.jl
index 2e59dbda..3ccac23f 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -2630,6 +2630,17 @@ end
         """) == "1.0 + 2.0\n"
     end
 
+    @testset "Leading zeros" begin
+        @test fmt(".1") == "0.1"
+        @test fmt("a * .1 + b") == "a * 0.1 + b"
+        @test fmt(".1 + .2 * im") == "0.1 + 0.2 * im"
+        @test fmt("[.1, .2]") == "[0.1, 0.2]"
+        @test fmt("""
+        .1 +
+            .2
+        """) == "0.1 + 0.2\n"
+    end
+
     # https://github.com/domluna/JuliaFormatter.jl/issues/77
     @testset "issue 77" begin
         str_ = """
Repository: domluna/JuliaFormatter.jl
Pull request #134 (2019-11-24 05:15:38)

Nest WhereOpCall front-to-back in all cases
Fixes a bug where the return type width and extra margin for WhereOpCall was not correct causing the node to nest back to front.


Patch:
diff --git a/src/nest.jl b/src/nest.jl
index bbfe9575..8494ccda 100644
--- a/src/nest.jl
+++ b/src/nest.jl
@@ -537,19 +537,19 @@ function n_binarycall!(x, s)
         idx = findfirst(n -> n.typ === CSTParser.WhereOpCall, x.nodes)
         return_width = 0
         if idx !== nothing && idx > 1
-            return_width = length(x[idx][1]) + length(x[2])
+            return_width = length(x[idx]) + length(x[2])
         elseif idx === nothing
             return_width, _ = length_to(x, [NEWLINE], start = 2)
         end
 
-        # @info "" return_width
-        # @info "" s.line_offset return_width length(x[1])
+        # @info "" idx map(n -> n.typ, x.nodes)
+        # @info "" s.line_offset length(x[1]) return_width x.extra_margin
 
         for (i, n) in enumerate(x.nodes)
             if n.typ === NEWLINE
                 s.line_offset = x.indent
             elseif i == 1
-                n.extra_margin = return_width
+                n.extra_margin = return_width + x.extra_margin
                 nest!(n, s)
             elseif i == idx
                 n.extra_margin = x.extra_margin


Test patch:
diff --git a/test/runtests.jl b/test/runtests.jl
index 77a5bf08..d96354f8 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -3097,12 +3097,46 @@ end
         @test fmt(str, 4, 33) == str_
 
         str_ = """
-        foo(a, b, c)::Rtype where {
+        foo(
+            a,
+            b,
+            c,
+        )::Rtype where {A,B} = 10"""
+        @test fmt(str, 4, 32) == str_
+        @test fmt(str, 4, 25) == str_
+
+        str_ = """
+        foo(
+            a,
+            b,
+            c,
+        )::Rtype where {A,B} =
+            10"""
+        @test fmt(str, 4, 24) == str_
+        @test fmt(str, 4, 22) == str_
+
+        str_ = """
+        foo(
+            a,
+            b,
+            c,
+        )::Rtype where {
             A,
             B,
         } = 10"""
-        @test fmt(str, 4, 32) == str_
-        @test fmt(str, 4, 19) == str_
+        @test fmt(str, 4, 21) == str_
+
+        str_ = """
+        foo(
+          a,
+          b,
+          c,
+        )::Rtype where {
+          A,
+          B,
+        } =
+          10"""
+        @test fmt(str, 2, 1) == str_
 
         str_ = """
         foo(