Skip to content

Commit

Permalink
remove repl prompt prefix before parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jul 25, 2016
1 parent 0d7c014 commit 43e0438
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1302,9 +1302,14 @@ Must satisfy `0 < tabwidth <= 16`.
"""
global tabwidth = 8

const STANDARD_REPL_PROMPTS = ["julia> ", "help?> ", "shell> "]

function bracketed_paste(s)
ps = state(s, mode(s))
input = readuntil(ps.terminal, "\e[201~")[1:(end-6)]
if ps.p.prompt in STANDARD_REPL_PROMPTS && startswith(input, ps.p.prompt)
input = input[sizeof(ps.p.prompt)+1:end]
end
input = replace(input, '\r', '\n')
if position(buffer(s)) == 0
indent = Base.indentation(input; tabwidth=tabwidth)[1]
Expand Down
12 changes: 12 additions & 0 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,18 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
oldpos = start(input)
firstline = true
while !done(input, oldpos) # loop until all lines have been executed
# Check if the next statement starts with "julia> ", in that case
# skip it. But first skip whitespace
c = oldpos
while c <= sizeof(input) && (input[c] == '\n' || input[c] == ' ' || input[c] == '\t')
c = nextind(input, c)
end
n_newlines = c - oldpos
# Skip over prompt prefix if statement starts with it
jl_prompt_len = 7
if c + jl_prompt_len <= sizeof(input) && input[c:c+jl_prompt_len-1] == "julia> "
oldpos += jl_prompt_len + n_newlines
end
ast, pos = Base.syntax_deprecation_warnings(false) do
Base.parse(input, oldpos, raise=false)
end
Expand Down
58 changes: 58 additions & 0 deletions test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,64 @@ begin
# Try entering search mode while in custom repl mode
LineEdit.enter_search(s, custom_histp, true)
end

# Test removal of prompt in bracket pasting
begin
stdin_write, stdout_read, stderr_read, repl = fake_repl()

repl.interface = REPL.setup_interface(repl)
repl_mode = repl.interface.modes[1]
shell_mode = repl.interface.modes[2]
help_mode = repl.interface.modes[3]

repltask = @async begin
Base.REPL.run_repl(repl)
end

c = Condition()

sendrepl2(cmd) = write(stdin_write,"$cmd\n notify(c)\n")

# Test single statement
sendrepl2("\e[200~julia> A = 2\e[201~\n")
wait(c)
@test A == 2

# Test multi statement
sendrepl2("""\e[200~
julia> type T17599; a::Int; end
function foo(julia)
julia> 3
end
julia> A = 3\e[201~
""")
wait(c)
@test A == 3
@test foo(4)
@test T17599(3).a == 3
@test !foo(2)

# Test only works in bracket mode
sendrepl2("julia = 4\n julia> 3 && (A = 1)\n")
wait(c)
@test A == 1

# Test shell> mode
tmpdir = mktempdir()
curr_dir = pwd()
write(stdin_write, ";")
write(stdin_write, "\e[200~shell> cd $(escape_string(tmpdir))\e[201~\n")
sendrepl2("tmpdirnow = pwd()")
wait(c)
@test tmpdirnow == tmpdir
cd(curr_dir)

write(stdin_write, '\x04')
wait(repltask)
end

# Simple non-standard REPL tests
if !is_windows() || Sys.windows_version() >= Sys.WINDOWS_VISTA_VER
stdin_write, stdout_read, stdout_read, repl = fake_repl()
Expand Down

0 comments on commit 43e0438

Please sign in to comment.