Skip to content

[REPL] Handle empty completion, keywords better #59045

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function complete_symbol!(suggestions::Vector{Completion},
complete_modules_only::Bool=false,
shift::Bool=false)
local mod, t, val
complete_internal_only = false
complete_internal_only = isempty(name)
if prefix !== nothing
res = repl_eval_ex(prefix, context_module)
res === nothing && return Completion[]
Expand Down Expand Up @@ -1095,26 +1095,27 @@ function completions(string::String, pos::Int, context_module::Module=Main, shif

# Symbol completion
# TODO: Should completions replace the identifier at the cursor?
looks_like_ident = Base.isidentifier(@view string[intersect(char_range(cur), 1:pos)])
if cur.parent !== nothing && kind(cur.parent) == K"var"
# Replace the entire var"foo", but search using only "foo".
r = intersect(char_range(cur.parent), 1:pos)
r2 = char_range(children_nt(cur.parent)[1])
s = string[intersect(r2, 1:pos)]
elseif kind(cur) in KSet"Identifier @"
r = intersect(char_range(cur), 1:pos)
s = string[r]
elseif kind(cur) == K"MacroName"
# Include the `@`
r = intersect(prevind(string, cur.position):char_last(cur), 1:pos)
s = string[r]
elseif looks_like_ident || kind(cur) in KSet"Bool Identifier @"
r = intersect(char_range(cur), 1:pos)
s = string[r]
else
r = nextind(string, pos):pos
s = ""
end

complete_modules_only = false
prefix = node_prefix(cur, context_module)
comp_keywords = prefix === nothing
comp_keywords = prefix === nothing && !isempty(s)

# Complete loadable module names:
# import Mod TAB
Expand Down
22 changes: 22 additions & 0 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2720,3 +2720,25 @@ let s = "foo58296(findfi"
@test "findfirst" in c
@test r == 10:15
end

# #58931 - only show local names when completing the empty string
let s = ""
c, r = test_complete_foo(s)
@test "test" in c
@test !("rand" in c)
end

# #58309, #58832 - don't show every name when completing after a full keyword
let s = "true" # bool is a little different (Base.isidentifier special case)
c, r = test_complete(s)
@test "trues" in c
@test "true" in c
@test !("rand" in c)
end

let s = "for"
c, r = test_complete(s)
@test "for" in c
@test "foreach" in c
@test !("rand" in c)
end