Skip to content

Commit 6073643

Browse files
know when to release the prefix context
1 parent db3d816 commit 6073643

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

stdlib/REPL/src/REPLCompletions.jl

+16-1
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,21 @@ function complete_loading_candidates!(suggestions::Vector{Completion}, pkgstarts
11201120
return suggestions
11211121
end
11221122

1123+
# Check whether we should still be limiting completions to the scope of the prefix
1124+
# i.e. `Base.@time myvar` should complete to `Base.@time myvariable` because `myvariable`
1125+
# is a valid expression in the active module, but not Base
1126+
function in_scope_of_prefix(string::String, pos::Int, separatorpos::Int)
1127+
sep = string[separatorpos]
1128+
sep == ':' && return true # `using Base: foo` etc.
1129+
sep == '.' || return false
1130+
pos <= separatorpos && return true
1131+
after_separator = string[separatorpos+1:pos]
1132+
isempty(after_separator) && return true
1133+
startswith(after_separator, "@") || return true
1134+
# is a macro, so check for space or `(` that signifies the start of the macro call
1135+
return !any(in((' ', '(')), after_separator)
1136+
end
1137+
11231138
function complete_identifiers!(suggestions::Vector{Completion},
11241139
context_module::Module, string::String, name::String,
11251140
pos::Int, separatorpos::Int, startpos::Int;
@@ -1130,7 +1145,7 @@ function complete_identifiers!(suggestions::Vector{Completion},
11301145
complete_keyword!(suggestions, name)
11311146
complete_keyval!(suggestions, name)
11321147
end
1133-
if separatorpos > 1 && (string[separatorpos] == '.' || string[separatorpos] == ':')
1148+
if separatorpos > 1 && in_scope_of_prefix(string, pos, separatorpos)
11341149
s = string[1:prevind(string, separatorpos)]
11351150
# First see if the whole string up to `pos` is a valid expression. If so, use it.
11361151
prefix = Meta.parse(s, raise=false, depwarn=false)

stdlib/REPL/test/replcompletions.jl

+16
Original file line numberDiff line numberDiff line change
@@ -2472,3 +2472,19 @@ let (c, r, res) = test_complete_context("global xxx::Number = Base.", Main)
24722472
@test res
24732473
@test "pi" c
24742474
end
2475+
2476+
# release context once past a qualified name
2477+
for s in ("Base.@time TestInternalBinding", "Base.@time Base.@time TestInternalBinding",
2478+
"Base.@time(TestInternalBinding", "@time(Base.@time TestInternalBinding", "Base.Base.@time TestInternalBinding")
2479+
let (c, r, res) = test_complete_context(s; shift=false)
2480+
@test res
2481+
@test "TestInternalBindingOnly" c
2482+
end
2483+
end
2484+
for s in ("Base.@time TestInternalBindingOnly.bind", "Base.@time Base.@time TestInternalBindingOnly.bind",
2485+
"Base.@time(TestInternalBindingOnly.bind", "@time(Base.@time TestInternalBindingOnly.bind")
2486+
let (c, r, res) = test_complete_context(s; shift=false)
2487+
@test res
2488+
@test "binding" c
2489+
end
2490+
end

0 commit comments

Comments
 (0)