@@ -391,16 +391,21 @@ function complete_line(s::MIState)
391
391
end
392
392
end
393
393
394
+ # Old complete_line return type: Vector{String}, String, Bool
395
+ # New complete_line return type: NamedCompletion{String}, String, Bool
396
+ # OR NamedCompletion{String}, Region, Bool
397
+ #
394
398
# due to close coupling of the Pkg ReplExt `complete_line` can still return a vector of strings,
395
399
# so we convert those in this helper
396
- function complete_line_named (args... ; kwargs... ):: Tuple{Vector{NamedCompletion},String,Bool}
397
- result = complete_line (args... ; kwargs... ):: Union{Tuple{Vector{NamedCompletion},String,Bool},Tuple{Vector{String},String,Bool}}
398
- if result isa Tuple{Vector{NamedCompletion},String,Bool}
399
- return result
400
- else
401
- completions, partial, should_complete = result
402
- return map (NamedCompletion, completions), partial, should_complete
403
- end
400
+ function complete_line_named (c, s, args... ; kwargs... ):: Tuple{Vector{NamedCompletion},Region,Bool}
401
+ r1, r2, should_complete = complete_line (c, s, args... ; kwargs... ):: Union {
402
+ Tuple{Vector{String}, String, Bool},
403
+ Tuple{Vector{NamedCompletion}, String, Bool},
404
+ Tuple{Vector{NamedCompletion}, Region, Bool},
405
+ }
406
+ completions = (r1 isa Vector{String} ? map (NamedCompletion, r1) : r1)
407
+ r = (r2 isa String ? (position (s)- sizeof (r2) => position (s)) : r2)
408
+ completions, r, should_complete
404
409
end
405
410
406
411
# checks for a hint and shows it if appropriate.
@@ -426,14 +431,14 @@ function check_show_hint(s::MIState)
426
431
return
427
432
end
428
433
t_completion = Threads. @spawn :default begin
429
- named_completions, partial , should_complete = nothing , nothing , nothing
434
+ named_completions, reg , should_complete = nothing , nothing , nothing
430
435
431
436
# only allow one task to generate hints at a time and check around lock
432
437
# if the user has pressed a key since the hint was requested, to skip old completions
433
438
next_key_pressed () && return
434
439
@lock s. hint_generation_lock begin
435
440
next_key_pressed () && return
436
- named_completions, partial , should_complete = try
441
+ named_completions, reg , should_complete = try
437
442
complete_line_named (st. p. complete, st, s. active_module; hint = true )
438
443
catch
439
444
lock_clear_hint ()
@@ -448,21 +453,19 @@ function check_show_hint(s::MIState)
448
453
return
449
454
end
450
455
# Don't complete for single chars, given e.g. `x` completes to `xor`
451
- if length (partial) > 1 && should_complete
456
+ if reg . second - reg . first > 1 && should_complete
452
457
singlecompletion = length (completions) == 1
453
458
p = singlecompletion ? completions[1 ] : common_prefix (completions)
454
459
if singlecompletion || p in completions # i.e. complete `@time` even though `@time_imports` etc. exists
455
- # The completion `p` and the input `partial ` may not share the same initial
460
+ # The completion `p` and the region `reg ` may not share the same initial
456
461
# characters, for instance when completing to subscripts or superscripts.
457
462
# So, in general, make sure that the hint starts at the correct position by
458
463
# incrementing its starting position by as many characters as the input.
459
- startind = 1 # index of p from which to start providing the hint
460
- maxind = ncodeunits (p)
461
- for _ in partial
462
- startind = nextind (p, startind)
463
- startind > maxind && break
464
- end
464
+ maxind = lastindex (p)
465
+ startind = sizeof (content (s, reg))
465
466
if startind ≤ maxind # completion on a complete name returns itself so check that there's something to hint
467
+ # index of p from which to start providing the hint
468
+ startind = nextind (p, startind)
466
469
hint = p[startind: end ]
467
470
next_key_pressed () && return
468
471
@lock s. line_modify_lock begin
@@ -491,25 +494,24 @@ function clear_hint(s::ModeState)
491
494
end
492
495
493
496
function complete_line (s:: PromptState , repeats:: Int , mod:: Module ; hint:: Bool = false )
494
- completions, partial , should_complete = complete_line_named (s. p. complete, s, mod; hint)
497
+ completions, reg , should_complete = complete_line_named (s. p. complete, s, mod; hint)
495
498
isempty (completions) && return false
496
499
if ! should_complete
497
500
# should_complete is false for cases where we only want to show
498
501
# a list of possible completions but not complete, e.g. foo(\t
499
502
show_completions (s, completions)
500
503
elseif length (completions) == 1
501
504
# Replace word by completion
502
- prev_pos = position (s)
503
505
push_undo (s)
504
- edit_splice! (s, (prev_pos - sizeof (partial)) => prev_pos , completions[1 ]. completion)
506
+ edit_splice! (s, reg , completions[1 ]. completion)
505
507
else
506
508
p = common_prefix (completions)
509
+ partial = content (s, reg. first => min (bufend (s), reg. first + sizeof (p)))
507
510
if ! isempty (p) && p != partial
508
511
# All possible completions share the same prefix, so we might as
509
- # well complete that
510
- prev_pos = position (s)
512
+ # well complete that.
511
513
push_undo (s)
512
- edit_splice! (s, (prev_pos - sizeof (partial)) => prev_pos , p)
514
+ edit_splice! (s, reg , p)
513
515
elseif repeats > 0
514
516
show_completions (s, completions)
515
517
end
@@ -830,12 +832,12 @@ function edit_move_right(m::MIState)
830
832
refresh_line (s)
831
833
return true
832
834
else
833
- completions, partial , should_complete = complete_line (s. p. complete, s, m. active_module)
834
- if should_complete && eof (buf) && length (completions) == 1 && length (partial) > 1
835
+ completions, reg , should_complete = complete_line (s. p. complete, s, m. active_module)
836
+ if should_complete && eof (buf) && length (completions) == 1 && reg . second - reg . first > 1
835
837
# Replace word by completion
836
838
prev_pos = position (s)
837
839
push_undo (s)
838
- edit_splice! (s, (prev_pos - sizeof (partial) ) => prev_pos, completions[1 ]. completion)
840
+ edit_splice! (s, (prev_pos - reg . second + reg . first ) => prev_pos, completions[1 ]. completion)
839
841
refresh_line (state (s))
840
842
return true
841
843
else
@@ -2255,12 +2257,12 @@ setmodifiers!(c) = nothing
2255
2257
2256
2258
# Search Mode completions
2257
2259
function complete_line (s:: SearchState , repeats, mod:: Module ; hint:: Bool = false )
2258
- completions, partial , should_complete = complete_line (s. histprompt. complete, s, mod; hint)
2260
+ completions, reg , should_complete = complete_line (s. histprompt. complete, s, mod; hint)
2259
2261
# For now only allow exact completions in search mode
2260
2262
if length (completions) == 1
2261
2263
prev_pos = position (s)
2262
2264
push_undo (s)
2263
- edit_splice! (s, (prev_pos - sizeof (partial) ) => prev_pos, completions[1 ]. completion)
2265
+ edit_splice! (s, (prev_pos - reg . second - reg . first ) => prev_pos, completions[1 ]. completion)
2264
2266
return true
2265
2267
end
2266
2268
return false
0 commit comments