Skip to content

Commit 6acc46b

Browse files
perryprogKristofferC
authored and
KristofferC
committed
Don't error when transposing a single character (#45420)
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. (cherry picked from commit 9dd993e)
1 parent a02630e commit 6acc46b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

stdlib/REPL/src/LineEdit.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1085,8 +1085,9 @@ function edit_transpose_chars(s::MIState)
10851085
end
10861086

10871087
function edit_transpose_chars(buf::IOBuffer)
1088-
position(buf) == 0 && return false
1088+
# Moving left but not transpoing anything is intentional, and matches Emacs's behavior
10891089
eof(buf) && char_move_left(buf)
1090+
position(buf) == 0 && return false
10901091
char_move_left(buf)
10911092
pos = position(buf)
10921093
a, b = read(buf, Char), read(buf, Char)

stdlib/REPL/test/lineedit.jl

+10
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ let buf = IOBuffer()
375375
@test content(buf) == "βγαεδ"
376376
LineEdit.edit_transpose_chars(buf)
377377
@test content(buf) == "βγαδε"
378+
379+
seek(buf, 0)
380+
@inferred(LineEdit.edit_clear(buf))
381+
edit_insert(buf, "a")
382+
LineEdit.edit_transpose_chars(buf)
383+
@test content(buf) == "a"
384+
seekend(buf)
385+
LineEdit.edit_transpose_chars(buf)
386+
@test content(buf) == "a"
387+
@test position(buf) == 0
378388
end
379389

380390
@testset "edit_word_transpose" begin

0 commit comments

Comments
 (0)