Skip to content

Commit

Permalink
complete false & true more generally as vals (#51326)
Browse files Browse the repository at this point in the history
(cherry picked from commit e85f0a5)
  • Loading branch information
IanButterworth authored and nalimilan committed Nov 5, 2023
1 parent d439cdf commit 31ea32b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
41 changes: 28 additions & 13 deletions stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ struct KeywordCompletion <: Completion
keyword::String
end

struct KeyvalCompletion <: Completion
keyval::String
end

struct PathCompletion <: Completion
path::String
end
Expand Down Expand Up @@ -99,6 +103,7 @@ end

_completion_text(c::TextCompletion) = c.text
_completion_text(c::KeywordCompletion) = c.keyword
_completion_text(c::KeyvalCompletion) = c.keyval
_completion_text(c::PathCompletion) = c.path
_completion_text(c::ModuleCompletion) = c.mod
_completion_text(c::PackageCompletion) = c.package
Expand Down Expand Up @@ -213,24 +218,30 @@ function complete_symbol(@nospecialize(ex), name::String, @nospecialize(ffunc),
suggestions
end

function complete_from_list(T::Type, list::Vector{String}, s::Union{String,SubString{String}})
r = searchsorted(list, s)
i = first(r)
n = length(list)
while i <= n && startswith(list[i],s)
r = first(r):i
i += 1
end
Completion[T(kw) for kw in list[r]]
end

const sorted_keywords = [
"abstract type", "baremodule", "begin", "break", "catch", "ccall",
"const", "continue", "do", "else", "elseif", "end", "export", "false",
"const", "continue", "do", "else", "elseif", "end", "export",
"finally", "for", "function", "global", "if", "import",
"let", "local", "macro", "module", "mutable struct",
"primitive type", "quote", "return", "struct",
"true", "try", "using", "while"]
"try", "using", "while"]

function complete_keyword(s::Union{String,SubString{String}})
r = searchsorted(sorted_keywords, s)
i = first(r)
n = length(sorted_keywords)
while i <= n && startswith(sorted_keywords[i],s)
r = first(r):i
i += 1
end
Completion[KeywordCompletion(kw) for kw in sorted_keywords[r]]
end
complete_keyword(s::Union{String,SubString{String}}) = complete_from_list(KeywordCompletion, sorted_keywords, s)

const sorted_keyvals = ["false", "true"]

complete_keyval(s::Union{String,SubString{String}}) = complete_from_list(KeyvalCompletion, sorted_keyvals, s)

function complete_path(path::AbstractString, pos::Int;
use_envpath=false, shell_escape=false,
Expand Down Expand Up @@ -919,6 +930,7 @@ function complete_keyword_argument(partial, last_idx, context_module)

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

return sort!(suggestions, by=completion_text), wordrange
end
Expand All @@ -941,7 +953,10 @@ end

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)
ex = nothing
comp_keywords && append!(suggestions, complete_keyword(name))
if comp_keywords
append!(suggestions, complete_keyword(name))
append!(suggestions, complete_keyval(name))
end
if dotpos > 1 && string[dotpos] == '.'
s = string[1:prevind(string, dotpos)]
# First see if the whole string up to `pos` is a valid expression. If so, use it.
Expand Down
9 changes: 9 additions & 0 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1880,3 +1880,12 @@ let s = "`abc`.e"
# (completions for the fields of `Cmd`)
@test c == Any["env", "exec"]
end

Issue49892(x) = x
let s = "Issue49892(fal"
c, r, res = test_complete_context(s, @__MODULE__)
@test res
for n in ("false", "falses")
@test n in c
end
end

0 comments on commit 31ea32b

Please sign in to comment.