Skip to content

Commit

Permalink
fix closing quote on completing files in a ~ path
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Oct 22, 2024
1 parent 08d11d0 commit 73b16a2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
18 changes: 7 additions & 11 deletions stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -935,17 +935,11 @@ function get_import_mode(s::String)
return nothing
end

function close_path_completion(dir, paths, str, pos)
length(paths) == 1 || return false # Only close if there's a single choice...
path = (paths[1]::PathCompletion).path
function close_path_completion(dir, path, str, pos)
path = unescape_string(replace(path, "\\\$"=>"\$"))
path = joinpath(dir, path)
# ...except if it's a directory...
try
isdir(path)
catch e
e isa Base.IOError || rethrow() # `path` cannot be determined to be a file
end && return false
Base.isaccessibledir(path) && return false
# ...and except if there's already a " at the cursor.
return lastindex(str) <= pos || str[nextind(str, pos)] != '"'
end
Expand Down Expand Up @@ -1356,10 +1350,12 @@ function completions(string::String, pos::Int, context_module::Module=Main, shif
if !isnothing(path)
paths, dir, success = complete_path(path::String, string_escape=true)

if close_path_completion(dir, paths, path, pos)
p = (paths[1]::PathCompletion).path * "\""
if length(paths) == 1
p = (paths[1]::PathCompletion).path
hint && was_expanded && (p = contractuser(p))
paths[1] = PathCompletion(p)
if close_path_completion(dir, p, path, pos)
paths[1] = PathCompletion(p * "\"")
end
end

if success && !isempty(dir)
Expand Down
27 changes: 26 additions & 1 deletion stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ let current_dir, forbidden
e isa Base.IOError && occursin("ELOOP", e.msg)
end
c, r = test_complete("\"$(escape_string(path))/selfsym")
@test c == ["selfsymlink"]
@test c == ["selfsymlink\""]
end
end

Expand Down Expand Up @@ -1357,6 +1357,31 @@ let (c, r, res) = test_complete("\"~/julia")
c, r, res = test_complete("\"foo~bar")
@test !res
end
if !Sys.iswindows()
# create a dir and file temporarily in the home directory
path = mkpath(joinpath(homedir(), "Zx6Wa0GkC0"))
touch(joinpath(path, "my_file"))
try
let (c, r, res) = test_complete("\"~/Zx6Wa0GkC")
@test res
@test c == String["Zx6Wa0GkC0/"]
end
let (c, r, res) = test_complete("\"~/Zx6Wa0GkC0")
@test res
@test c == String[homedir() * "/Zx6Wa0GkC0"]
end
let (c, r, res) = test_complete("\"~/Zx6Wa0GkC0/my_")
@test res
@test c == String["my_file\""]
end
let (c, r, res) = test_complete("\"~/Zx6Wa0GkC0/my_file")
@test res
@test c == String[homedir() * "/Zx6Wa0GkC0/my_file"]
end
finally
rm(path, recursive=true)
end
end

# Test the completion returns nothing when the folder do not exist
let (c, r) = test_complete("cd(\"folder_do_not_exist_77/file")
Expand Down

0 comments on commit 73b16a2

Please sign in to comment.