Skip to content

Commit

Permalink
Spaces around anonymous function heads (->), closes #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jun 14, 2024
1 parent 4017cf7 commit 0080cdf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ function format_node!(ctx::Context, node::Node)::Union{Node, Nothing, NullNode}
@return_something format_float_literals(ctx, node)
@return_something spaces_around_operators(ctx, node)
@return_something spaces_around_assignments(ctx, node)
@return_something spaces_around_anonymous_function(ctx, node)
@return_something no_spaces_around_colon_etc(ctx, node)
@return_something for_loop_use_in(ctx, node)
@return_something four_space_indent(ctx, node)
Expand Down
8 changes: 8 additions & 0 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ function spaces_around_assignments(ctx::Context, node::Node)
return spaces_around_x(ctx, node, is_x)
end

function spaces_around_anonymous_function(ctx::Context, node::Node)
if !(kind(node) === K"->" && !is_leaf(node))
return nothing
end
is_x = x -> kind(x) === K"->"
return spaces_around_x(ctx, node, is_x)
end

# Opposite of `spaces_around_x`: remove spaces around `x`
function no_spaces_around_x(ctx::Context, node::Node, is_x::F) where F
@assert !is_leaf(node)
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ end
end
end

@testset "whitespace around ->" begin
for sp in ("", " ", " ")
@test format_string("a$(sp)->$(sp)b") == "a -> b"
end
end

@testset "whitespace in comparison chains" begin
for sp in ("", " ", " ")
@test format_string("a$(sp)==$(sp)b") == "a == b"
Expand Down

0 comments on commit 0080cdf

Please sign in to comment.