Repository: fredrikekre/Runic.jl
Pull request #113 (2024-11-28 12:44:28)

Consistently use single space after `function`
Fix a bug that caused "single space after keyword" to not apply after the `function` keyword in non-standard function definitions.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/fredrikekre/Runic.jl/pull/113?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fredrik+Ekre) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 57.34%. Comparing base [(`c9702f1`)](https://app.codecov.io/gh/fredrikekre/Runic.jl/commit/c9702f18149adb83865d6d2246d3b5d4a858e833?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fredrik+Ekre) to head [(`52b044d`)](https://app.codecov.io/gh/fredrikekre/Runic.jl/commit/52b044dc26af297d870973b02fe13355d292b92e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fredrik+Ekre).

> :exclamation:  There is a different number of reports uploaded between BASE (c9702f1) and HEAD (52b044d). Click for more details.
> 
> <details><summary>HEAD has 5 uploads less than BASE</summary>
>
>| Flag | BASE (c9702f1) | HEAD (52b044d) |
>|------|------|------|
>||8|3|
></details>

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           master     #113       +/-   ##
===========================================
- Coverage   93.57%   57.34%   -36.23%     
===========================================
  Files           8        7        -1     
  Lines        3563     3526       -37     
===========================================
- Hits         3334     2022     -1312     
- Misses        229     1504     +1275     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/fredrikekre/Runic.jl/pull/113?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fredrik+Ekre).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fredrik+Ekre).


Patch:
diff --git a/.github/workflows/Check.yml b/.github/workflows/Check.yml
index 0275c58..bdfddee 100644
--- a/.github/workflows/Check.yml
+++ b/.github/workflows/Check.yml
@@ -38,8 +38,10 @@ jobs:
           git ls-files -z -- '*.jl' | xargs -0 julia --color=yes --project --code-coverage=@ -e 'using Runic; exit(Runic.main(ARGS))' -- --check --diff
         if: ${{ matrix.version != 'nightly' }}
       - uses: julia-actions/julia-processcoverage@v1
-      - uses: codecov/codecov-action@v4
+      - uses: codecov/codecov-action@v5
         with:
-          file: lcov.info
+          files: lcov.info
+          plugins: noop
+          disable_search: true
         env:
           CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6ac78e3..52d5801 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
 [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
diff --git a/src/chisels.jl b/src/chisels.jl
index d1ec258..7b7caa0 100644
--- a/src/chisels.jl
+++ b/src/chisels.jl
@@ -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)
diff --git a/src/runestone.jl b/src/runestone.jl
index b1218a1..cfb2541 100644
--- a/src/runestone.jl
+++ b/src/runestone.jl
@@ -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
@@ -3187,7 +3183,7 @@ function remove_trailing_semicolon(ctx::Context, node::Node)
     pos = position(ctx.fmt_io)
     kids = verified_kids(node)
     kids′ = kids
-    block_predicate = function(x)
+    block_predicate = function (x)
         return kind(x) === K"block" && !JuliaSyntax.has_flags(x, JuliaSyntax.PARENS_FLAG)
     end
     block_idx = findfirst(block_predicate, kids′)
@@ -3296,7 +3292,7 @@ function has_return(node::Node)
     elseif kind(node) in KSet"try if elseif"
         # Look for the initial try/if block and then for
         # catch/else/finally (for try) or elseif/else (for if).
-        pred = function(x)
+        pred = function (x)
             return !is_leaf(x) && kind(x) in KSet"catch else finally elseif block"
         end
         idx = findfirst(pred, kids)


Test patch:
diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml
index ef773d2..11187c3 100644
--- a/.github/workflows/Test.yml
+++ b/.github/workflows/Test.yml
@@ -37,8 +37,10 @@ jobs:
       - uses: julia-actions/julia-processcoverage@v1
         with:
           directories: "src,juliac"
-      - uses: codecov/codecov-action@v4
+      - uses: codecov/codecov-action@v5
         with:
-          file: lcov.info
+          files: lcov.info
+          plugins: noop
+          disable_search: true
         env:
           CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
diff --git a/test/runtests.jl b/test/runtests.jl
index 728c61d..0445b39 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -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"
@@ -865,7 +864,7 @@ end
             # Blocklike RHS
             for thing in (
                     "if c\n    x\nend", "try\n    x\ncatch\n    y\nend",
-                    "let c = 1\n    c\nend", "function()\n    return x\nend",
+                    "let c = 1\n    c\nend", "function ()\n    return x\nend",
                     "\"\"\"\nfoo\n\"\"\"", "r\"\"\"\nfoo\n\"\"\"",
                     "```\nfoo\n```", "r```\nfoo\n```", "```\nfoo\n```x",
                 )
@@ -1179,7 +1178,7 @@ end
         @test format_string("let a = 1 # a\nx$(d)end") == "let a = 1 # a\n    x\nend"
         # function-end
         @test format_string("function f()$(d)x$(d)end") == "function f()\n    return x\nend"
-        @test format_string("function()$(d)x$(d)end") == "function()\n    return x\nend"
+        @test format_string("function()$(d)x$(d)end") == "function ()\n    return x\nend"
         @test format_string("function ()$(d)x$(d)end") == "function ()\n    return x\nend"
         @test format_string("function f end") == "function f end"
         # macro-end