Browse Source

Consistently use single space after `function` (#113)

Fix a bug that caused "single space after keyword" to not apply after
the `function` keyword in non-standard function definitions.
pull/116/head
Fredrik Ekre 1 year ago committed by GitHub
parent
commit
a5a8c81935
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      CHANGELOG.md
  2. 33
      src/chisels.jl
  3. 4
      src/runestone.jl
  4. 3
      test/runtests.jl

6
CHANGELOG.md

@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased changes
### Changed
- Fix a bug that caused "single space after keyword" to not apply after the `function`
keyword in non-standard function definitions. ([#113])
## [v1.0.1] - 2024-11-28
### Fixed
- Fix an incorrect whitespace assertion in function indentation ([#109], [#110]).
@ -19,3 +24,4 @@ First stable release of Runic.jl. See [README.md](README.md) for details and doc @@ -19,3 +24,4 @@ First stable release of Runic.jl. See [README.md](README.md) for details and doc
[v1.0.1]: https://github.com/fredrikekre/Runic.jl/releases/tag/v1.0.1
[#109]: https://github.com/fredrikekre/Runic.jl/issues/109
[#110]: https://github.com/fredrikekre/Runic.jl/issues/110
[#113]: https://github.com/fredrikekre/Runic.jl/issues/113

33
src/chisels.jl

@ -581,39 +581,6 @@ function unwrap_to_call_or_tuple(x) @@ -581,39 +581,6 @@ function unwrap_to_call_or_tuple(x)
return unwrap_to_call_or_tuple(xkids[xi])
end
# TODO: This should be reworked to be more specific, in particular K"parens" is maybe not
# correct (found in e.g. `function(x * b)\n\nend`).
function is_longform_anon_function(node::Node)
is_leaf(node) && return false
kind(node) === K"function" || return false
kids = verified_kids(node)
kw = findfirst(x -> kind(x) === K"function", kids)
@assert kw !== nothing
sig = findnext(x -> !JuliaSyntax.is_whitespace(x), kids, kw + 1)::Int
sigkid = kids[sig]
maybe_tuple = unwrap_to_call_or_tuple(sigkid)
if maybe_tuple === nothing
return false
else
return kind(maybe_tuple) in KSet"tuple parens"
end
end
function is_longform_functor(node::Node)
is_leaf(node) && return false
kind(node) === K"function" || return false
kids = verified_kids(node)
kw = findfirst(x -> kind(x) === K"function", kids)
@assert kw !== nothing
calli = findnext(x -> !JuliaSyntax.is_whitespace(x), kids, kw + 1)::Int
call = kids[calli]
if !is_leaf(call) && kind(call) == K"call" &&
kind(first(verified_kids(call))) === K"parens"
return true
end
return false
end
# Just like `JuliaSyntax.is_infix_op_call`, but also check that the node is K"call" or
# K"dotcall"
function is_infix_op_call(node::Node)

4
src/runestone.jl

@ -1121,10 +1121,6 @@ function spaces_around_keywords(ctx::Context, node::Node) @@ -1121,10 +1121,6 @@ function spaces_around_keywords(ctx::Context, node::Node)
if !(kind(node) in keyword_set)
return nothing
end
if is_longform_anon_function(node)
# TODO: `function(` should have no space, handled elsewhere
return nothing
end
kids = verified_kids(node)
kids′ = kids
any_changes = false

3
test/runtests.jl

@ -824,9 +824,8 @@ end @@ -824,9 +824,8 @@ end
# Functors
@test format_string("function$(sp)(a::A)(b)\nx\nend") ==
"function (a::A)(b)\n return x\nend"
# TODO: Spaces after function keyword isn't removed.
@test format_string("function$(sp)(a * b)\nreturn\nend") ==
"function$(sp)(a * b)\n return\nend"
"function (a * b)\n return\nend"
# https://github.com/fredrikekre/Runic.jl/issues/109
@test format_string("function$(sp)(::Type{T})(::Int) where {T}\n$(sp)return T\n$(sp)end") ==
"function (::Type{T})(::Int) where {T}\n return T\nend"

Loading…
Cancel
Save