Browse Source

Indentation: handle empty function definitions

pull/19/head
Fredrik Ekre 2 years ago
parent
commit
8ea1074b12
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 15
      src/runestone.jl
  2. 1
      test/runtests.jl

15
src/runestone.jl

@ -517,9 +517,22 @@ function indent_function_or_macro(ctx::Context, node::Node)
space_idx = 2 space_idx = 2
space_node = kids[space_idx] space_node = kids[space_idx]
@assert is_leaf(space_node) && kind(space_node) === K"Whitespace" @assert is_leaf(space_node) && kind(space_node) === K"Whitespace"
# Third node is the signature (call/where/::) # Third node is the signature (call/where/::) for standard method definitions but just
# an Identifier for cases like `function f end`.
sig_idx = 3 sig_idx = 3
sig_node = kids[sig_idx] sig_node = kids[sig_idx]
if kind(sig_node) === K"Identifier"
# Empty function definition like `function f end`.
# TODO: Make sure the spaces around are correct
end_idx = findnext(x -> kind(x) === K"end", kids, sig_idx + 1)::Int
end_node = kids[end_idx]
@assert is_leaf(end_node) && kind(end_node) === K"end"
if !has_tag(end_node, TAG_DEDENT)
kids[end_idx] = add_tag(end_node, TAG_DEDENT)
any_kid_changed = true
end
return any_kid_changed ? node : nothing
end
@assert !is_leaf(sig_node) && kind(sig_node) in KSet"call where ::" @assert !is_leaf(sig_node) && kind(sig_node) in KSet"call where ::"
# Fourth node is the function/macro body block. # Fourth node is the function/macro body block.
block_idx = 4 block_idx = 4

1
test/runtests.jl

@ -291,6 +291,7 @@ end
# function-end # function-end
@test format_string("function f()\n$(sp)x\n$(sp)end") == @test format_string("function f()\n$(sp)x\n$(sp)end") ==
"function f()\n x\nend" "function f()\n x\nend"
@test format_string("function f end") == "function f end"
# macro-end # macro-end
@test format_string("macro f()\n$(sp)x\n$(sp)end") == @test format_string("macro f()\n$(sp)x\n$(sp)end") ==
"macro f()\n x\nend" "macro f()\n x\nend"

Loading…
Cancel
Save