• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37635

29 Sep 2023 07:31AM UTC coverage: 87.418% (+0.01%) from 87.408%
#37635

push

local

web-flow
More tests for replace with AbstractDict (#50902)

Co-authored-by: Rafael Fourquet <fourquet.rafael@gmail.com>

2 of 2 new or added lines in 1 file covered. (100.0%)

73865 of 84496 relevant lines covered (87.42%)

11845431.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.1
/stdlib/REPL/src/REPLCompletions.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
module REPLCompletions
4

5
export completions, shell_completions, bslash_completions, completion_text
6

7
using Core: CodeInfo, MethodInstance, CodeInstance, Const
8
const CC = Core.Compiler
9
using Base.Meta
10
using Base: propertynames, something
11

12
abstract type Completion end
13

14
struct TextCompletion <: Completion
15
    text::String
4✔
16
end
17

18
struct KeywordCompletion <: Completion
19
    keyword::String
158✔
20
end
21

22
struct KeyvalCompletion <: Completion
23
    keyval::String
15✔
24
end
25

26
struct PathCompletion <: Completion
27
    path::String
7,121✔
28
end
29

30
struct ModuleCompletion <: Completion
31
    parent::Module
379,183✔
32
    mod::String
33
end
34

35
struct PackageCompletion <: Completion
36
    package::String
195✔
37
end
38

39
struct PropertyCompletion <: Completion
40
    value
40✔
41
    property::Symbol
42
end
43

44
struct FieldCompletion <: Completion
45
    typ::DataType
25✔
46
    field::Symbol
47
end
48

49
struct MethodCompletion <: Completion
50
    tt # may be used by an external consumer to infer return type, etc.
51
    method::Method
52
    MethodCompletion(@nospecialize(tt), method::Method) = new(tt, method)
2,046✔
53
end
54

55
struct BslashCompletion <: Completion
56
    bslash::String
5,164✔
57
end
58

59
struct ShellCompletion <: Completion
60
    text::String
61
end
62

63
struct DictCompletion <: Completion
64
    dict::AbstractDict
124✔
65
    key::String
66
end
67

68
struct KeywordArgumentCompletion <: Completion
69
    kwarg::String
47✔
70
end
71

72
# interface definition
73
function Base.getproperty(c::Completion, name::Symbol)
2,898✔
74
    if name === :text
8,232,651✔
75
        return getfield(c, :text)::String
4✔
76
    elseif name === :keyword
8,230,940✔
77
        return getfield(c, :keyword)::String
1,707✔
78
    elseif name === :path
8,223,939✔
79
        return getfield(c, :path)::String
57,461✔
80
    elseif name === :parent
8,223,939✔
81
        return getfield(c, :parent)::Module
×
82
    elseif name === :mod
8,845✔
83
        return getfield(c, :mod)::String
8,215,094✔
84
    elseif name === :package
8,845✔
85
        return getfield(c, :package)::String
2,358✔
86
    elseif name === :property
6,487✔
87
        return getfield(c, :property)::Symbol
124✔
88
    elseif name === :field
6,363✔
89
        return getfield(c, :field)::Symbol
55✔
90
    elseif name === :method
5,593✔
91
        return getfield(c, :method)::Method
2,048✔
92
    elseif name === :bslash
429✔
93
        return getfield(c, :bslash)::String
5,164✔
94
    elseif name === :text
429✔
95
        return getfield(c, :text)::String
×
96
    elseif name === :key
429✔
97
        return getfield(c, :key)::String
124✔
98
    elseif name === :kwarg
305✔
99
        return getfield(c, :kwarg)::String
222✔
100
    end
101
    return getfield(c, name)
83✔
102
end
103

104
_completion_text(c::TextCompletion) = c.text
4✔
105
_completion_text(c::KeywordCompletion) = c.keyword
1,707✔
106
_completion_text(c::KeyvalCompletion) = c.keyval
83✔
107
_completion_text(c::PathCompletion) = c.path
6,980✔
108
_completion_text(c::ModuleCompletion) = c.mod
8,215,094✔
109
_completion_text(c::PackageCompletion) = c.package
2,358✔
110
_completion_text(c::PropertyCompletion) = sprint(Base.show_sym, c.property)
124✔
111
_completion_text(c::FieldCompletion) = sprint(Base.show_sym, c.field)
55✔
112
_completion_text(c::MethodCompletion) = repr(c.method)
704✔
113
_completion_text(c::BslashCompletion) = c.bslash
5,164✔
114
_completion_text(c::ShellCompletion) = c.text
×
115
_completion_text(c::DictCompletion) = c.key
124✔
116
_completion_text(c::KeywordArgumentCompletion) = c.kwarg*'='
222✔
117

118
completion_text(c) = _completion_text(c)::String
8,232,619✔
119

120
const Completions = Tuple{Vector{Completion}, UnitRange{Int}, Bool}
121

122
function completes_global(x, name)
1,868,091✔
123
    return startswith(x, name) && !('#' in x)
1,868,091✔
124
end
125

126
function appendmacro!(syms, macros, needle, endchar)
20,916✔
127
    for macsym in macros
39,594✔
128
        s = String(macsym)
51,454✔
129
        if endswith(s, needle)
51,454✔
130
            from = nextind(s, firstindex(s))
3,446✔
131
            to = prevind(s, sizeof(s)-sizeof(needle)+1)
3,446✔
132
            push!(syms, s[from:to]*endchar)
6,892✔
133
        end
134
    end
51,454✔
135
end
136

137
function filtered_mod_names(ffunc::Function, mod::Module, name::AbstractString, all::Bool = false, imported::Bool = false)
10,458✔
138
    ssyms = names(mod, all = all, imported = imported)
19,335✔
139
    all || filter!(Base.Fix1(Base.isexported, mod), ssyms)
19,335✔
140
    filter!(ffunc, ssyms)
10,458✔
141
    macros = filter(x -> startswith(String(x), "@" * name), ssyms)
1,878,549✔
142
    syms = String[sprint((io,s)->Base.show_sym(io, s; allow_macroname=true), s) for s in ssyms if completes_global(String(s), name)]
386,195✔
143
    appendmacro!(syms, macros, "_str", "\"")
10,458✔
144
    appendmacro!(syms, macros, "_cmd", "`")
10,458✔
145
    return [ModuleCompletion(mod, sym) for sym in syms]
10,458✔
146
end
147

148
# REPL Symbol Completions
149
function complete_symbol(@nospecialize(ex), name::String, @nospecialize(ffunc), context_module::Module=Main)
1,643✔
150
    mod = context_module
×
151

152
    lookup_module = true
×
153
    t = Union{}
×
154
    val = nothing
×
155
    if ex !== nothing
1,643✔
156
        res = repl_eval_ex(ex, context_module)
661✔
157
        res === nothing && return Completion[]
661✔
158
        if res isa Const
643✔
159
            val = res.val
627✔
160
            if isa(val, Module)
627✔
161
                mod = val
599✔
162
                lookup_module = true
599✔
163
            else
164
                lookup_module = false
×
165
                t = typeof(val)
655✔
166
            end
167
        else
168
            lookup_module = false
×
169
            t = CC.widenconst(res)
16✔
170
        end
171
    end
172

173
    suggestions = Completion[]
1,625✔
174
    if lookup_module
1,625✔
175
        # We will exclude the results that the user does not want, as well
176
        # as excluding Main.Main.Main, etc., because that's most likely not what
177
        # the user wants
178
        p = let mod=mod, modname=nameof(mod)
1,581✔
179
            (s::Symbol) -> !Base.isdeprecated(mod, s) && s != modname && ffunc(mod, s)::Bool
1,895,288✔
180
        end
181
        # Looking for a binding in a module
182
        if mod == context_module
1,581✔
183
            # Also look in modules we got through `using`
184
            mods = ccall(:jl_module_usings, Any, (Any,), context_module)::Vector
1,292✔
185
            for m in mods
1,292✔
186
                append!(suggestions, filtered_mod_names(p, m::Module, name))
11,955✔
187
            end
10,169✔
188
            append!(suggestions, filtered_mod_names(p, mod, name, true, true))
2,017✔
189
        else
190
            append!(suggestions, filtered_mod_names(p, mod, name, true, false))
1,870✔
191
        end
192
    elseif val !== nothing # looking for a property of an instance
44✔
193
        for property in propertynames(val, false)
27✔
194
            # TODO: support integer arguments (#36872)
195
            if property isa Symbol && startswith(string(property), name)
82✔
196
                push!(suggestions, PropertyCompletion(val, property))
40✔
197
            end
198
        end
42✔
199
    else
200
        # Looking for a member of a type
201
        add_field_completions!(suggestions, name, t)
17✔
202
    end
203
    return suggestions
1,625✔
204
end
205

206
function add_field_completions!(suggestions::Vector{Completion}, name::String, @nospecialize(t))
21✔
207
    if isa(t, Union)
21✔
208
        add_field_completions!(suggestions, name, t.a)
2✔
209
        add_field_completions!(suggestions, name, t.b)
2✔
210
    elseif t isa DataType && t != Any
19✔
211
        # Check for cases like Type{typeof(+)}
212
        if Base.isType(t)
16✔
213
            t = typeof(t.parameters[1])
×
214
        end
215
        # Only look for fields if this is a concrete type
216
        if isconcretetype(t)
16✔
217
            fields = fieldnames(t)
16✔
218
            for field in fields
16✔
219
                isa(field, Symbol) || continue # Tuple type has ::Int field name
29✔
220
                s = string(field)
29✔
221
                if startswith(s, name)
58✔
222
                    push!(suggestions, FieldCompletion(t, field))
25✔
223
                end
224
            end
29✔
225
        end
226
    end
227
end
228

229
function complete_from_list(T::Type, list::Vector{String}, s::Union{String,SubString{String}})
1,255✔
230
    r = searchsorted(list, s)
1,255✔
231
    i = first(r)
1,255✔
232
    n = length(list)
1,255✔
233
    while i <= n && startswith(list[i],s)
2,604✔
234
        r = first(r):i
173✔
235
        i += 1
173✔
236
    end
173✔
237
    Completion[T(kw) for kw in list[r]]
1,255✔
238
end
239

240
const sorted_keywords = [
241
    "abstract type", "baremodule", "begin", "break", "catch", "ccall",
242
    "const", "continue", "do", "else", "elseif", "end", "export",
243
    "finally", "for", "function", "global", "if", "import",
244
    "let", "local", "macro", "module", "mutable struct",
245
    "primitive type", "quote", "return", "struct",
246
    "try", "using", "while"]
247

248
complete_keyword(s::Union{String,SubString{String}}) = complete_from_list(KeywordCompletion, sorted_keywords, s)
543✔
249

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

252
complete_keyval(s::Union{String,SubString{String}}) = complete_from_list(KeyvalCompletion, sorted_keyvals, s)
712✔
253

254
function complete_path(path::AbstractString, pos::Int;
1,136✔
255
                       use_envpath=false, shell_escape=false,
256
                       string_escape=false)
257
    @assert !(shell_escape && string_escape)
568✔
258
    if Base.Sys.isunix() && occursin(r"^~(?:/|$)", path)
568✔
259
        # if the path is just "~", don't consider the expanded username as a prefix
260
        if path == "~"
2✔
261
            dir, prefix = homedir(), ""
2✔
262
        else
263
            dir, prefix = splitdir(homedir() * path[2:end])
2✔
264
        end
265
    else
266
        dir, prefix = splitdir(path)
566✔
267
    end
268
    local files
×
269
    try
568✔
270
        if isempty(dir)
568✔
271
            files = readdir()
302✔
272
        elseif isdir(dir)
266✔
273
            files = readdir(dir)
107✔
274
        else
275
            return Completion[], 0:-1, false
568✔
276
        end
277
    catch
278
        return Completion[], 0:-1, false
×
279
    end
280

281
    matches = Set{String}()
409✔
282
    for file in files
414✔
283
        if startswith(file, prefix)
79,670✔
284
            p = joinpath(dir, file)
6,760✔
285
            is_dir = try isdir(p) catch; false end
20,282✔
286
            push!(matches, is_dir ? joinpath(file, "") : file)
6,760✔
287
        end
288
    end
43,976✔
289

290
    if use_envpath && length(dir) == 0
409✔
291
        # Look for files in PATH as well
292
        local pathdirs = split(ENV["PATH"], @static Sys.iswindows() ? ";" : ":")
18✔
293

294
        for pathdir in pathdirs
18✔
295
            local actualpath
×
296
            try
228✔
297
                actualpath = realpath(pathdir)
260✔
298
            catch
299
                # Bash doesn't expect every folder in PATH to exist, so neither shall we
300
                continue
32✔
301
            end
302

303
            if actualpath != pathdir && in(actualpath,pathdirs)
244✔
304
                # Remove paths which (after resolving links) are in the env path twice.
305
                # Many distros eg. point /bin to /usr/bin but have both in the env path.
306
                continue
48✔
307
            end
308

309
            local filesinpath
×
310
            try
148✔
311
                filesinpath = readdir(pathdir)
149✔
312
            catch e
313
                # Bash allows dirs in PATH that can't be read, so we should as well.
314
                if isa(e, Base.IOError) || isa(e, Base.ArgumentError)
1✔
315
                    continue
1✔
316
                else
317
                    # We only handle IOError and ArgumentError here
318
                    rethrow()
×
319
                end
320
            end
321

322
            for file in filesinpath
195✔
323
                # In a perfect world, we would filter on whether the file is executable
324
                # here, or even on whether the current user can execute the file in question.
325
                if startswith(file, prefix) && isfile(joinpath(pathdir, file))
63,204✔
326
                    push!(matches, file)
589✔
327
                end
328
            end
34,639✔
329
        end
246✔
330
    end
331

332
    function do_escape(s)
409✔
333
        return shell_escape ? replace(s, r"(\s|\\)" => s"\\\0") :
10,777✔
334
               string_escape ? escape_string(s, ('\"','$')) :
335
               s
336
    end
337

338
    matchList = Completion[PathCompletion(do_escape(s)) for s in matches]
7,387✔
339
    startpos = pos - lastindex(do_escape(prefix)) + 1
543✔
340
    # The pos - lastindex(prefix) + 1 is correct due to `lastindex(prefix)-lastindex(prefix)==0`,
341
    # hence we need to add one to get the first index. This is also correct when considering
342
    # pos, because pos is the `lastindex` a larger string which `endswith(path)==true`.
343
    return matchList, startpos:pos, !isempty(matchList)
409✔
344
end
345

346
function complete_expanduser(path::AbstractString, r)
138✔
347
    expanded =
138✔
348
        try expanduser(path)
139✔
349
        catch e
350
            e isa ArgumentError || rethrow()
1✔
351
            path
139✔
352
        end
353
    return Completion[PathCompletion(expanded)], r, path != expanded
138✔
354
end
355

356
# Returns a range that includes the method name in front of the first non
357
# closed start brace from the end of the string.
358
function find_start_brace(s::AbstractString; c_start='(', c_end=')')
5,292✔
359
    r = reverse(s)
2,646✔
360
    i = firstindex(r)
×
361
    braces = in_comment = 0
×
362
    in_single_quotes = in_double_quotes = in_back_ticks = false
×
363
    while i <= ncodeunits(r)
61,356✔
364
        c, i = iterate(r, i)
118,744✔
365
        if c == '#' && i <= ncodeunits(r) && iterate(r, i)[1] == '='
59,372✔
366
            c, i = iterate(r, i) # consume '='
8✔
367
            new_comments = 1
×
368
            # handle #=#=#=#, by counting =# pairs
369
            while i <= ncodeunits(r) && iterate(r, i)[1] == '#'
6✔
370
                c, i = iterate(r, i) # consume '#'
6✔
371
                iterate(r, i)[1] == '=' || break
3✔
372
                c, i = iterate(r, i) # consume '='
4✔
373
                new_comments += 1
2✔
374
            end
2✔
375
            if c == '='
4✔
376
                in_comment += new_comments
3✔
377
            else
378
                in_comment -= new_comments
5✔
379
            end
380
        elseif !in_single_quotes && !in_double_quotes && !in_back_ticks && in_comment == 0
59,368✔
381
            if c == c_start
57,633✔
382
                braces += 1
911✔
383
            elseif c == c_end
56,722✔
384
                braces -= 1
249✔
385
            elseif c == '\''
56,473✔
386
                in_single_quotes = true
15✔
387
            elseif c == '"'
56,458✔
388
                in_double_quotes = true
271✔
389
            elseif c == '`'
56,187✔
390
                in_back_ticks = true
57,633✔
391
            end
392
        else
393
            if in_single_quotes &&
1,735✔
394
                c == '\'' && i <= ncodeunits(r) && iterate(r, i)[1] != '\\'
395
                in_single_quotes = false
15✔
396
            elseif in_double_quotes &&
1,720✔
397
                c == '"' && i <= ncodeunits(r) && iterate(r, i)[1] != '\\'
398
                in_double_quotes = false
228✔
399
            elseif in_back_ticks &&
1,492✔
400
                c == '`' && i <= ncodeunits(r) && iterate(r, i)[1] != '\\'
401
                in_back_ticks = false
7✔
402
            elseif in_comment > 0 &&
1,485✔
403
                c == '=' && i <= ncodeunits(r) && iterate(r, i)[1] == '#'
404
                # handle =#=#=#=, by counting #= pairs
405
                c, i = iterate(r, i) # consume '#'
6✔
406
                old_comments = 1
×
407
                while i <= ncodeunits(r) && iterate(r, i)[1] == '='
4✔
408
                    c, i = iterate(r, i) # consume '='
4✔
409
                    iterate(r, i)[1] == '#' || break
2✔
410
                    c, i = iterate(r, i) # consume '#'
2✔
411
                    old_comments += 1
1✔
412
                end
1✔
413
                if c == '#'
3✔
414
                    in_comment -= old_comments
2✔
415
                else
416
                    in_comment += old_comments
1✔
417
                end
418
            end
419
        end
420
        braces == 1 && break
59,372✔
421
    end
58,710✔
422
    braces != 1 && return 0:-1, -1
2,646✔
423
    method_name_end = reverseind(s, i)
662✔
424
    startind = nextind(s, something(findprev(in(non_identifier_chars), s, method_name_end), 0))::Int
1,025✔
425
    return (startind:lastindex(s), method_name_end)
662✔
426
end
427

428
struct REPLInterpreterCache
429
    dict::IdDict{MethodInstance,CodeInstance}
×
430
end
431
REPLInterpreterCache() = REPLInterpreterCache(IdDict{MethodInstance,CodeInstance}())
×
432
const REPL_INTERPRETER_CACHE = REPLInterpreterCache()
433

434
function get_code_cache()
×
435
    # XXX Avoid storing analysis results into the cache that persists across precompilation,
436
    #     as [sys|pkg]image currently doesn't support serializing externally created `CodeInstance`.
437
    #     Otherwise, `CodeInstance`s created by `REPLInterpreter`, that are much less optimized
438
    #     that those produced by `NativeInterpreter`, will leak into the native code cache,
439
    #     potentially causing runtime slowdown.
440
    #     (see https://github.com/JuliaLang/julia/issues/48453).
441
    if Base.generating_output()
481✔
442
        return REPLInterpreterCache()
×
443
    else
444
        return REPL_INTERPRETER_CACHE
481✔
445
    end
446
end
447

448
struct REPLInterpreter <: CC.AbstractInterpreter
449
    repl_frame::CC.InferenceResult
450
    world::UInt
451
    inf_params::CC.InferenceParams
452
    opt_params::CC.OptimizationParams
453
    inf_cache::Vector{CC.InferenceResult}
454
    code_cache::REPLInterpreterCache
455
    function REPLInterpreter(repl_frame::CC.InferenceResult;
962✔
456
                             world::UInt = Base.get_world_counter(),
457
                             inf_params::CC.InferenceParams = CC.InferenceParams(;
458
                                unoptimize_throw_blocks=false),
459
                             opt_params::CC.OptimizationParams = CC.OptimizationParams(),
460
                             inf_cache::Vector{CC.InferenceResult} = CC.InferenceResult[],
461
                             code_cache::REPLInterpreterCache = get_code_cache())
462
        return new(repl_frame, world, inf_params, opt_params, inf_cache, code_cache)
481✔
463
    end
464
end
465
CC.InferenceParams(interp::REPLInterpreter) = interp.inf_params
126,970✔
466
CC.OptimizationParams(interp::REPLInterpreter) = interp.opt_params
×
467
CC.get_world_counter(interp::REPLInterpreter) = interp.world
34,996✔
468
CC.get_inference_cache(interp::REPLInterpreter) = interp.inf_cache
11,762✔
469
CC.code_cache(interp::REPLInterpreter) = CC.WorldView(interp.code_cache, CC.WorldRange(interp.world))
31,262✔
470
CC.get(wvc::CC.WorldView{REPLInterpreterCache}, mi::MethodInstance, default) = get(wvc.cache.dict, mi, default)
43,149✔
471
CC.getindex(wvc::CC.WorldView{REPLInterpreterCache}, mi::MethodInstance) = getindex(wvc.cache.dict, mi)
×
472
CC.haskey(wvc::CC.WorldView{REPLInterpreterCache}, mi::MethodInstance) = haskey(wvc.cache.dict, mi)
3,852✔
473
CC.setindex!(wvc::CC.WorldView{REPLInterpreterCache}, ci::CodeInstance, mi::MethodInstance) = setindex!(wvc.cache.dict, ci, mi)
3,852✔
474

475
# REPLInterpreter is only used for type analysis, so it should disable optimization entirely
476
CC.may_optimize(::REPLInterpreter) = false
×
477

478
# REPLInterpreter analyzes a top-level frame, so better to not bail out from it
479
CC.bail_out_toplevel_call(::REPLInterpreter, ::CC.InferenceLoopState, ::CC.InferenceState) = false
×
480

481
# `REPLInterpreter` aggressively resolves global bindings to enable reasonable completions
482
# for lines like `Mod.a.|` (where `|` is the cursor position).
483
# Aggressive binding resolution poses challenges for the inference cache validation
484
# (until https://github.com/JuliaLang/julia/issues/40399 is implemented).
485
# To avoid the cache validation issues, `REPLInterpreter` only allows aggressive binding
486
# resolution for top-level frame representing REPL input code (`repl_frame`) and for child
487
# `getproperty` frames that are constant propagated from the `repl_frame`. This works, since
488
# a.) these frames are never cached, and
489
# b.) their results are only observed by the non-cached `repl_frame`.
490
#
491
# `REPLInterpreter` also aggressively concrete evaluate `:inconsistent` calls within
492
# `repl_frame` to provide reasonable completions for lines like `Ref(Some(42))[].|`.
493
# Aggressive concrete evaluation allows us to get accurate type information about complex
494
# expressions that otherwise can not be constant folded, in a safe way, i.e. it still
495
# doesn't evaluate effectful expressions like `pop!(xs)`.
496
# Similarly to the aggressive binding resolution, aggressive concrete evaluation doesn't
497
# present any cache validation issues because `repl_frame` is never cached.
498

499
is_repl_frame(interp::REPLInterpreter, sv::CC.InferenceState) = interp.repl_frame === sv.result
59,556✔
500

501
# aggressive global binding resolution within `repl_frame`
502
function CC.abstract_eval_globalref(interp::REPLInterpreter, g::GlobalRef,
×
503
                                    sv::CC.InferenceState)
504
    if is_repl_frame(interp, sv)
38,032✔
505
        if CC.isdefined_globalref(g)
637✔
506
            return Const(ccall(:jl_get_globalref_value, Any, (Any,), g))
632✔
507
        end
508
        return Union{}
5✔
509
    end
510
    return @invoke CC.abstract_eval_globalref(interp::CC.AbstractInterpreter, g::GlobalRef,
37,395✔
511
                                              sv::CC.InferenceState)
512
end
513

514
function is_repl_frame_getproperty(interp::REPLInterpreter, sv::CC.InferenceState)
67✔
515
    def = sv.linfo.def
67✔
516
    def isa Method || return false
67✔
517
    def.name === :getproperty || return false
67✔
518
    sv.cached && return false
67✔
519
    return is_repl_frame(interp, sv.parent)
66✔
520
end
521

522
# aggressive global binding resolution for `getproperty(::Module, ::Symbol)` calls within `repl_frame`
523
function CC.builtin_tfunction(interp::REPLInterpreter, @nospecialize(f),
11,798✔
524
                              argtypes::Vector{Any}, sv::CC.InferenceState)
525
    if f === Core.getglobal && is_repl_frame_getproperty(interp, sv)
11,798✔
526
        if length(argtypes) == 2
43✔
527
            a1, a2 = argtypes
43✔
528
            if isa(a1, Const) && isa(a2, Const)
43✔
529
                a1val, a2val = a1.val, a2.val
43✔
530
                if isa(a1val, Module) && isa(a2val, Symbol)
43✔
531
                    g = GlobalRef(a1val, a2val)
43✔
532
                    if CC.isdefined_globalref(g)
43✔
533
                        return Const(ccall(:jl_get_globalref_value, Any, (Any,), g))
43✔
534
                    end
535
                    return Union{}
×
536
                end
537
            end
538
        end
539
    end
540
    return @invoke CC.builtin_tfunction(interp::CC.AbstractInterpreter, f::Any,
11,755✔
541
                                        argtypes::Vector{Any}, sv::CC.InferenceState)
542
end
543

544
# aggressive concrete evaluation for `:inconsistent` frames within `repl_frame`
545
function CC.concrete_eval_eligible(interp::REPLInterpreter, @nospecialize(f),
×
546
                                   result::CC.MethodCallResult, arginfo::CC.ArgInfo,
547
                                   sv::CC.InferenceState)
548
    if is_repl_frame(interp, sv)
21,458✔
549
        neweffects = CC.Effects(result.effects; consistent=CC.ALWAYS_TRUE)
161✔
550
        result = CC.MethodCallResult(result.rt, result.edgecycle, result.edgelimited,
322✔
551
                                     result.edge, neweffects)
552
    end
553
    return @invoke CC.concrete_eval_eligible(interp::CC.AbstractInterpreter, f::Any,
21,458✔
554
                                             result::CC.MethodCallResult, arginfo::CC.ArgInfo,
555
                                             sv::CC.InferenceState)
556
end
557

558
function resolve_toplevel_symbols!(src::Core.CodeInfo, mod::Module)
×
559
    @ccall jl_resolve_globals_in_ir(
481✔
560
        #=jl_array_t *stmts=# src.code::Any,
561
        #=jl_module_t *m=# mod::Any,
562
        #=jl_svec_t *sparam_vals=# Core.svec()::Any,
563
        #=int binding_effects=# 0::Int)::Cvoid
564
    return src
×
565
end
566

567
# lower `ex` and run type inference on the resulting top-level expression
568
function repl_eval_ex(@nospecialize(ex), context_module::Module)
1,286✔
569
    if isexpr(ex, :toplevel) || isexpr(ex, :tuple)
2,571✔
570
        # get the inference result for the last expression
571
        ex = ex.args[end]
3✔
572
    end
573
    lwr = try
1,286✔
574
        Meta.lower(context_module, ex)
1,299✔
575
    catch # macro expansion failed, etc.
576
        return nothing
13✔
577
    end
578
    if lwr isa Symbol
1,273✔
579
        return isdefined(context_module, lwr) ? Const(getfield(context_module, lwr)) : nothing
670✔
580
    end
581
    lwr isa Expr || return Const(lwr) # `ex` is literal
695✔
582
    isexpr(lwr, :thunk) || return nothing # lowered to `Expr(:error, ...)` or similar
541✔
583
    src = lwr.args[1]::Core.CodeInfo
481✔
584

585
    # construct top-level `MethodInstance`
586
    mi = ccall(:jl_new_method_instance_uninit, Ref{Core.MethodInstance}, ());
481✔
587
    mi.specTypes = Tuple{}
481✔
588

589
    mi.def = context_module
481✔
590
    resolve_toplevel_symbols!(src, context_module)
481✔
591
    @atomic mi.uninferred = src
481✔
592

593
    result = CC.InferenceResult(mi)
481✔
594
    interp = REPLInterpreter(result)
962✔
595
    frame = CC.InferenceState(result, src, #=cache=#:no, interp)::CC.InferenceState
481✔
596

597
    # NOTE Use the fixed world here to make `REPLInterpreter` robust against
598
    #      potential invalidations of `Core.Compiler` methods.
599
    Base.invoke_in_world(COMPLETION_WORLD[], CC.typeinf, interp, frame)
481✔
600

601
    result = frame.result.result
481✔
602
    result === Union{} && return nothing # for whatever reason, callers expect this as the Bottom and/or Top type instead
481✔
603
    return result
472✔
604
end
605

606
# `COMPLETION_WORLD[]` will be initialized within `__init__`
607
# (to allow us to potentially remove REPL from the sysimage in the future).
608
# Note that inference from the `code_typed` call below will use the current world age
609
# rather than `typemax(UInt)`, since `Base.invoke_in_world` uses the current world age
610
# when the given world age is higher than the current one.
611
const COMPLETION_WORLD = Ref{UInt}(typemax(UInt))
612

613
# Generate code cache for `REPLInterpreter` now:
614
# This code cache will be available at the world of `COMPLETION_WORLD`,
615
# assuming no invalidation will happen before initializing REPL.
616
# Once REPL is loaded, `REPLInterpreter` will be resilient against future invalidations.
617
code_typed(CC.typeinf, (REPLInterpreter, CC.InferenceState))
618

619
# Method completion on function call expression that look like :(max(1))
620
MAX_METHOD_COMPLETIONS::Int = 40
621
function _complete_methods(ex_org::Expr, context_module::Module, shift::Bool)
310✔
622
    funct = repl_eval_ex(ex_org.args[1], context_module)
310✔
623
    funct === nothing && return 2, nothing, [], Set{Symbol}()
310✔
624
    funct = CC.widenconst(funct)
306✔
625
    args_ex, kwargs_ex, kwargs_flag = complete_methods_args(ex_org, context_module, true, true)
606✔
626
    return kwargs_flag, funct, args_ex, kwargs_ex
306✔
627
end
628

629
function complete_methods(ex_org::Expr, context_module::Module=Main, shift::Bool=false)
2✔
630
    kwargs_flag, funct, args_ex, kwargs_ex = _complete_methods(ex_org, context_module, shift)::Tuple{Int, Any, Vector{Any}, Set{Symbol}}
140✔
631
    out = Completion[]
139✔
632
    kwargs_flag == 2 && return out # one of the kwargs is invalid
139✔
633
    kwargs_flag == 0 && push!(args_ex, Vararg{Any}) # allow more arguments if there is no semicolon
127✔
634
    complete_methods!(out, funct, args_ex, kwargs_ex, shift ? -2 : MAX_METHOD_COMPLETIONS, kwargs_flag == 1)
184✔
635
    return out
127✔
636
end
637

638
MAX_ANY_METHOD_COMPLETIONS::Int = 10
639
function complete_any_methods(ex_org::Expr, callee_module::Module, context_module::Module, moreargs::Bool, shift::Bool)
13✔
640
    out = Completion[]
13✔
641
    args_ex, kwargs_ex, kwargs_flag = try
13✔
642
        # this may throw, since we set default_any to false
643
        complete_methods_args(ex_org, context_module, false, false)
13✔
644
    catch ex
645
        ex isa ArgumentError || rethrow()
×
646
        return out
13✔
647
    end
648
    kwargs_flag == 2 && return out # one of the kwargs is invalid
13✔
649

650
    # moreargs determines whether to accept more args, independently of the presence of a
651
    # semicolon for the ".?(" syntax
652
    moreargs && push!(args_ex, Vararg{Any})
13✔
653

654
    seen = Base.IdSet()
13✔
655
    for name in names(callee_module; all=true)
13✔
656
        if !Base.isdeprecated(callee_module, name) && isdefined(callee_module, name) && !startswith(string(name), '#')
1,313✔
657
            func = getfield(callee_module, name)
637✔
658
            if !isa(func, Module)
637✔
659
                funct = Core.Typeof(func)
1,157✔
660
                if !in(funct, seen)
624✔
661
                    push!(seen, funct)
598✔
662
                    complete_methods!(out, funct, args_ex, kwargs_ex, MAX_ANY_METHOD_COMPLETIONS, false)
598✔
663
                end
664
            elseif callee_module === Main && isa(func, Module)
26✔
665
                callee_module2 = func
×
666
                for name in names(callee_module2)
×
667
                    if !Base.isdeprecated(callee_module2, name) && isdefined(callee_module2, name) && !startswith(string(name), '#')
×
668
                        func = getfield(callee_module, name)
×
669
                        if !isa(func, Module)
×
670
                            funct = Core.Typeof(func)
×
671
                            if !in(funct, seen)
×
672
                                push!(seen, funct)
×
673
                                complete_methods!(out, funct, args_ex, kwargs_ex, MAX_ANY_METHOD_COMPLETIONS, false)
×
674
                            end
675
                        end
676
                    end
677
                end
×
678
            end
679
        end
680
    end
1,326✔
681

682
    if !shift
13✔
683
        # Filter out methods where all arguments are `Any`
684
        filter!(out) do c
2✔
685
            isa(c, TextCompletion) && return false
11✔
686
            isa(c, MethodCompletion) || return true
11✔
687
            sig = Base.unwrap_unionall(c.method.sig)::DataType
11✔
688
            return !all(T -> T === Any || T === Vararg{Any}, sig.parameters[2:end])
19✔
689
        end
690
    end
691

692
    return out
13✔
693
end
694

695
function detect_invalid_kwarg!(kwargs_ex::Vector{Symbol}, @nospecialize(x), kwargs_flag::Int, possible_splat::Bool)
×
696
    n = isexpr(x, :kw) ? x.args[1] : x
57✔
697
    if n isa Symbol
55✔
698
        push!(kwargs_ex, n)
41✔
699
        return kwargs_flag
41✔
700
    end
701
    possible_splat && isexpr(x, :...) && return kwargs_flag
14✔
702
    return 2 # The kwarg is invalid
10✔
703
end
704

705
function detect_args_kwargs(funargs::Vector{Any}, context_module::Module, default_any::Bool, broadcasting::Bool)
319✔
706
    args_ex = Any[]
319✔
707
    kwargs_ex = Symbol[]
319✔
708
    kwargs_flag = 0
×
709
    # kwargs_flag is:
710
    # * 0 if there is no semicolon and no invalid kwarg
711
    # * 1 if there is a semicolon and no invalid kwarg
712
    # * 2 if there are two semicolons or more, or if some kwarg is invalid, which
713
    #        means that it is not of the form "bar=foo", "bar" or "bar..."
714
    for i in (1+!broadcasting):length(funargs)
483✔
715
        ex = funargs[i]
295✔
716
        if isexpr(ex, :parameters)
460✔
717
            kwargs_flag = ifelse(kwargs_flag == 0, 1, 2) # there should be at most one :parameters
59✔
718
            for x in ex.args
89✔
719
                kwargs_flag = detect_invalid_kwarg!(kwargs_ex, x, kwargs_flag, true)
46✔
720
            end
62✔
721
        elseif isexpr(ex, :kw)
401✔
722
            kwargs_flag = detect_invalid_kwarg!(kwargs_ex, ex, kwargs_flag, false)
22✔
723
        else
724
            if broadcasting
214✔
725
                # handle broadcasting, but only handle number of arguments instead of
726
                # argument types
727
                push!(args_ex, Any)
5✔
728
            else
729
                argt = repl_eval_ex(ex, context_module)
209✔
730
                if argt !== nothing
209✔
731
                    push!(args_ex, CC.widenconst(argt))
183✔
732
                elseif default_any
26✔
733
                    push!(args_ex, Any)
26✔
734
                else
735
                    throw(ArgumentError("argument not found"))
×
736
                end
737
            end
738
        end
739
    end
426✔
740
    return args_ex, Set{Symbol}(kwargs_ex), kwargs_flag
319✔
741
end
742

743
is_broadcasting_expr(ex::Expr) = ex.head === :. && isexpr(ex.args[2], :tuple)
1,781✔
744

745
function complete_methods_args(ex::Expr, context_module::Module, default_any::Bool, allow_broadcasting::Bool)
×
746
    if allow_broadcasting && is_broadcasting_expr(ex)
306✔
747
        return detect_args_kwargs((ex.args[2]::Expr).args, context_module, default_any, true)
6✔
748
    end
749
    return detect_args_kwargs(ex.args, context_module, default_any, false)
313✔
750
end
751

752
function complete_methods!(out::Vector{Completion}, @nospecialize(funct), args_ex::Vector{Any}, kwargs_ex::Set{Symbol}, max_method_completions::Int, exact_nargs::Bool)
894✔
753
    # Input types and number of arguments
754
    t_in = Tuple{funct, args_ex...}
894✔
755
    m = Base._methods_by_ftype(t_in, nothing, max_method_completions, Base.get_world_counter(),
894✔
756
        #=ambig=# true, Ref(typemin(UInt)), Ref(typemax(UInt)), Ptr{Int32}(C_NULL))
757
    if !isa(m, Vector)
894✔
758
        push!(out, TextCompletion(sprint(Base.show_signature_function, funct) * "( too many methods, use SHIFT-TAB to show )"))
4✔
759
        return
4✔
760
    end
761
    for match in m
1,354✔
762
        # TODO: if kwargs_ex, filter out methods without kwargs?
763
        push!(out, MethodCompletion(match.spec_types, match.method))
2,046✔
764
    end
2,046✔
765
    # TODO: filter out methods with wrong number of arguments if `exact_nargs` is set
766
end
767

768
include("latex_symbols.jl")
769
include("emoji_symbols.jl")
770

771
const non_identifier_chars = [" \t\n\r\"\\'`\$><=:;|&{}()[],+-*/?%^~"...]
772
const whitespace_chars = [" \t\n\r"...]
773
# "\"'`"... is added to whitespace_chars as non of the bslash_completions
774
# characters contain any of these characters. It prohibits the
775
# bslash_completions function to try and complete on escaped characters in strings
776
const bslash_separators = [whitespace_chars..., "\"'`"...]
777

778
const subscripts = Dict(k[3]=>v[1] for (k,v) in latex_symbols if startswith(k, "\\_") && length(k)==3)
779
const subscript_regex = Regex("^\\\\_[" * join(isdigit(k) || isletter(k) ? "$k" : "\\$k" for k in keys(subscripts)) * "]+\\z")
780
const superscripts = Dict(k[3]=>v[1] for (k,v) in latex_symbols if startswith(k, "\\^") && length(k)==3)
781
const superscript_regex = Regex("^\\\\\\^[" * join(isdigit(k) || isletter(k) ? "$k" : "\\$k" for k in keys(superscripts)) * "]+\\z")
782

783
# Aux function to detect whether we're right after a
784
# using or import keyword
785
function afterusing(string::String, startpos::Int)
1,471✔
786
    (isempty(string) || startpos == 0) && return false
1,471✔
787
    str = string[1:prevind(string,startpos)]
2,625✔
788
    isempty(str) && return false
1,468✔
789
    rstr = reverse(str)
1,157✔
790
    r = findfirst(r"\s(gnisu|tropmi)\b", rstr)
1,157✔
791
    r === nothing && return false
1,157✔
792
    fr = reverseind(str, last(r))
30✔
793
    return occursin(r"^\b(using|import)\s*((\w+[.])*\w+\s*,\s*)*$", str[fr:end])
30✔
794
end
795

796
function close_path_completion(str, startpos, r, paths, pos)
135✔
797
    length(paths) == 1 || return false  # Only close if there's a single choice...
254✔
798
    _path = str[startpos:prevind(str, first(r))] * (paths[1]::PathCompletion).path
25✔
799
    path = expanduser(unescape_string(replace(_path, "\\\$"=>"\$", "\\\""=>"\"")))
16✔
800
    # ...except if it's a directory...
801
    try
16✔
802
        isdir(path)
17✔
803
    catch e
804
        e isa Base.IOError || rethrow() # `path` cannot be determined to be a file
17✔
805
    end && return false
806
    # ...and except if there's already a " at the cursor.
807
    return lastindex(str) <= pos || str[nextind(str, pos)] != '"'
6✔
808
end
809

810
function bslash_completions(string::String, pos::Int)
1,886✔
811
    slashpos = something(findprev(isequal('\\'), string, pos), 0)
1,936✔
812
    if (something(findprev(in(bslash_separators), string, pos), 0) < slashpos &&
1,914✔
813
        !(1 < slashpos && (string[prevind(string, slashpos)]=='\\')))
814
        # latex / emoji symbol substitution
815
        s = string[slashpos:pos]
96✔
816
        latex = get(latex_symbols, s, "")
69✔
817
        if !isempty(latex) # complete an exact match
48✔
818
            return (true, (Completion[BslashCompletion(latex)], slashpos:pos, true))
21✔
819
        elseif occursin(subscript_regex, s)
27✔
820
            sub = map(c -> subscripts[c], s[3:end])
8✔
821
            return (true, (Completion[BslashCompletion(sub)], slashpos:pos, true))
1✔
822
        elseif occursin(superscript_regex, s)
26✔
823
            sup = map(c -> superscripts[c], s[3:end])
8✔
824
            return (true, (Completion[BslashCompletion(sup)], slashpos:pos, true))
1✔
825
        end
826
        emoji = get(emoji_symbols, s, "")
27✔
827
        if !isempty(emoji)
25✔
828
            return (true, (Completion[BslashCompletion(emoji)], slashpos:pos, true))
2✔
829
        end
830
        # return possible matches; these cannot be mixed with regular
831
        # Julian completions as only latex / emoji symbols contain the leading \
832
        if startswith(s, "\\:") # emoji
44✔
833
            namelist = Iterators.filter(k -> startswith(k, s), keys(emoji_symbols))
2,371✔
834
        else # latex
835
            namelist = Iterators.filter(k -> startswith(k, s), keys(latex_symbols))
104,984✔
836
        end
837
        return (true, (Completion[BslashCompletion(name) for name in sort!(collect(namelist))], slashpos:pos, true))
23✔
838
    end
839
    return (false, (Completion[], 0:-1, false))
1,838✔
840
end
841

842
function dict_identifier_key(str::String, tag::Symbol, context_module::Module=Main)
2,020✔
843
    if tag === :string
2,020✔
844
        str_close = str*"\""
158✔
845
    elseif tag === :cmd
1,861✔
846
        str_close = str*"`"
5✔
847
    else
848
        str_close = str
×
849
    end
850
    frange, end_of_identifier = find_start_brace(str_close, c_start='[', c_end=']')
2,019✔
851
    isempty(frange) && return (nothing, nothing, nothing)
2,019✔
852
    objstr = str[1:end_of_identifier]
212✔
853
    objex = Meta.parse(objstr, raise=false, depwarn=false)
106✔
854
    objt = repl_eval_ex(objex, context_module)
106✔
855
    isa(objt, Core.Const) || return (nothing, nothing, nothing)
130✔
856
    obj = objt.val
82✔
857
    isa(obj, AbstractDict) || return (nothing, nothing, nothing)
83✔
858
    length(obj)::Int < 1_000_000 || return (nothing, nothing, nothing)
81✔
859
    begin_of_key = something(findnext(!isspace, str, nextind(str, end_of_identifier) + 1), # +1 for [
156✔
860
                             lastindex(str)+1)
861
    return (obj, str[begin_of_key:end], begin_of_key)
81✔
862
end
863

864
# This needs to be a separate non-inlined function, see #19441
865
@noinline function find_dict_matches(identifier::AbstractDict, partial_key)
80✔
866
    matches = String[]
80✔
867
    for key in keys(identifier)
122✔
868
        rkey = repr(key)
933✔
869
        startswith(rkey,partial_key) && push!(matches,rkey)
1,584✔
870
    end
1,448✔
871
    return matches
80✔
872
end
873

874
# Identify an argument being completed in a method call. If the argument is empty, method
875
# suggestions will be provided instead of argument completions.
876
function identify_possible_method_completion(partial, last_idx)
2,603✔
877
    fail = 0:-1, Expr(:nothing), 0:-1, 0
2,603✔
878

879
    # First, check that the last punctuation is either ',', ';' or '('
880
    idx_last_punct = something(findprev(x -> ispunct(x) && x != '_' && x != '!', partial, last_idx), 0)::Int
18,511✔
881
    idx_last_punct == 0 && return fail
2,603✔
882
    last_punct = partial[idx_last_punct]
4,222✔
883
    last_punct == ',' || last_punct == ';' || last_punct == '(' || return fail
4,012✔
884

885
    # Then, check that `last_punct` is only followed by an identifier or nothing
886
    before_last_word_start = something(findprev(in(non_identifier_chars), partial, last_idx), 0)
1,414✔
887
    before_last_word_start == 0 && return fail
707✔
888
    all(isspace, @view partial[nextind(partial, idx_last_punct):before_last_word_start]) || return fail
787✔
889

890
    # Check that `last_punct` is either the last '(' or placed after a previous '('
891
    frange, method_name_end = find_start_brace(@view partial[1:idx_last_punct])
627✔
892
    method_name_end ∈ frange || return fail
772✔
893

894
    # Strip the preceding ! operators, if any, and close the expression with a ')'
895
    s = replace(partial[frange], r"\G\!+([^=\(]+)" => s"\1"; count=1) * ')'
964✔
896
    ex = Meta.parse(s, raise=false, depwarn=false)
482✔
897
    isa(ex, Expr) || return fail
482✔
898

899
    # `wordrange` is the position of the last argument to complete
900
    wordrange = nextind(partial, before_last_word_start):last_idx
620✔
901
    return frange, ex, wordrange, method_name_end
482✔
902
end
903

904
# Provide completion for keyword arguments in function calls
905
function complete_keyword_argument(partial, last_idx, context_module)
1,640✔
906
    frange, ex, wordrange, = identify_possible_method_completion(partial, last_idx)
1,640✔
907
    fail = Completion[], 0:-1, frange
1,640✔
908
    ex.head === :call || is_broadcasting_expr(ex) || return fail
3,112✔
909

910
    kwargs_flag, funct, args_ex, kwargs_ex = _complete_methods(ex, context_module, true)::Tuple{Int, Any, Vector{Any}, Set{Symbol}}
171✔
911
    kwargs_flag == 2 && return fail # one of the previous kwargs is invalid
171✔
912

913
    methods = Completion[]
169✔
914
    complete_methods!(methods, funct, Any[Vararg{Any}], kwargs_ex, -1, kwargs_flag == 1)
169✔
915
    # TODO: use args_ex instead of Any[Vararg{Any}] and only provide kwarg completion for
916
    # method calls compatible with the current arguments.
917

918
    # For each method corresponding to the function call, provide completion suggestions
919
    # for each keyword that starts like the last word and that is not already used
920
    # previously in the expression. The corresponding suggestion is "kwname=".
921
    # If the keyword corresponds to an existing name, also include "kwname" as a suggestion
922
    # since the syntax "foo(; kwname)" is equivalent to "foo(; kwname=kwname)".
923
    last_word = partial[wordrange] # the word to complete
338✔
924
    kwargs = Set{String}()
169✔
925
    for m in methods
169✔
926
        m::MethodCompletion
1,333✔
927
        possible_kwargs = Base.kwarg_decl(m.method)
1,333✔
928
        current_kwarg_candidates = String[]
1,333✔
929
        for _kw in possible_kwargs
2,408✔
930
            kw = String(_kw)
472✔
931
            if !endswith(kw, "...") && startswith(kw, last_word) && _kw ∉ kwargs_ex
797✔
932
                push!(current_kwarg_candidates, kw)
67✔
933
            end
934
        end
730✔
935
        union!(kwargs, current_kwarg_candidates)
1,333✔
936
    end
1,502✔
937

938
    suggestions = Completion[KeywordArgumentCompletion(kwarg) for kwarg in kwargs]
216✔
939
    append!(suggestions, complete_symbol(nothing, last_word, Returns(true), context_module))
169✔
940
    append!(suggestions, complete_keyval(last_word))
169✔
941

942
    return sort!(suggestions, by=completion_text), wordrange
169✔
943
end
944

945
function project_deps_get_completion_candidates(pkgstarts::String, project_file::String)
1✔
946
    loading_candidates = String[]
1✔
947
    d = Base.parsed_toml(project_file)
1✔
948
    pkg = get(d, "name", nothing)::Union{String, Nothing}
2✔
949
    if pkg !== nothing && startswith(pkg, pkgstarts)
2✔
950
        push!(loading_candidates, pkg)
1✔
951
    end
952
    deps = get(d, "deps", nothing)::Union{Dict{String, Any}, Nothing}
2✔
953
    if deps !== nothing
1✔
954
        for (pkg, _) in deps
2✔
955
            startswith(pkg, pkgstarts) && push!(loading_candidates, pkg)
2✔
956
        end
1✔
957
    end
958
    return Completion[PackageCompletion(name) for name in loading_candidates]
1✔
959
end
960

961
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)
1,474✔
962
    ex = nothing
3✔
963
    if comp_keywords
1,474✔
964
        append!(suggestions, complete_keyword(name))
543✔
965
        append!(suggestions, complete_keyval(name))
543✔
966
    end
967
    if dotpos > 1 && string[dotpos] == '.'
2,585✔
968
        s = string[1:dotpos-1]
1,322✔
969
        # First see if the whole string up to `pos` is a valid expression. If so, use it.
970
        ex = Meta.parse(s, raise=false, depwarn=false)
661✔
971
        if isexpr(ex, :incomplete)
706✔
972
            s = string[startpos:pos]
1,090✔
973
            # Heuristic to find the start of the expression. TODO: This would be better
974
            # done with a proper error-recovering parser.
975
            if 0 < startpos <= lastindex(string) && string[startpos] == '.'
1,090✔
976
                i = prevind(string, startpos)
1✔
977
                while 0 < i
1✔
978
                    c = string[i]
2✔
979
                    if c in (')', ']')
4✔
980
                        if c == ')'
×
981
                            c_start = '('
×
982
                            c_end = ')'
×
983
                        elseif c == ']'
×
984
                            c_start = '['
×
985
                            c_end = ']'
×
986
                        end
987
                        frange, end_of_identifier = find_start_brace(string[1:prevind(string, i)], c_start=c_start, c_end=c_end)
×
988
                        isempty(frange) && break # unbalanced parens
×
989
                        startpos = first(frange)
×
990
                        i = prevind(string, startpos)
×
991
                    elseif c in ('\'', '\"', '\`')
6✔
992
                        s = "$c$c"*string[startpos:pos]
×
993
                        break
×
994
                    else
995
                        break
×
996
                    end
997
                    s = string[startpos:pos]
×
998
                end
×
999
            end
1000
            if something(findlast(in(non_identifier_chars), s), 0) < something(findlast(isequal('.'), s), 0)
1,090✔
1001
                lookup_name, name = rsplit(s, ".", limit=2)
545✔
1002
                name = String(name)
545✔
1003

1004
                ex = Meta.parse(lookup_name, raise=false, depwarn=false)
545✔
1005
            end
1006
            isexpr(ex, :incomplete) && (ex = nothing)
873✔
1007
        end
1008
    end
1009
    append!(suggestions, complete_symbol(ex, name, ffunc, context_module))
1,474✔
1010
    return sort!(unique(suggestions), by=completion_text), (dotpos+1):pos, true
1,474✔
1011
end
1012

1013
function completions(string::String, pos::Int, context_module::Module=Main, shift::Bool=true)
2,376✔
1014
    # First parse everything up to the current position
1015
    partial = string[1:pos]
4,406✔
1016
    inc_tag = Base.incomplete_tag(Meta.parse(partial, raise=false, depwarn=false))
2,031✔
1017

1018
    # ?(x, y)TAB lists methods you can call with these objects
1019
    # ?(x, y TAB lists methods that take these objects as the first two arguments
1020
    # MyModule.?(x, y)TAB restricts the search to names in MyModule
1021
    rexm = match(r"(\w+\.|)\?\((.*)$", partial)
2,031✔
1022
    if rexm !== nothing
2,031✔
1023
        # Get the module scope
1024
        if isempty(rexm.captures[1])
26✔
1025
            callee_module = context_module
×
1026
        else
1027
            modname = Symbol(rexm.captures[1][1:end-1])
13✔
1028
            if isdefined(context_module, modname)
13✔
1029
                callee_module = getfield(context_module, modname)
13✔
1030
                if !isa(callee_module, Module)
13✔
1031
                    callee_module = context_module
13✔
1032
                end
1033
            else
1034
                callee_module = context_module
×
1035
            end
1036
        end
1037
        moreargs = !endswith(rexm.captures[2], ')')
14✔
1038
        callstr = "_(" * rexm.captures[2]
13✔
1039
        if moreargs
13✔
1040
            callstr *= ')'
7✔
1041
        end
1042
        ex_org = Meta.parse(callstr, raise=false, depwarn=false)
13✔
1043
        if isa(ex_org, Expr)
13✔
1044
            return complete_any_methods(ex_org, callee_module::Module, context_module, moreargs, shift), (0:length(rexm.captures[1])+1) .+ rexm.offset, false
13✔
1045
        end
1046
    end
1047

1048
    # if completing a key in a Dict
1049
    identifier, partial_key, loc = dict_identifier_key(partial, inc_tag, context_module)
2,098✔
1050
    if identifier !== nothing
2,018✔
1051
        matches = find_dict_matches(identifier, partial_key)
80✔
1052
        length(matches)==1 && (lastindex(string) <= pos || string[nextind(string,pos)] != ']') && (matches[1]*=']')
84✔
1053
        length(matches)>0 && return Completion[DictCompletion(identifier, match) for match in sort!(matches)], loc::Int:pos, true
80✔
1054
    end
1055

1056
    ffunc = Returns(true)
×
1057
    suggestions = Completion[]
1,954✔
1058

1059
    # Check if this is a var"" string macro that should be completed like
1060
    # an identifier rather than a string.
1061
    # TODO: It would be nice for the parser to give us more information here
1062
    # so that we can lookup the macro by identity rather than pattern matching
1063
    # its invocation.
1064
    varrange = findprev("var\"", string, pos)
1,954✔
1065

1066
    if varrange !== nothing
1,954✔
1067
        ok, ret = bslash_completions(string, pos)
3✔
1068
        ok && return ret
3✔
1069
        startpos = first(varrange) + 4
3✔
1070
        dotpos = something(findprev(isequal('.'), string, first(varrange)-1), 0)
5✔
1071
        return complete_identifiers!(Completion[], ffunc, context_module, string,
3✔
1072
            string[startpos:pos], pos, dotpos, startpos)
1073
    elseif inc_tag === :cmd
1,951✔
1074
        m = match(r"[\t\n\r\"`><=*?|]| (?!\\)", reverse(partial))
1✔
1075
        startpos = nextind(partial, reverseind(partial, m.offset))
2✔
1076
        r = startpos:pos
1✔
1077

1078
        # This expansion with "\\ "=>' ' replacement and shell_escape=true
1079
        # assumes the path isn't further quoted within the cmd backticks.
1080
        expanded = complete_expanduser(replace(string[r], r"\\ " => " "), r)
2✔
1081
        expanded[3] && return expanded  # If user expansion available, return it
1✔
1082

1083
        paths, r, success = complete_path(replace(string[r], r"\\ " => " "), pos,
2✔
1084
                                          shell_escape=true)
1085

1086
        return sort!(paths, by=p->p.path), r, success
1✔
1087
    elseif inc_tag === :string
1,950✔
1088
        # Find first non-escaped quote
1089
        m = match(r"\"(?!\\)", reverse(partial))
137✔
1090
        startpos = nextind(partial, reverseind(partial, m.offset))
274✔
1091
        r = startpos:pos
159✔
1092

1093
        expanded = complete_expanduser(string[r], r)
252✔
1094
        expanded[3] && return expanded  # If user expansion available, return it
137✔
1095

1096
        path_prefix = try
135✔
1097
            unescape_string(replace(string[r], "\\\$"=>"\$", "\\\""=>"\""))
248✔
1098
        catch
1099
            nothing
135✔
1100
        end
1101
        if !isnothing(path_prefix)
270✔
1102
            paths, r, success = complete_path(path_prefix, pos, string_escape=true)
135✔
1103

1104
            if close_path_completion(string, startpos, r, paths, pos)
135✔
1105
                paths[1] = PathCompletion((paths[1]::PathCompletion).path * "\"")
5✔
1106
            end
1107

1108
            # Fallthrough allowed so that Latex symbols can be completed in strings
1109
            success && return sort!(paths, by=p->p.path), r, success
50,595✔
1110
        end
1111
    end
1112

1113
    ok, ret = bslash_completions(string, pos)
1,878✔
1114
    ok && return ret
1,878✔
1115

1116
    # Make sure that only bslash_completions is working on strings
1117
    inc_tag === :string && return Completion[], 0:-1, false
1,835✔
1118
    if inc_tag === :other
1,779✔
1119
        frange, ex, wordrange, method_name_end = identify_possible_method_completion(partial, pos)
963✔
1120
        if last(frange) != -1 && all(isspace, @view partial[wordrange]) # no last argument to complete
963✔
1121
            if ex.head === :call
138✔
1122
                return complete_methods(ex, context_module, shift), first(frange):method_name_end, false
135✔
1123
            elseif is_broadcasting_expr(ex)
3✔
1124
                return complete_methods(ex, context_module, shift), first(frange):(method_name_end - 1), false
3✔
1125
            end
1126
        end
1127
    elseif inc_tag === :comment
816✔
1128
        return Completion[], 0:-1, false
1✔
1129
    end
1130

1131
    # Check whether we can complete a keyword argument in a function call
1132
    kwarg_completion, wordrange = complete_keyword_argument(partial, pos, context_module)
3,111✔
1133
    isempty(wordrange) || return kwarg_completion, wordrange, !isempty(kwarg_completion)
1,809✔
1134

1135
    dotpos = something(findprev(isequal('.'), string, pos), 0)
2,255✔
1136
    startpos = nextind(string, something(findprev(in(non_identifier_chars), string, pos), 0))
2,626✔
1137
    # strip preceding ! operator
1138
    if (m = match(r"\G\!+", partial, startpos)) isa RegexMatch
1,471✔
1139
        startpos += length(m.match)
2✔
1140
    end
1141

1142
    name = string[max(startpos, dotpos+1):pos]
2,571✔
1143
    comp_keywords = !isempty(name) && startpos > dotpos
1,471✔
1144
    if afterusing(string, startpos)
1,471✔
1145
        # We're right after using or import. Let's look only for packages
1146
        # and modules we can reach from here
1147

1148
        # If there's no dot, we're in toplevel, so we should
1149
        # also search for packages
1150
        s = string[startpos:pos]
33✔
1151
        if dotpos <= startpos
18✔
1152
            for dir in Base.load_path()
17✔
1153
                if basename(dir) in Base.project_names && isfile(dir)
93✔
1154
                    append!(suggestions, project_deps_get_completion_candidates(s, dir))
1✔
1155
                end
1156
                isdir(dir) || continue
37✔
1157
                for pname in readdir(dir)
18✔
1158
                    if pname[1] != '.' && pname != "METADATA" &&
1,974✔
1159
                        pname != "REQUIRE" && startswith(pname, s)
1160
                        # Valid file paths are
1161
                        #   <Mod>.jl
1162
                        #   <Mod>/src/<Mod>.jl
1163
                        #   <Mod>.jl/src/<Mod>.jl
1164
                        if isfile(joinpath(dir, pname))
194✔
1165
                            endswith(pname, ".jl") && push!(suggestions,
×
1166
                                                            PackageCompletion(pname[1:prevind(pname, end-2)]))
1167
                        else
1168
                            mod_name = if endswith(pname, ".jl")
194✔
1169
                                pname[1:prevind(pname, end-2)]
×
1170
                            else
1171
                                pname
194✔
1172
                            end
1173
                            if isfile(joinpath(dir, pname, "src",
194✔
1174
                                               "$mod_name.jl"))
1175
                                push!(suggestions, PackageCompletion(mod_name))
193✔
1176
                            end
1177
                        end
1178
                    end
1179
                end
1,023✔
1180
            end
54✔
1181
        end
1182
        ffunc = (mod,x)->(Base.isbindingresolved(mod, x) && isdefined(mod, x) && isa(getfield(mod, x), Module))
20,263✔
1183
        comp_keywords = false
×
1184
    end
1185

1186
    startpos == 0 && (pos = -1)
1,471✔
1187
    dotpos < startpos && (dotpos = startpos - 1)
1,471✔
1188
    return complete_identifiers!(suggestions, ffunc, context_module, string,
1,471✔
1189
        name, pos, dotpos, startpos, comp_keywords)
1190
end
1191

1192
function shell_completions(string, pos)
452✔
1193
    # First parse everything up to the current position
1194
    scs = string[1:pos]
904✔
1195
    local args, last_parse
×
1196
    try
452✔
1197
        args, last_parse = Base.shell_parse(scs, true)::Tuple{Expr,UnitRange{Int}}
469✔
1198
    catch
1199
        return Completion[], 0:-1, false
17✔
1200
    end
1201
    ex = args.args[end]::Expr
435✔
1202
    # Now look at the last thing we parsed
1203
    isempty(ex.args) && return Completion[], 0:-1, false
435✔
1204
    arg = ex.args[end]
435✔
1205
    if all(s -> isa(s, AbstractString), ex.args)
1,307✔
1206
        arg = arg::AbstractString
432✔
1207
        # Treat this as a path
1208

1209
        # As Base.shell_parse throws away trailing spaces (unless they are escaped),
1210
        # we need to special case here.
1211
        # If the last char was a space, but shell_parse ignored it search on "".
1212
        ignore_last_word = arg != " " && scs[end] == ' '
863✔
1213
        prefix = ignore_last_word ? "" : join(ex.args)
847✔
1214

1215
        # Also try looking into the env path if the user wants to complete the first argument
1216
        use_envpath = !ignore_last_word && length(args.args) < 2
432✔
1217

1218
        return complete_path(prefix, pos, use_envpath=use_envpath, shell_escape=true)
432✔
1219
    elseif isexpr(arg, :incomplete) || isexpr(arg, :error)
5✔
1220
        partial = scs[last_parse]
4✔
1221
        ret, range = completions(partial, lastindex(partial))
2✔
1222
        range = range .+ (first(last_parse) - 1)
2✔
1223
        return ret, range, true
2✔
1224
    end
1225
    return Completion[], 0:-1, false
1✔
1226
end
1227

1228
function UndefVarError_hint(io::IO, ex::UndefVarError)
3✔
1229
    var = ex.var
3✔
1230
    if var === :or
3✔
1231
        print(io, "\nsuggestion: Use `||` for short-circuiting boolean OR.")
×
1232
    elseif var === :and
3✔
1233
        print(io, "\nsuggestion: Use `&&` for short-circuiting boolean AND.")
×
1234
    elseif var === :help
3✔
1235
        println(io)
×
1236
        # Show friendly help message when user types help or help() and help is undefined
1237
        show(io, MIME("text/plain"), Base.Docs.parsedoc(Base.Docs.keywords[:help]))
×
1238
    elseif var === :quit
3✔
1239
        print(io, "\nsuggestion: To exit Julia, use Ctrl-D, or type exit() and press enter.")
×
1240
    end
1241
end
1242

1243
function __init__()
11✔
1244
    Base.Experimental.register_error_hint(UndefVarError_hint, UndefVarError)
11✔
1245
    COMPLETION_WORLD[] = Base.get_world_counter()
11✔
1246
    nothing
11✔
1247
end
1248

1249
end # module
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc