From da6ce25287a4b4ea0b96907c5ed57459bd41e1f3 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 12 Jul 2024 20:19:27 +0200 Subject: [PATCH] Fix indentation of module with expression as module name --- src/runestone.jl | 24 +++++++++++++++--------- test/runtests.jl | 5 +++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/runestone.jl b/src/runestone.jl index 06c9f90..e8658f6 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -2169,17 +2169,23 @@ function indent_module(ctx::Context, node::Node) kids[mod_idx] = add_tag(mod_node, TAG_INDENT) any_kid_changed = true end - # Second node is the space between keyword and name - # TODO: Make sure there is just a single space + # Next we expect whitespace + identifier, but can also be expression with whitespace + # hidden inside... space_idx = 2 space_node = kids[space_idx] - @assert is_leaf(space_node) && kind(space_node) === K"Whitespace" - # Third node is the module identifier - id_idx = 3 - id_node = kids[id_idx] - @assert kind(id_node) in KSet"Identifier var" - # Fourth node is the module body block. - block_idx = 4 + if kind(space_node) === K"Whitespace" + # Now we need an identifier or var" + id_idx = 3 + id_node = kids[id_idx] + @assert kind(id_node) in KSet"Identifier var" + block_idx = 4 + else + # This can be reached if the module name is interpolated for example + @assert kind(first_leaf(space_node)) === K"Whitespace" + @assert !JuliaSyntax.is_whitespace(space_node) + block_idx = 3 + end + # Next node is the module body block. block_node′ = indent_block(ctx, kids[block_idx]) if block_node′ !== nothing kids[block_idx] = block_node′ diff --git a/test/runtests.jl b/test/runtests.jl index 53b9691..e59c7db 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -600,6 +600,11 @@ end # var"" as module name @test format_string("$(b)module var\"A\"\n$(sp)x\n$(sp)end\nf") == "$(b)module var\"A\"\n x\nend\nf" + # interpolated module name + @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 \$(A)\n$(sp)x\n$(sp)end\nf") == + "$(b)module \$(A)\n x\nend\nf" # single line module @test format_string("$(b)module A; x; end\nf") == "$(b)module A; x; end\nf" end