Skip to content
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

complete false & true more generally as vals #51326

Merged
Merged
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
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 @@ -220,24 +225,30 @@ function add_field_completions!(suggestions::Vector{Completion}, name::String, @
end
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 @@ -926,6 +937,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))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix


return sort!(suggestions, by=completion_text), wordrange
end
Expand All @@ -948,7 +960,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:dotpos-1]
# 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 @@ -1887,3 +1887,12 @@ let s = "union_some_ref(1, 1.0)."
@test res
@test "value" in c && "x" in c
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