backport: feat(restart)!: ZR restores session #40669

(cherry picked from commit c0a16a5977)
This commit is contained in:
Nathan Zeng
2026-07-10 03:58:59 -07:00
committed by GitHub
parent 65345c9981
commit 3dc32d5cd1
4 changed files with 31 additions and 10 deletions

View File

@@ -1209,8 +1209,10 @@ ZZ Write current file, if modified, and close the current
ZQ Quit without checking for changes (same as ":q!").
*ZR*
[count]ZR Performs |:restart!|. If a [count] is given, restarts
without checking for changes (":restart! +qall!").
[count]ZR Performs |:restart|. If a [count] of [1-8] is given,
the session is not restored (":restart! +qall"). If a
[count] of 9 is given, restarts without restoring the
session or checking for changes (":restart! +qall!").
MULTIPLE WINDOWS AND BUFFERS *window-exit*

View File

@@ -192,7 +192,7 @@ EDITOR
(Or use `:{range}lua` to skip the detection.)
• |:wall| with |++p| auto-creates missing parent directories.
• |:restart| now restores the current session while |:restart!| does not.
• |ZR| restarts Nvim (|:restart!|).
• |ZR| restarts Nvim (|:restart|).
EVENTS

View File

@@ -3299,12 +3299,14 @@ static void nv_Zet(cmdarg_T *cap)
do_cmdline_cmd("q!");
break;
// "ZR": restart. With count, restart without checking for changes.
// "ZR": restart. With count, does not restore session/check for changes.
case 'R':
if (cap->count0 >= 1) {
if (cap->count0 >= 1 && cap->count0 <= 8) {
do_cmdline_cmd("restart!");
} else if (cap->count0 == 9) {
do_cmdline_cmd("restart! +qall!");
} else {
do_cmdline_cmd("restart!");
do_cmdline_cmd("restart");
}
break;

View File

@@ -340,7 +340,12 @@ describe('TUI :restart', function()
end)
it('ZR', function()
-- Just exercise ZR, don't need to test all :restart! functionality here.
-- Just exercise ZR, don't need to test all :restart functionality here.
local file = 'Xtest-restart-file'
write_file(file, 'foo')
finally(function()
os.remove(file)
end)
local server_pipe = new_pipename()
local server_session
finally(function()
@@ -363,18 +368,30 @@ describe('TUI :restart', function()
finally(function()
os.remove(testlog)
end)
screen:expect({ any = '%[No Name%]' })
feed_data(':edit ' .. file .. '\r')
screen:expect({ any = 'foo' })
server_session = n.connect(server_pipe)
local _, starttime = server_session:request('nvim_eval', 'v:starttime')
-- ZR preserves screen state
tt.feed_data('ZR')
starttime, server_session = assert_restarted(starttime, server_session, server_pipe)
screen:expect({ any = 'foo' })
-- [1-8]ZR does not preserve screen state
tt.feed_data('1ZR')
screen:expect({ any = vim.pesc('[No Name]'), none = 'foo' })
-- ZR on modified buffer fails with E37.
tt.feed_data('ifoo\027')
tt.feed_data('ZR')
screen:expect({ any = 'E37:' })
-- [count]ZR discards unsaved changes.
tt.feed_data('1ZR')
screen:expect({ any = 'E37:' })
-- [9]ZR discards unsaved changes.
tt.feed_data('9ZR')
screen:expect({ any = vim.pesc('[No Name]') })
starttime, server_session = assert_restarted(starttime, server_session, server_pipe)