Skip to content

Commit

Permalink
repl: fix first-col edit_move_{up,down} bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nolta committed Mar 30, 2014
1 parent ca8aea3 commit c2f309c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
38 changes: 16 additions & 22 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,59 +332,53 @@ end
# Querying the terminal is expensive, memory access is cheap
# so to find the current column, we find the offset for the start
# of the line.
function edit_move_up(s)
buf = buffer(s)

function edit_move_up(buf::IOBuffer)
npos = rsearch(buf.data,'\n',position(buf))
if npos == 0 #we're in the first line
return false
end
# We're interested in character count, not byte count
offset = length(bytestring(buf.data[(npos+1):(position(buf))]))-1
offset = length(bytestring(buf.data[(npos+1):(position(buf))]))
npos2 = rsearch(buf.data,'\n',npos-1)
seek(buf,npos2+1)
seek(buf,npos2)
for _ = 1:offset
pos = position(buf)
if read(buf,Char) == '\n'
seek(buf,pos)
break
end
end
refresh_line(s)
return true
end
function edit_move_up(s)
changed = edit_move_up(buffer(s))
changed && refresh_line(s)
changed
end

function edit_move_down(s)
buf = buffer(s)
function edit_move_down(buf::IOBuffer)
npos = rsearch(buf.data[1:buf.size],'\n',position(buf))
# We're interested in character count, not byte count
offset = length(bytestring(buf.data[(npos+1):(position(buf))]))-1
offset = length(bytestring(buf.data[(npos+1):(position(buf))]))
npos2 = search(buf.data[1:buf.size],'\n',position(buf)+1)
if npos2 == 0 #we're in the last line
return false
end
seek(buf,npos2+1)
seek(buf,npos2)
for _ = 1:offset
pos = position(buf)
if eof(buf) || read(buf,Char) == '\n'
seek(buf,pos)
break
end
end
refresh_line(s)
return true
end

function charlen(ch::Char)
if ch < 0x80
return 1
elseif ch < 0x800
return 2
elseif ch < 0x10000
return 3
elseif ch < 0x110000
return 4
end
error("Corrupt UTF8")
function edit_move_down(s)
changed = edit_move_down(buffer(s))
changed && refresh_line(s)
changed
end


Expand Down
42 changes: 42 additions & 0 deletions test/lineedit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,45 @@ run_test(test2_func,IOBuffer("aaabb"))
run_test(test3_func,IOBuffer("aab"))
@test a_bar == 2
@test b_bar == 1

buf = IOBuffer("type X\n a::Int\nend")
for i = 0:6
seek(buf,i)
@test !LineEdit.edit_move_up(buf)
@test position(buf) == i
seek(buf,i)
@test LineEdit.edit_move_down(buf)
@test position(buf) == i+7
end
for i = 7:17
seek(buf,i)
@test LineEdit.edit_move_up(buf)
@test position(buf) == min(i-7,6)
seek(buf,i)
@test LineEdit.edit_move_down(buf)
@test position(buf) == min(i+11,21)
end
for i = 18:21
seek(buf,i)
@test LineEdit.edit_move_up(buf)
@test position(buf) == i-11
seek(buf,i)
@test !LineEdit.edit_move_down(buf)
@test position(buf) == i
end

buf = IOBuffer("type X\n\n")
seekend(buf)
@test LineEdit.edit_move_up(buf)
@test position(buf) == 7
@test LineEdit.edit_move_up(buf)
@test position(buf) == 0
@test !LineEdit.edit_move_up(buf)
@test position(buf) == 0
seek(buf,0)
@test LineEdit.edit_move_down(buf)
@test position(buf) == 7
@test LineEdit.edit_move_down(buf)
@test position(buf) == 8
@test !LineEdit.edit_move_down(buf)
@test position(buf) == 8

0 comments on commit c2f309c

Please sign in to comment.