From 5e81fbd0eb0ece69595d3b302212432b1dabdd2e Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 4 Nov 2024 21:00:55 +0100 Subject: [PATCH] Ensure single space before module name --- src/runestone.jl | 11 +++++++---- test/runtests.jl | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/runestone.jl b/src/runestone.jl index 3f88835..f0153af 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -1232,7 +1232,10 @@ end # global, const function spaces_around_keywords(ctx::Context, node::Node) is_leaf(node) && return nothing - keyword_set = KSet"where do mutable struct abstract primitive type function if elseif catch while return local global const" + keyword_set = KSet""" + where do mutable struct abstract primitive type function if elseif catch while return + local global const module baremodule + """ if !(kind(node) in keyword_set) return nothing end @@ -1331,7 +1334,7 @@ function spaces_around_keywords(ctx::Context, node::Node) @assert false # Unreachable? else # Reachable in e.g. `T where{T}`, `if(`, ... insert space - @assert kind(node) in KSet"where if elseif while do function return local global" + @assert kind(node) in KSet"where if elseif while do function return local global module baremodule" any_changes = true if kids′ === kids kids′ = kids[1:(i - 1)] @@ -3001,10 +3004,10 @@ function indent_module(ctx::Context, node::Node; do_indent::Bool = true) space_idx = 2 space_node = kids[space_idx] if kind(space_node) === K"Whitespace" - # Now we need an identifier or var" + # Now we need an identifier, var" or parens... id_idx = 3 id_node = kids[id_idx] - @assert kind(id_node) in KSet"Identifier var" + @assert kind(id_node) in KSet"Identifier var parens" block_idx = 4 else # This can be reached if the module name is interpolated or parenthesized, for diff --git a/test/runtests.jl b/test/runtests.jl index 981cf26..620a365 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -507,6 +507,8 @@ end @test format_string("f()$(sp)do; y end") == "f() do;\n y\nend" @test format_string("function f()\n return$(sp)1\nend") == "function f()\n return 1\nend" @test format_string("function f()\n return$(sp)\nend") == "function f()\n return\nend" + @test format_string("module$(sp)A\nend") == "module A\nend" + @test format_string("module$(sp)(A)\nend") == "module (A)\nend" for word in ("local", "global"), rhs in ("a", "a, b", "a = 1", "a, b = 1, 2") word == "const" && rhs in ("a", "a, b") && continue @test format_string("$(word)$(sp)$(rhs)") == "$(word) $(rhs)" @@ -528,6 +530,7 @@ end @test format_string("function f()\n return(1)\nend") == "function f()\n return (1)\nend" @test format_string("local(a)") == "local (a)" @test format_string("global(a)") == "global (a)" + @test format_string("module(A)\nend") == "module (A)\nend" end @testset "replace ∈ and = with in in for loops and generators" begin @@ -712,8 +715,8 @@ end @test format_string("$(b)module \$A\n$(sp)x\n$(sp)end\nf") == "$(b)module \$A\n x\nend\nf" # parenthesized module name (Why....) - @test format_string("$(b)module(A)\n$(sp)x\n$(sp)end\nf") == - "$(b)module(A)\n x\nend\nf" + @test format_string("$(b)module$(sp)(A)\n$(sp)x\n$(sp)end\nf") == + "$(b)module (A)\n x\nend\nf" @test format_string("$(b)module \$(A)\n$(sp)x\n$(sp)end\nf") == "$(b)module \$(A)\n x\nend\nf" # single line module