Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REPL: know when to release the prefix context when completing #56322

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,21 @@ function complete_loading_candidates!(suggestions::Vector{Completion}, pkgstarts
return suggestions
end

# Check whether we should still be limiting completions to the scope of the prefix
# i.e. `Base.@time myvar` should complete to `Base.@time myvariable` because `myvariable`
# is a valid expression in the active module, but not Base
function in_scope_of_prefix(string::String, pos::Int, separatorpos::Int)
sep = string[separatorpos]
sep == ':' && return true # `using Base: foo` etc.
sep == '.' || return false
pos <= separatorpos && return true
after_separator = string[separatorpos+1:pos]
isempty(after_separator) && return true
startswith(after_separator, "@") || return true
# is a macro, so check for space or `(` that signifies the start of the macro call
return !any(in((' ', '(')), after_separator)
end

function complete_identifiers!(suggestions::Vector{Completion},
context_module::Module, string::String, name::String,
pos::Int, separatorpos::Int, startpos::Int;
Expand All @@ -1130,7 +1145,7 @@ function complete_identifiers!(suggestions::Vector{Completion},
complete_keyword!(suggestions, name)
complete_keyval!(suggestions, name)
end
if separatorpos > 1 && (string[separatorpos] == '.' || string[separatorpos] == ':')
if separatorpos > 1 && in_scope_of_prefix(string, pos, separatorpos)
s = string[1:prevind(string, separatorpos)]
# First see if the whole string up to `pos` is a valid expression. If so, use it.
prefix = Meta.parse(s, raise=false, depwarn=false)
Expand Down
16 changes: 16 additions & 0 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2472,3 +2472,19 @@ let (c, r, res) = test_complete_context("global xxx::Number = Base.", Main)
@test res
@test "pi" ∈ c
end

# release context once past a qualified name
for s in ("Base.@time TestInternalBinding", "Base.@time Base.@time TestInternalBinding",
"Base.@time(TestInternalBinding", "@time(Base.@time TestInternalBinding", "Base.Base.@time TestInternalBinding")
let (c, r, res) = test_complete_context(s; shift=false)
@test res
@test "TestInternalBindingOnly" ∈ c
end
end
for s in ("Base.@time TestInternalBindingOnly.bind", "Base.@time Base.@time TestInternalBindingOnly.bind",
"Base.@time(TestInternalBindingOnly.bind", "@time(Base.@time TestInternalBindingOnly.bind")
let (c, r, res) = test_complete_context(s; shift=false)
@test res
@test "binding" ∈ c
end
end