Skip to content

Commit

Permalink
Don't error when transposing a single character (#45420)
Browse files Browse the repository at this point in the history
This fixes an issue where an error would be thrown in the REPL if you tried to
transpose an input that was a single character while your cursor was to the
right of that character (e.g., "A|"). To fix this, let's move left once before
we check for if we're at the start of a line. This does change behavior slightly
in that the cursor can move left once without actually transposing anything, but
this seems to match what Emacs does with M-x transpose-chars in an equivalent
situation.
  • Loading branch information
perryprog authored May 23, 2022
1 parent e0d8ba7 commit 9dd993e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1087,8 +1087,9 @@ function edit_transpose_chars(s::MIState)
end

function edit_transpose_chars(buf::IOBuffer)
position(buf) == 0 && return false
# Moving left but not transpoing anything is intentional, and matches Emacs's behavior
eof(buf) && char_move_left(buf)
position(buf) == 0 && return false
char_move_left(buf)
pos = position(buf)
a, b = read(buf, Char), read(buf, Char)
Expand Down
10 changes: 10 additions & 0 deletions stdlib/REPL/test/lineedit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,16 @@ let buf = IOBuffer()
@test content(buf) == "βγαεδ"
LineEdit.edit_transpose_chars(buf)
@test content(buf) == "βγαδε"

seek(buf, 0)
@inferred(LineEdit.edit_clear(buf))
edit_insert(buf, "a")
LineEdit.edit_transpose_chars(buf)
@test content(buf) == "a"
seekend(buf)
LineEdit.edit_transpose_chars(buf)
@test content(buf) == "a"
@test position(buf) == 0
end

@testset "edit_word_transpose" begin
Expand Down

0 comments on commit 9dd993e

Please sign in to comment.