Skip to content

Conversation

xal-0
Copy link
Member

@xal-0 xal-0 commented Jul 28, 2025

In _complete_methods, desugar the :do Expr into a call with a lambda in the first argument.

Fixes #58833.

@xal-0 xal-0 requested a review from giordano July 28, 2025 23:40
@xal-0 xal-0 added bugfix This change fixes an existing bug completions Tab and autocompletion in the repl labels Jul 28, 2025
@giordano giordano added the backport 1.12 Change should be backported to release-1.12 label Jul 28, 2025
Comment on lines +695 to +703
# Desugar do block call into call with lambda
if ex_org.head === :do && length(ex_org.args) >= 2
ex_call = ex_org.args[1]
ex_args = [x for x in ex_call.args if !(x isa Expr && x.head === :parameters)]
ex_params = findfirst(x -> x isa Expr && x.head === :parameters, ex_call.args)
new_args = [ex_args[1], ex_org.args[end], ex_args[2:end]...]
ex_params !== nothing && push!(new_args, ex_call.args[ex_params])
ex_org = Expr(:call, new_args...)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The special-casing of the do block makes me slightly nervous and I wonder if a more general solution would be possible (at the juliacon hackathon you mentioned the option of reading just up to position for the purpose of completion, but that also has drawbacks), but I could confirm this fixes the issue (as confirmed by the tests as well) and that makes me happy!

Copy link
Member Author

@xal-0 xal-0 Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'd call it a special case; I was just unaware when I changed this that JuliaSyntax puts the do inside the call node rather than outside, like Expr:

julia> Base.JuliaSyntax.parsestmt(Base.JuliaSyntax.SyntaxNode, "foo(a) do b; c end")
SyntaxNode:
[call]
  foo                                    :: Identifier
  a                                      :: Identifier
  [do]
    [tuple]
      b                                  :: Identifier
    [block]
      c                                  :: Identifier

julia> Meta.show_sexpr(Expr(Base.JuliaSyntax.parsestmt(Base.JuliaSyntax.SyntaxNode, "foo(a) do b; c end")))
(:do, (:call, :foo, :a), (:->, (:tuple, :b), (:block,
      :(#= line 1 =#),
      :c
    )))

The previous REPL completion code would have ignored the do entirely, iirc. This change actually "fixes" method completion with a do block for free:

julia> foo(f, x::Int, y::Int) = 1
       foo(f, x::String, y::String) = 2
foo (generic function with 2 methods)

julia> foo(3, ) do x; x end
foo(f, x::Int64, y::Int64) @ Main REPL[1]:1

Is this useful enough to justify parsing the input beyond the cursor? I don't know, but so far I haven't seen anything that really causes it to explode.

@xal-0 xal-0 merged commit 50c8956 into JuliaLang:master Jul 29, 2025
8 of 12 checks passed
@KristofferC KristofferC mentioned this pull request Aug 6, 2025
38 tasks
@KristofferC KristofferC mentioned this pull request Aug 19, 2025
27 tasks
This was referenced Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 1.12 Change should be backported to release-1.12 bugfix This change fixes an existing bug completions Tab and autocompletion in the repl
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[REPL] Autocompletion of keyword arguments with do-blocks is broken
2 participants