diff --git a/src/runestone.jl b/src/runestone.jl index 4a56540..04597e3 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -517,9 +517,22 @@ function indent_function_or_macro(ctx::Context, node::Node) space_idx = 2 space_node = kids[space_idx] @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_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 ::" # Fourth node is the function/macro body block. block_idx = 4 diff --git a/test/runtests.jl b/test/runtests.jl index d0173d8..0135238 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -291,6 +291,7 @@ end # function-end @test format_string("function f()\n$(sp)x\n$(sp)end") == "function f()\n x\nend" + @test format_string("function f end") == "function f end" # macro-end @test format_string("macro f()\n$(sp)x\n$(sp)end") == "macro f()\n x\nend"