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 f738c87 commit 2689e9e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 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
38 changes: 17 additions & 21 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -727,28 +727,16 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
on_enter = return_callback)

# Setup help mode
help_ondone = x -> begin
if startswith(x, "help?> ")
x = x[8:end]
end
Docs.helpmode(x)
end
help_mode = Prompt("help?> ",
prompt_prefix = hascolor ? repl.help_color : "",
prompt_suffix = hascolor ?
(repl.envcolors ? Base.input_color : repl.input_color) : "",
keymap_func_data = repl,
complete = replc,
# When we're done transform the entered line into a call to help("$line")
on_done = respond(help_ondone, repl, julia_prompt))
on_done = respond(Docs.helpmode, repl, julia_prompt))

# Set up shell mode
shell_ondone = x -> begin
if startswith(x, "shell> ")
x = x[8:end]
end
Expr(:call, :(Base.repl_cmd), macroexpand(Expr(:macrocall, Symbol("@cmd"), x)), outstream(repl))
end
shell_mode = Prompt("shell> ";
prompt_prefix = hascolor ? repl.shell_color : "",
prompt_suffix = hascolor ?
Expand All @@ -758,7 +746,9 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
# Transform "foo bar baz" into `foo bar baz` (shell quoting)
# and pass into Base.repl_cmd for processing (handles `ls` and `cd`
# special)
on_done = respond(shell_ondone, repl, julia_prompt))
on_done = respond(repl, julia_prompt) do line
Expr(:call, :(Base.repl_cmd), macroexpand(Expr(:macrocall, Symbol("@cmd"),line)), outstream(repl))
end)


################################# Stage II #############################
Expand All @@ -785,13 +775,7 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
shell_mode.hist = hp
help_mode.hist = hp

function julia_ondone(x)
if startswith(x, "julia> ")
x = x[8:end]
end
Base.parse_input_line(x,filename=repl_filename(repl,hp))
end
julia_prompt.on_done = respond(julia_ondone, repl, julia_prompt)
julia_prompt.on_done = respond(x->Base.parse_input_line(x,filename=repl_filename(repl,hp)), repl, julia_prompt)


search_prompt, skeymap = LineEdit.setup_search_keymap(hp)
Expand Down Expand Up @@ -848,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
60 changes: 60 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 Expand Up @@ -480,6 +538,8 @@ if !is_windows()
close(master)
end



# Test stream mode
if !is_windows() || Sys.windows_version() >= Sys.WINDOWS_VISTA_VER
outs, ins, p = readandwrite(`$exename --startup-file=no --quiet`)
Expand Down

0 comments on commit 2689e9e

Please sign in to comment.