From 0080cdfe204b5a97dffb9af3b431ebf44b2e36b0 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 14 Jun 2024 11:10:43 +0200 Subject: [PATCH] Spaces around anonymous function heads (->), closes #7. --- src/Runic.jl | 1 + src/runestone.jl | 8 ++++++++ test/runtests.jl | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/src/Runic.jl b/src/Runic.jl index 7a67408..c263ff6 100644 --- a/src/Runic.jl +++ b/src/Runic.jl @@ -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) diff --git a/src/runestone.jl b/src/runestone.jl index 683500f..7a84d75 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 935b91e..b2fe459 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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"