Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix indentation of documented modules in files with more toplevel code #72

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading