Skip to content

Commit e85f0a5

Browse files
complete false & true more generally as vals (#51326)
1 parent 42d98a2 commit e85f0a5

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

stdlib/REPL/src/REPLCompletions.jl

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ struct KeywordCompletion <: Completion
1919
keyword::String
2020
end
2121

22+
struct KeyvalCompletion <: Completion
23+
keyval::String
24+
end
25+
2226
struct PathCompletion <: Completion
2327
path::String
2428
end
@@ -99,6 +103,7 @@ end
99103

100104
_completion_text(c::TextCompletion) = c.text
101105
_completion_text(c::KeywordCompletion) = c.keyword
106+
_completion_text(c::KeyvalCompletion) = c.keyval
102107
_completion_text(c::PathCompletion) = c.path
103108
_completion_text(c::ModuleCompletion) = c.mod
104109
_completion_text(c::PackageCompletion) = c.package
@@ -220,24 +225,30 @@ function add_field_completions!(suggestions::Vector{Completion}, name::String, @
220225
end
221226
end
222227

228+
function complete_from_list(T::Type, list::Vector{String}, s::Union{String,SubString{String}})
229+
r = searchsorted(list, s)
230+
i = first(r)
231+
n = length(list)
232+
while i <= n && startswith(list[i],s)
233+
r = first(r):i
234+
i += 1
235+
end
236+
Completion[T(kw) for kw in list[r]]
237+
end
238+
223239
const sorted_keywords = [
224240
"abstract type", "baremodule", "begin", "break", "catch", "ccall",
225-
"const", "continue", "do", "else", "elseif", "end", "export", "false",
241+
"const", "continue", "do", "else", "elseif", "end", "export",
226242
"finally", "for", "function", "global", "if", "import",
227243
"let", "local", "macro", "module", "mutable struct",
228244
"primitive type", "quote", "return", "struct",
229-
"true", "try", "using", "while"]
245+
"try", "using", "while"]
230246

231-
function complete_keyword(s::Union{String,SubString{String}})
232-
r = searchsorted(sorted_keywords, s)
233-
i = first(r)
234-
n = length(sorted_keywords)
235-
while i <= n && startswith(sorted_keywords[i],s)
236-
r = first(r):i
237-
i += 1
238-
end
239-
Completion[KeywordCompletion(kw) for kw in sorted_keywords[r]]
240-
end
247+
complete_keyword(s::Union{String,SubString{String}}) = complete_from_list(KeywordCompletion, sorted_keywords, s)
248+
249+
const sorted_keyvals = ["false", "true"]
250+
251+
complete_keyval(s::Union{String,SubString{String}}) = complete_from_list(KeyvalCompletion, sorted_keyvals, s)
241252

242253
function complete_path(path::AbstractString, pos::Int;
243254
use_envpath=false, shell_escape=false,
@@ -926,6 +937,7 @@ function complete_keyword_argument(partial, last_idx, context_module)
926937

927938
suggestions = Completion[KeywordArgumentCompletion(kwarg) for kwarg in kwargs]
928939
append!(suggestions, complete_symbol(nothing, last_word, Returns(true), context_module))
940+
append!(suggestions, complete_keyval(last_word))
929941

930942
return sort!(suggestions, by=completion_text), wordrange
931943
end
@@ -948,7 +960,10 @@ end
948960

949961
function complete_identifiers!(suggestions::Vector{Completion}, @nospecialize(ffunc::Function), context_module::Module, string::String, name::String, pos::Int, dotpos::Int, startpos::Int, comp_keywords=false)
950962
ex = nothing
951-
comp_keywords && append!(suggestions, complete_keyword(name))
963+
if comp_keywords
964+
append!(suggestions, complete_keyword(name))
965+
append!(suggestions, complete_keyval(name))
966+
end
952967
if dotpos > 1 && string[dotpos] == '.'
953968
s = string[1:dotpos-1]
954969
# First see if the whole string up to `pos` is a valid expression. If so, use it.

stdlib/REPL/test/replcompletions.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,3 +1887,12 @@ let s = "union_some_ref(1, 1.0)."
18871887
@test res
18881888
@test "value" in c && "x" in c
18891889
end
1890+
1891+
Issue49892(x) = x
1892+
let s = "Issue49892(fal"
1893+
c, r, res = test_complete_context(s, @__MODULE__)
1894+
@test res
1895+
for n in ("false", "falses")
1896+
@test n in c
1897+
end
1898+
end

0 commit comments

Comments
 (0)