Skip to content

Commit

Permalink
Fix indentation of documented modules in files with more toplevel code (
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored Oct 21, 2024
1 parent 0a3c5b1 commit 0bb9b42
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2936,9 +2936,31 @@ function indent_comparison(ctx::Context, node::Node)
return continue_all_newlines(ctx, node)
end

# Indent a nested documented module
function indent_doc_module(ctx::Context, node::Node; do_indent::Bool)
@assert kind(node) === K"doc"
kids = verified_kids(node)
mod_idx = findfirst(x -> kind(x) === K"module", kids)::Int
pos = position(ctx.fmt_io)
for i in 1:(mod_idx - 1)
accept_node!(ctx, kids[i])
end
mod′ = indent_module(ctx, kids[mod_idx]; do_indent = do_indent)
seek(ctx.fmt_io, pos)
if mod′ === nothing
return nothing
end
kids′ = copy(kids)
kids′[mod_idx] = mod′
return make_node(node, kids′)
end

# Indent a nested module
function indent_module(ctx::Context, node::Node; do_indent::Bool = true)
@assert kind(node) === K"module"
@assert kind(node) in KSet"module doc"
if kind(node) === K"doc"
return indent_doc_module(ctx, node; do_indent = do_indent)
end
kids = verified_kids(node)
any_kid_changed = false
pos = position(ctx.fmt_io)
Expand Down Expand Up @@ -2998,7 +3020,10 @@ end
function indent_toplevel(ctx::Context, node::Node)
@assert kind(node) === K"toplevel"
kids = verified_kids(node)
mod_idx = findfirst(x -> kind(x) === K"module", kids)
mod_idx = findfirst(kids) do x
kind(x) === K"module" ||
(kind(x) === K"doc" && findfirst(y -> kind(y) === K"module", verified_kids(x)) !== nothing)
end
if mod_idx === nothing
# No module here
return nothing
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,9 @@ end
# nested documented modules
@test format_string("\"doc\"\n$(b)module A\n\"doc\"\n$(b)module B\n$(sp)x\n$(sp)end\n$(sp)end") ==
"\"doc\"\n$(b)module A\n\"doc\"\n$(b)module B\n x\nend\nend"
# toplevel documented module with more things
@test format_string("\"doc\"\n$(b)module A\n$(sp)x\nend\nf") ==
"\"doc\"\n$(b)module A\n x\nend\nf"
# 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"
Expand Down

0 comments on commit 0bb9b42

Please sign in to comment.