Skip to content

Commit

Permalink
Merge pull request #14209 from mariushoch/replcompletions-path-unique
Browse files Browse the repository at this point in the history
Make sure commands suggested in REPL shell mode are unique
  • Loading branch information
jakebolewski committed Dec 17, 2015
2 parents 8cb7202 + f3f0da9 commit bedd4db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
10 changes: 5 additions & 5 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ function complete_path(path::AbstractString, pos; use_envpath=false)
return UTF8String[], 0:-1, false
end

matches = UTF8String[]
matches = Dict{UTF8String,Bool}()
for file in files
if startswith(file, prefix)
id = try isdir(joinpath(dir, file)) catch; false end
# joinpath is not used because windows needs to complete with double-backslash
push!(matches, id ? file * (@windows? "\\\\" : "/") : file)
matches[id ? file * (@windows? "\\\\" : "/") : file] = true
end
end

Expand Down Expand Up @@ -178,18 +178,18 @@ function complete_path(path::AbstractString, pos; use_envpath=false)
# In a perfect world, we would filter on whether the file is executable
# here, or even on whether the current user can execute the file in question.
if startswith(file, prefix) && isfile(joinpath(pathdir, file))
push!(matches, file)
matches[file] = true
end
end
end
end

matches = UTF8String[replace(s, r"\s", "\\ ") for s in matches]
matchList = UTF8String[replace(s, r"\s", "\\ ") for s in keys(matches)]
startpos = pos - endof(prefix) + 1 - length(matchall(r" ", prefix))
# The pos - endof(prefix) + 1 is correct due to `endof(prefix)-endof(prefix)==0`,
# hence we need to add one to get the first index. This is also correct when considering
# pos, because pos is the `endof` a larger string which `endswith(path)==true`.
return matches, startpos:pos, !isempty(matches)
return matchList, startpos:pos, !isempty(matchList)
end

# Determines whether method_complete should be tried. It should only be done if
Expand Down
26 changes: 25 additions & 1 deletion test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ c, r, res = test_scomplete(s)
# Pressing tab after having entered "/tmp " should not
# attempt to complete "/tmp" but rather work on the current
# working directory again.

let
file = joinpath(path, "repl completions")
s = "/tmp "
Expand Down Expand Up @@ -494,6 +493,31 @@ c, r, res = test_scomplete(s)
ENV["PATH"] = oldpath
end

# Make sure completion results are unique in case things are in the env path twice.
let
file0 = joinpath(tempdir(), "repl-completion")
dir = joinpath(tempdir(), "repl-completion-subdir")
file1 = joinpath(dir, "repl-completion")

try
# Create /tmp/repl-completion and /tmp/repl-completion-subdir/repl-completion
mkdir(dir)
touch(file0)
touch(file1)

withenv("PATH" => string(tempdir(), ":", dir)) do
s = string("repl-completio")
c,r = test_scomplete(s)
@test [utf8("repl-completion")] == c
@test s[r] == "repl-completio"
end

finally
rm(file0)
rm(file1)
rm(dir)
end
end
end

let #test that it can auto complete with spaces in file/path
Expand Down

0 comments on commit bedd4db

Please sign in to comment.