Browse Source

Fix indentation of module with expression as module name

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

16
src/runestone.jl

@ -2169,17 +2169,23 @@ function indent_module(ctx::Context, node::Node) @@ -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
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"
# Fourth node is the module body block.
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′

5
test/runtests.jl

@ -600,6 +600,11 @@ end @@ -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

Loading…
Cancel
Save