Skip to content

Commit

Permalink
Handle infix operators in REPL completion
Browse files Browse the repository at this point in the history
  • Loading branch information
Liozou committed Sep 18, 2023
1 parent 26ceebf commit 163391c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ function complete_identifiers!(suggestions::Vector{Completion}, @nospecialize(ff
append!(suggestions, complete_keyval(name))
end
if dotpos > 1 && string[dotpos] == '.'
s = string[1:dotpos-1]
s = string[1:prevind(string, dotpos)]
# First see if the whole string up to `pos` is a valid expression. If so, use it.
ex = Meta.parse(s, raise=false, depwarn=false)
if isexpr(ex, :incomplete)
Expand Down Expand Up @@ -1005,6 +1005,17 @@ function complete_identifiers!(suggestions::Vector{Completion}, @nospecialize(ff
ex = Meta.parse(lookup_name, raise=false, depwarn=false)
end
isexpr(ex, :incomplete) && (ex = nothing)
elseif isexpr(ex, :call) && length(ex.args) > 1
isinfix = s[end] != ')'
# A complete call expression that does not finish with ')' is an infix call.
if !isinfix
# Handle infix call argument completion of the form bar + foo(qux).
frange, end_of_identifier = find_start_brace(@view s[1:end-1])
isinfix = Meta.parse(@view(s[frange[1]:end]), raise=false, depwarn=false) == ex.args[end]
end
if isinfix
ex = ex.args[end]
end
end
end
append!(suggestions, complete_symbol(ex, name, ffunc, context_module))
Expand Down
14 changes: 14 additions & 0 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let ex = quote
end
type_test = Test_x(Test_y(1))
(::Test_y)() = "", ""
Base.:(+)(x::Test_x, y::Test_y) = Test_x(Test_y(x.xx.yy + y.yy))
module CompletionFoo2

end
Expand Down Expand Up @@ -1910,3 +1911,16 @@ end
# If this last test starts failing, that's okay, just pick a new example symbol:
@test !Base.isexported(Base, :ispublic)
end

# issue #51194
for (s, compl) in (("2*CompletionFoo.nam", "named"),
(":a isa CompletionFoo.test!1", "test!12"),
("-CompletionFoo.Test_y(3).", "yy"),
("99 ⨷⁻ᵨ⁷ CompletionFoo.type_test.", "xx"),
("CompletionFoo.type_test + CompletionFoo.Test_y(2).", "yy"),
("foo'CompletionFoo.test!1", "test!12"))
c, r = test_complete(s)
@test only(c) == compl
end

@test isempty(first(test_complete("ℯ.")))

0 comments on commit 163391c

Please sign in to comment.