Skip to content

Commit

Permalink
Fixes space completion in paths in the REPL like: cd C:\\Program\ Fil…
Browse files Browse the repository at this point in the history
…es\\
  • Loading branch information
dhoegh committed Nov 23, 2014
1 parent b82aa82 commit f333eec
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
11 changes: 6 additions & 5 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ function complete_path(path::AbstractString, pos)
push!(matches, id ? file * (@windows? "\\\\" : "/") : file)
end
end
matches, (nextind(path, pos-sizeof(prefix))):pos, length(matches) > 0
matches = UTF8String[replace(s, r"\s", "\\ ") for s in matches]
return matches, nextind(path, pos - sizeof(prefix) - length(matchall(r" ", prefix))):pos, length(matches) > 0
end

function complete_methods(input::AbstractString)
Expand All @@ -148,7 +149,6 @@ end
include("latex_symbols.jl")

const non_identifier_chars = [" \t\n\r\"\\'`\$><=:;|&{}()[],+-*/?%^~"...]
const non_filename_chars = [(" \t\n\r\"'`@\$><=;|&{(" * (@unix?"\\" : "")) ...]
const whitespace_chars = [" \t\n\r"...]

# Aux function to detect whether we're right after a
Expand Down Expand Up @@ -187,12 +187,13 @@ function completions(string, pos)
partial = string[1:pos]
inc_tag = Base.incomplete_tag(parse(partial , raise=false))
if inc_tag in [:cmd, :string]
startpos = nextind(partial, rsearch(partial, non_filename_chars, pos))
m = match(r"[\t\n\r\"'`@\$><=;|&\{]| (?!\\)",reverse(partial))
startpos = length(partial)-(m == nothing ? 1 : m.offset) + 2
r = startpos:pos
paths, r, success = complete_path(string[r], pos)
paths, r, success = complete_path(unescape_string(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
!isdir(unescape_string(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
Expand Down
19 changes: 18 additions & 1 deletion base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,24 @@ macro mstr(s...); triplequoted(s...); end
## shell-like command parsing ##

function shell_parse(raw::AbstractString, interp::Bool)
s = strip(raw)
s = lstrip(raw)
#Strips the end but respects the space when the string endswith "\\ "
r = RevString(s)
i = start(r)
c_old = nothing
while !done(r,i)
c, j = next(r,i)
if c == '\\' && c_old == ' '
i -= 1
break
elseif !(c in _default_delims)
break
end
i = j
c_old = c
end
s = s[1:end-i+1]

last_parse = 0:-1
isempty(s) && return interp ? (Expr(:tuple,:()),last_parse) : ([],last_parse)

Expand Down
15 changes: 15 additions & 0 deletions test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ end
c,r = test_complete(s)
@test r == length(s) - 1:length(s)
@test file in c

mv(file, "space .test")
try
s = "rm space"
c,r = test_scomplete(s)
@test r == 4:8
@test "space\\ .test" in c

s = "cd(\"space"
c,r = test_complete(s)
@test r == 5:9
@test "space\\ .test\"" in c
finally
mv("space .test", file)
end
end
rm(tmp)
end

0 comments on commit f333eec

Please sign in to comment.