Skip to content

Commit

Permalink
Merge pull request JuliaLang#8047 from lucasb-eyer/master
Browse files Browse the repository at this point in the history
REPL tab don't double " (Fix JuliaLang#7318)
  • Loading branch information
Keno committed Aug 20, 2014
2 parents 3806506 + a329f78 commit 538b3d4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 46 deletions.
9 changes: 6 additions & 3 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,23 @@ bytestring_beforecursor(buf::IOBuffer) = bytestring(pointer(buf.data), buf.ptr-1

function complete_line(c::REPLCompletionProvider, s)
partial = bytestring_beforecursor(s.input_buffer)
ret, range, should_complete = completions(partial, endof(partial))
full = LineEdit.input_string(s)
ret, range, should_complete = completions(full, endof(partial))
return ret, partial[range], should_complete
end

function complete_line(c::ShellCompletionProvider, s)
# First parse everything up to the current position
partial = bytestring_beforecursor(s.input_buffer)
ret, range, should_complete = shell_completions(partial, endof(partial))
full = LineEdit.input_string(s)
ret, range, should_complete = shell_completions(full, endof(partial))
return ret, partial[range], should_complete
end

function complete_line(c::LatexCompletions, s)
partial = bytestring_beforecursor(LineEdit.buffer(s))
ret, range, should_complete = latex_completions(partial, endof(partial))[2]
full = LineEdit.input_string(s)
ret, range, should_complete = latex_completions(full, endof(partial))[2]
return ret, partial[range], should_complete
end

Expand Down
71 changes: 29 additions & 42 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,27 @@ function complete_keyword(s::ByteString)
sorted_keywords[r]
end

function complete_path(path::ByteString)
matches = ByteString[]
function complete_path(path::String, pos)
dir, prefix = splitdir(path)
if length(dir) == 0
files = readdir()
elseif isdir(dir)
files = readdir(dir)
else
return matches
local files
try
if length(dir) == 0
files = readdir()
elseif isdir(dir)
files = readdir(dir)
else
return UTF8String[], 0:-1, false
end
catch
return UTF8String[], 0:-1, false
end
matches = UTF8String[]
for file in files
if beginswith(file, prefix)
p = joinpath(dir, file)
push!(matches, isdir(p) ? joinpath(p,"") : p)
push!(matches, isdir(joinpath(dir, file)) ? joinpath(file,"") : file)
end
end
matches
matches, (nextind(path, pos-sizeof(prefix))):pos, length(matches) > 0
end

function complete_methods(input::String)
Expand Down Expand Up @@ -176,18 +180,23 @@ function latex_completions(string, pos)
end

function completions(string, pos)
inc_tag = Base.incomplete_tag(parse(string[1:pos], raise=false))
# First parse everything up to the current position
partial = string[1:pos]
inc_tag = Base.incomplete_tag(parse(partial , raise=false))
if inc_tag in [:cmd, :string]
startpos = nextind(string, rsearch(string, non_filename_chars, pos))
startpos = nextind(partial, rsearch(partial, non_filename_chars, pos))
r = startpos:pos
paths = complete_path(string[r])
if inc_tag == :string && length(paths) == 1 && !isdir(paths[1])
paths, r, success = complete_path(string[r], pos)
if inc_tag == :string &&
length(paths) == 1 && # Only close if there's a single choice,
!isdir(string[startpos:start(r)-1] * paths[1]) && # except if it's a directory
(length(string) <= pos || string[pos+1] != '"') # or there's already a " at the cursor.
paths[1] *= "\""
end
return sort(paths), r, true
return sort(paths), r, success
end

ok, ret = latex_completions(string,pos)
ok, ret = latex_completions(string, pos)
ok && return ret

if inc_tag == :other && string[pos] == '('
Expand Down Expand Up @@ -230,7 +239,7 @@ function completions(string, pos)
return sort(unique(suggestions)), (dotpos+1):pos, true
end

function shell_completions(string,pos)
function shell_completions(string, pos)
# First parse everything up to the current position
scs = string[1:pos]
local args, last_parse
Expand All @@ -239,34 +248,12 @@ function shell_completions(string,pos)
catch
return UTF8String[], 0:-1, false
end
# Now look at the last this we parsed
# Now look at the last thing we parsed
isempty(args.args[end].args) && return UTF8String[], 0:-1, false
arg = args.args[end].args[end]
if isa(arg,String)
# Treat this as a path (perhaps give a list of comands in the future as well?)
dir,name = splitdir(arg)
local files
try
if isempty(dir)
files = readdir()
else
isdir(dir) || return UTF8String[], 0:-1, false
files = readdir(dir)
end
catch
return UTF8String[], 0:-1, false
end
# Filter out files and directories that do not begin with the partial name we were
# completing and append "/" to directories to simplify further completion
ret = map(filter(x->beginswith(x, name), files)) do x
if !isdir(joinpath(dir, x))
return x
else
return x*"/"
end
end
r = (nextind(string, pos-sizeof(name))):pos
return ret, r, true
return complete_path(arg, pos)
elseif isexpr(arg, :escape) && (isexpr(arg.args[1], :incomplete) || isexpr(arg.args[1], :error))
r = first(last_parse):prevind(last_parse, last(last_parse))
partial = scs[r]
Expand Down
32 changes: 31 additions & 1 deletion test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ c,r = test_complete(s)
@test r == 19:23
@test s[r] == "getin"

# inexistent completion inside a string
s = "Pkg.add(\"lol"
c,r,res = test_complete(s)
@test res == false

# test latex symbol completions
s = "\\alpha"
c,r = test_latexcomplete(s)
Expand Down Expand Up @@ -96,8 +101,33 @@ c,r = test_latexcomplete(s)

@unix_only begin
#Assume that we can rely on the existence and accessibility of /tmp

# Tests path in Julia code and closing " if it's a file
# Issue #8047
s = "@show \"/dev/nul"
c,r = test_complete(s)
@test "null\"" in c
@test r == 13:15
@test s[r] == "nul"

# Tests path in Julia code and not closing " if it's a directory
# Issue #8047
s = "@show \"/tm"
c,r = test_complete(s)
@test "tmp/" in c
@test r == 9:10
@test s[r] == "tm"

# Tests path in Julia code and not double-closing "
# Issue #8047
s = "@show \"/dev/nul\""
c,r = completions(s, 15)
@test "null" in c
@test r == 13:15
@test s[r] == "nul"

s = "/t"
c,r = test_scomplete("/t")
c,r = test_scomplete(s)
@test "tmp/" in c
@test r == 2:2
@test s[r] == "t"
Expand Down

0 comments on commit 538b3d4

Please sign in to comment.