Skip to content

Commit

Permalink
Merge pull request #22948 from JuliaLang/rf/repl-newline
Browse files Browse the repository at this point in the history
REPL: newline auto-indents like line above
  • Loading branch information
StefanKarpinski committed Aug 22, 2017
2 parents e6c8b6b + 33f8f64 commit 85cb4a8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
24 changes: 19 additions & 5 deletions base/repl/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,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 @@ -473,6 +474,19 @@ function edit_insert(buf::IOBuffer, c)
end
end

# align: number of ' ' to insert after '\n'
# if align < 0: align like line above
function edit_insert_newline(s::PromptState, align=-1)
buf = buffer(s)
if align < 0
beg = beginofline(buf)
align = findnext(_notspace, buf.data[beg+1:buf.size], 1) - 1
align < 0 && (align = buf.size-beg)
end
edit_insert(buf, '\n' * ' '^align)
refresh_line(s)
end

# align: delete up to 4 spaces to align to a multiple of 4 chars
# adjust: also delete spaces on the right of the cursor to try to keep aligned what is
# on the right
Expand Down Expand Up @@ -1411,7 +1425,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 @@ -1445,7 +1459,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
23 changes: 23 additions & 0 deletions test/lineedit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,26 @@ end
@test bufpos(s) == 26
@test bufferdata(s) == "for x=1:10\n éé=3\n \nend"
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 é = 1")
LineEdit.edit_insert_newline(s)
@test bufferdata(s) == "for x=1:10\n é = 1\n "
LineEdit.edit_insert(s, " b = 2")
LineEdit.edit_insert_newline(s)
@test bufferdata(s) == "for x=1:10\n é = 1\n b = 2\n "
# after an empty line, should still insert the expected number of spaces
LineEdit.edit_insert_newline(s)
@test bufferdata(s) == "for x=1:10\n é = 1\n b = 2\n \n "
LineEdit.edit_insert_newline(s, 0)
@test bufferdata(s) == "for x=1:10\n é = 1\n b = 2\n \n \n"
LineEdit.edit_insert_newline(s, 2)
@test bufferdata(s) == "for x=1:10\n é = 1\n b = 2\n \n \n\n "
end

0 comments on commit 85cb4a8

Please sign in to comment.