Problem: After :diffget into an empty buffer, undo does not restore the
empty buffer, the last line stays behind (Narendran
Gopalakrishnan)
Solution: Include the empty line of the empty buffer in the undo
information, it is deleted once the first line was obtained
(Hirohito Higashi).
fixes: #20950
closes: #20951
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Hirohito Higashi <h.east.727@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
dfree = NULL;
lnum = dp->df_lnum[idx_to];
count = dp->df_count[idx_to];
+ // The empty line of an empty buffer is deleted below, include it in
+ // the undo information, otherwise undo leaves a line behind.
+ linenr_T undo_bot = lnum + count + (count == 0 && BUFEMPTY() ? 1 : 0);
+
if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
- && u_save(lnum - 1, lnum + count) != FAIL)
+ && u_save(lnum - 1, undo_bot) != FAIL)
{
// Inside the specified range and saving for undo worked.
start_skip = 0;
%bwipe!
endfunc
+" Undo after getting lines into an empty buffer must leave it empty again
+func Test_diffget_undo_empty_buffer()
+ enew!
+ diffthis
+ new
+ call setline(1, ['1', '2'])
+ diffthis
+
+ wincmd p
+ normal do
+ call assert_equal(['1', '2'], getline(1, '$'))
+ undo
+ call assert_equal([''], getline(1, '$'))
+
+ windo diffoff
+ %bwipe!
+endfunc
+
" Test putting two changes from one buffer to another
func Test_diffput_two()
new a
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 914,
/**/
913,
/**/