From 31ea32b9f8728889452ca1770f83b7fe559a7254 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 15 Sep 2023 14:02:41 -0400 Subject: [PATCH] complete false & true more generally as vals (#51326) (cherry picked from commit e85f0a5a718f68e581b07eb60fd0d8203b0cd0da) --- stdlib/REPL/src/REPLCompletions.jl | 41 ++++++++++++++++++++--------- stdlib/REPL/test/replcompletions.jl | 9 +++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/stdlib/REPL/src/REPLCompletions.jl b/stdlib/REPL/src/REPLCompletions.jl index 57797583de75f..a6cb12ab05211 100644 --- a/stdlib/REPL/src/REPLCompletions.jl +++ b/stdlib/REPL/src/REPLCompletions.jl @@ -19,6 +19,10 @@ struct KeywordCompletion <: Completion keyword::String end +struct KeyvalCompletion <: Completion + keyval::String +end + struct PathCompletion <: Completion path::String end @@ -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 @@ -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, @@ -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 @@ -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. diff --git a/stdlib/REPL/test/replcompletions.jl b/stdlib/REPL/test/replcompletions.jl index 4c777a78cf593..501193b15e2f4 100644 --- a/stdlib/REPL/test/replcompletions.jl +++ b/stdlib/REPL/test/replcompletions.jl @@ -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