Skip to content

Commit 1cc404e

Browse files
authored
fix string completion with cursor in the middle of text (#60055)
1 parent e2f3178 commit 1cc404e

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
@@ -1458,6 +1458,40 @@ let (c, r) = test_complete("cd(\"folder_do_not_exist_77/file")
14581458
@test length(c) == 0
14591459
end
14601460

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

0 commit comments

Comments
 (0)