Skip to content

Commit afcf015

Browse files
committed
fix string completion with cursor in the middle of text (#60055)
(cherry picked from commit 1cc404e)
1 parent 54e1e97 commit afcf015

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

stdlib/REPL/src/REPLCompletions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ function completions(string::String, pos::Int, context_module::Module=Main, shif
10471047
# "~/example.txt TAB => "/home/user/example.txt"
10481048
r, closed = find_str(cur)
10491049
if r !== nothing
1050-
s = do_string_unescape(string[r])
1050+
s = do_string_unescape(string[intersect(r, 1:pos)])
10511051
ret, success = complete_path_string(s, hint; string_escape=true,
10521052
dirsep=Sys.iswindows() ? '\\' : '/')
10531053
if length(ret) == 1 && !closed && close_path_completion(ret[1].path)

stdlib/REPL/test/replcompletions.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,40 @@ let (c, r) = test_complete("cd(\"folder_do_not_exist_77/file")
14551455
@test length(c) == 0
14561456
end
14571457

1458+
# Test path completion in the middle of a line (issue #60050)
1459+
mktempdir() do path
1460+
# Create test directory structure
1461+
foo_dir = joinpath(path, "foo_dir")
1462+
mkpath(foo_dir)
1463+
touch(joinpath(path, "foo_file.txt"))
1464+
1465+
# Completion at end of line should work
1466+
let (c, r, res) = test_complete("\"$(path)/foo")
1467+
@test res
1468+
@test length(c) == 2
1469+
@test "$(path)/foo_dir/" in c
1470+
@test "$(path)/foo_file.txt" in c
1471+
end
1472+
1473+
# Completion in middle of line should also work (regression in 1.12)
1474+
let (c, r, res) = test_complete_pos("\"$(path)/foo|/bar.toml\"")
1475+
@test res
1476+
@test length(c) == 2
1477+
@test "$(path)/foo_dir/" in c
1478+
@test "$(path)/foo_file.txt" in c
1479+
# Check that the range covers only the part before the cursor
1480+
@test findfirst("/bar", "\"$(path)/foo/bar.toml\"")[1] - 1 in r
1481+
end
1482+
1483+
# Completion in middle of function call with trailing arguments
1484+
let (c, r, res) = test_complete_pos("run_something(\"$(path)/foo|/bar.toml\"; kwarg=true)")
1485+
@test res
1486+
@test length(c) == 2
1487+
@test "$(path)/foo_dir/" in c
1488+
@test "$(path)/foo_file.txt" in c
1489+
end
1490+
end
1491+
14581492
if Sys.iswindows()
14591493
tmp = tempname()
14601494
touch(tmp)

0 commit comments

Comments
 (0)