Skip to content

Commit

Permalink
Fix indentation of module with expression as module name
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jul 12, 2024
1 parent d5dea45 commit da6ce25
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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′
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit da6ce25

Please sign in to comment.