Skip to content

Commit 79c3bba

Browse files
KristofferCKristofferC
authored andcommitted
fix string completion with cursor in the middle of text (#60082)
(cherry picked from commit 9e75960)
1 parent 761b05f commit 79c3bba

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,45 @@ 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+
# On Windows, use backslashes; on Unix, use forward slashes
1466+
sep = Sys.iswindows() ? "\\\\" : "/"
1467+
# On Windows, completion results have escaped backslashes
1468+
path_expected = Sys.iswindows() ? replace(path, "\\" => "\\\\") : path
1469+
1470+
# Completion at end of line should work
1471+
let (c, r, res) = test_complete("\"$(path)$(sep)foo")
1472+
@test res
1473+
@test length(c) == 2
1474+
@test "$(path_expected)$(sep)foo_dir$(sep)" in c
1475+
@test "$(path_expected)$(sep)foo_file.txt" in c
1476+
end
1477+
1478+
# Completion in middle of line should also work (regression in 1.12)
1479+
let (c, r, res) = test_complete_pos("\"$(path)$(sep)foo|$(sep)bar.toml\"")
1480+
@test res
1481+
@test length(c) == 2
1482+
@test "$(path_expected)$(sep)foo_dir$(sep)" in c
1483+
@test "$(path_expected)$(sep)foo_file.txt" in c
1484+
# Check that the range covers only the part before the cursor
1485+
@test findfirst("$(sep)bar", "\"$(path)$(sep)foo$(sep)bar.toml\"")[1] - 1 in r
1486+
end
1487+
1488+
# Completion in middle of function call with trailing arguments
1489+
let (c, r, res) = test_complete_pos("run_something(\"$(path)$(sep)foo|$(sep)bar.toml\"; kwarg=true)")
1490+
@test res
1491+
@test length(c) == 2
1492+
@test "$(path_expected)$(sep)foo_dir$(sep)" in c
1493+
@test "$(path_expected)$(sep)foo_file.txt" in c
1494+
end
1495+
end
1496+
14581497
if Sys.iswindows()
14591498
tmp = tempname()
14601499
touch(tmp)

0 commit comments

Comments
 (0)