-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't error when transposing an empty buffer #46925
Don't error when transposing an empty buffer #46925
Conversation
@@ -1101,7 +1101,7 @@ end | |||
|
|||
function edit_transpose_chars(buf::IOBuffer) | |||
# Moving left but not transpoing anything is intentional, and matches Emacs's behavior | |||
eof(buf) && char_move_left(buf) | |||
eof(buf) && position(buf) !== 0 && char_move_left(buf) | |||
position(buf) == 0 && return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just swap this and the line above rather than duplicating the condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was the original logic that led to #45417 being filed—what will happen is if the buffer's position is at 1, with a length of 1, then we'll try to read past the end of the buffer.
Since I figured that it'd be best to be as close to Emacs as possible, I went with just flipping those two lines (hence the comment).
In this PR, I'm not sure there's a good way to get around the duplication of the position(buf)
stuff unless I'm missing something obvious (which isn't unlikely)—we ultimately need to check it twice, first time to make sure we can move left, and then again to make sure we aren't at the start of the buffer. One other idea could be something like this though, but I'm not sure which I prefer (probably this version, but I'm curious what you think):
function edit_transpose_chars(buf::IOBuffer)
content(buf) == "" && 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
# ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see char_move_left
might update the buf position. Alright, seems reasonable I suppose
(cherry picked from commit 757f21e)
(cherry picked from commit 757f21e)
This fixes an issue where transposing an empty buffer in the REPL with ^T would cause an error.
Fixes #45417—for real this time! (See also #45420, which introduced this bug.)