Skip to content

Commit

Permalink
REPL: newline auto-indents like line above
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jul 25, 2017
1 parent 510efbf commit 0dd4390
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
21 changes: 15 additions & 6 deletions base/repl/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ complete_line(c::EmptyCompletionProvider, s) = [], true, true
terminal(s::IO) = s
terminal(s::PromptState) = s.terminal

for f in [:terminal, :edit_insert, :on_enter, :add_history, :buffer, :edit_backspace, :(Base.isempty),
:replace_line, :refresh_multi_line, :input_string, :edit_move_left, :edit_move_right,
:edit_move_word_left, :edit_move_word_right, :update_display_buffer]
for f in [:terminal, :edit_insert, :edit_insert_newline, :on_enter, :add_history,
:buffer, :edit_backspace, :(Base.isempty), :replace_line, :refresh_multi_line,
:input_string, :edit_move_left, :edit_move_right,
:edit_move_word_left, :edit_move_word_right, :update_display_buffer]
@eval ($f)(s::MIState, args...) = $(f)(s.mode_state[s.current_mode], args...)
end

Expand Down Expand Up @@ -271,7 +272,7 @@ end


# Edit functionality
is_non_word_char(c) = c in " \t\n\"\\'`@\$><=:;|&{}()[].,+-*/?%^~"
is_non_word_char(c) = c in """ \t\n\"\\'`@\$><=:;|&{}()[].,+-*/?%^~"""

function reset_key_repeats(f::Function, s::MIState)
key_repeats_sav = s.key_repeats
Expand Down Expand Up @@ -475,6 +476,14 @@ function edit_insert(buf::IOBuffer, c)
end
end

function edit_insert_newline(s::PromptState)
buf = buffer(s)
beg = rsearch(buf.data, '\n', position(buf))
align = findnext(c -> c != UInt8(' '), buf.data, beg+1) - 1 - beg
edit_insert(buf, '\n' * ' '^align)
refresh_line(s)
end

function edit_backspace(s::PromptState)
if edit_backspace(s.input_buffer)
refresh_line(s)
Expand Down Expand Up @@ -1354,7 +1363,7 @@ AnyDict(
commit_line(s)
return :done
else
edit_insert(s, '\n')
edit_insert_newline(s)
end
end,
'\n' => KeyAlias('\r'),
Expand Down Expand Up @@ -1388,7 +1397,7 @@ AnyDict(
# Ctrl-Right Arrow on rxvt
"\eOc" => "\ef",
# Meta Enter
"\e\r" => (s,o...)->(edit_insert(s, '\n')),
"\e\r" => (s,o...)->edit_insert_newline(s),
"\e\n" => "\e\r",
# Simply insert it into the buffer by default
"*" => (s,data,c)->(edit_insert(s, c)),
Expand Down
16 changes: 16 additions & 0 deletions test/lineedit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,19 @@ let
Base.LineEdit.InputAreaState(0,0), "julia> ", indent = 7)
@test s == Base.LineEdit.InputAreaState(3,1)
end

@testset "newline alignment feature" begin
term = TestHelpers.FakeTerminal(IOBuffer(), IOBuffer(), IOBuffer())
s = LineEdit.init_state(term, ModalInterface([Prompt("test> ")]))
function bufferdata(s)
buf = LineEdit.buffer(s)
String(buf.data[1:buf.size])
end

LineEdit.edit_insert(s, "for x=1:10\n a = 1")
LineEdit.edit_insert_newline(s)
@test bufferdata(s) == "for x=1:10\n a = 1\n "
LineEdit.edit_insert(s, " b = 2")
LineEdit.edit_insert_newline(s)
@test bufferdata(s) == "for x=1:10\n a = 1\n b = 2\n "
end

0 comments on commit 0dd4390

Please sign in to comment.