Skip to content

Commit 1eca37e

Browse files
stevengjKristofferC
authored andcommitted
fixes for getindex tab-completion (#31499)
* fixes for getindex tab-completion * test fix
1 parent 802e719 commit 1eca37e

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

stdlib/REPL/src/REPLCompletions.jl

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,11 @@ function dict_identifier_key(str,tag)
530530
sym = Symbol(name)
531531
isdefined(obj, sym) || return (nothing, nothing, nothing)
532532
obj = getfield(obj, sym)
533-
# Avoid `isdefined(::Array, ::Symbol)`
534-
isa(obj, Array) && return (nothing, nothing, nothing)
535533
end
536-
begin_of_key = first(something(findnext(r"\S", str, nextind(str, end_of_identifier) + 1), 1)) # 1 for [
537-
begin_of_key==0 && return (true, nothing, nothing)
538-
partial_key = str[begin_of_key:end]
539-
(isa(obj, AbstractDict) && length(obj) < 1e6) || return (true, nothing, nothing)
540-
return (obj, partial_key, begin_of_key)
534+
(isa(obj, AbstractDict) && length(obj) < 1_000_000) || return (nothing, nothing, nothing)
535+
begin_of_key = something(findnext(!isspace, str, nextind(str, end_of_identifier) + 1), # +1 for [
536+
lastindex(str)+1)
537+
return (obj, str[begin_of_key:end], begin_of_key)
541538
end
542539

543540
# This needs to be a separate non-inlined function, see #19441
@@ -581,13 +578,9 @@ function completions(string, pos, context_module=Main)::Completions
581578
# if completing a key in a Dict
582579
identifier, partial_key, loc = dict_identifier_key(partial,inc_tag)
583580
if identifier !== nothing
584-
if partial_key !== nothing
585-
matches = find_dict_matches(identifier, partial_key)
586-
length(matches)==1 && (length(string) <= pos || string[pos+1] != ']') && (matches[1]*="]")
587-
length(matches)>0 && return [DictCompletion(identifier, match) for match in sort!(matches)], loc:pos, true
588-
else
589-
return Completion[], 0:-1, false
590-
end
581+
matches = find_dict_matches(identifier, partial_key)
582+
length(matches)==1 && (lastindex(string) <= pos || string[nextind(string,pos)] != ']') && (matches[1]*=']')
583+
length(matches)>0 && return [DictCompletion(identifier, match) for match in sort!(matches)], loc:pos, true
591584
end
592585

593586
# otherwise...
@@ -605,7 +598,7 @@ function completions(string, pos, context_module=Main)::Completions
605598
length(paths) == 1 && # Only close if there's a single choice,
606599
!isdir(expanduser(replace(string[startpos:prevind(string, first(r))] * paths[1].path,
607600
r"\\ " => " "))) && # except if it's a directory
608-
(length(string) <= pos ||
601+
(lastindex(string) <= pos ||
609602
string[nextind(string,pos)] != '"') # or there's already a " at the cursor.
610603
paths[1] = PathCompletion(paths[1].path * "\"")
611604
end

stdlib/REPL/test/replcompletions.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ let s = "\"C:\\\\ \\alpha"
266266
@test length(c) == 1
267267
end
268268

269+
# test latex symbol completion in getindex expressions (#24705)
270+
let s = "tuple[\\alpha"
271+
c, r, res = test_complete_context(s)
272+
@test c[1] == "α"
273+
@test r == 7:12
274+
@test length(c) == 1
275+
end
276+
269277
let s = "\\a"
270278
c, r, res = test_complete(s)
271279
"\\alpha" in c
@@ -863,7 +871,7 @@ function test_dict_completion(dict_name)
863871
@test c == Any[":α]"]
864872
s = "$dict_name["
865873
c, r = test_complete(s)
866-
@test !isempty(c)
874+
@test c == sort!(repr.(keys(Main.CompletionFoo.test_dict)))
867875
end
868876
test_dict_completion("CompletionFoo.test_dict")
869877
test_dict_completion("CompletionFoo.test_customdict")

0 commit comments

Comments
 (0)