Browse Source

Ensure single space before module name

pull/90/head
Fredrik Ekre 1 year ago
parent
commit
5e81fbd0eb
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 11
      src/runestone.jl
  2. 7
      test/runtests.jl

11
src/runestone.jl

@ -1232,7 +1232,10 @@ end
# global, const # global, const
function spaces_around_keywords(ctx::Context, node::Node) function spaces_around_keywords(ctx::Context, node::Node)
is_leaf(node) && return nothing 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) if !(kind(node) in keyword_set)
return nothing return nothing
end end
@ -1331,7 +1334,7 @@ function spaces_around_keywords(ctx::Context, node::Node)
@assert false # Unreachable? @assert false # Unreachable?
else else
# Reachable in e.g. `T where{T}`, `if(`, ... insert space # 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 any_changes = true
if kids′ === kids if kids′ === kids
kids′ = kids[1:(i - 1)] kids′ = kids[1:(i - 1)]
@ -3001,10 +3004,10 @@ function indent_module(ctx::Context, node::Node; do_indent::Bool = true)
space_idx = 2 space_idx = 2
space_node = kids[space_idx] space_node = kids[space_idx]
if kind(space_node) === K"Whitespace" 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_idx = 3
id_node = kids[id_idx] 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 block_idx = 4
else else
# This can be reached if the module name is interpolated or parenthesized, for # This can be reached if the module name is interpolated or parenthesized, for

7
test/runtests.jl

@ -507,6 +507,8 @@ end
@test format_string("f()$(sp)do; y end") == "f() do;\n y\nend" @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)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("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") for word in ("local", "global"), rhs in ("a", "a, b", "a = 1", "a, b = 1, 2")
word == "const" && rhs in ("a", "a, b") && continue word == "const" && rhs in ("a", "a, b") && continue
@test format_string("$(word)$(sp)$(rhs)") == "$(word) $(rhs)" @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("function f()\n return(1)\nend") == "function f()\n return (1)\nend"
@test format_string("local(a)") == "local (a)" @test format_string("local(a)") == "local (a)"
@test format_string("global(a)") == "global (a)" @test format_string("global(a)") == "global (a)"
@test format_string("module(A)\nend") == "module (A)\nend"
end end
@testset "replace ∈ and = with in in for loops and generators" begin @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") == @test format_string("$(b)module \$A\n$(sp)x\n$(sp)end\nf") ==
"$(b)module \$A\n x\nend\nf" "$(b)module \$A\n x\nend\nf"
# parenthesized module name (Why....) # parenthesized module name (Why....)
@test format_string("$(b)module(A)\n$(sp)x\n$(sp)end\nf") == @test format_string("$(b)module$(sp)(A)\n$(sp)x\n$(sp)end\nf") ==
"$(b)module(A)\n x\nend\nf" "$(b)module (A)\n x\nend\nf"
@test format_string("$(b)module \$(A)\n$(sp)x\n$(sp)end\nf") == @test format_string("$(b)module \$(A)\n$(sp)x\n$(sp)end\nf") ==
"$(b)module \$(A)\n x\nend\nf" "$(b)module \$(A)\n x\nend\nf"
# single line module # single line module

Loading…
Cancel
Save