Skip to content

Commit 0b1fb33

Browse files
committed
fix: make split views behave with splitkeep and restore cursor position after re-render
1 parent 6824794 commit 0b1fb33

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lua/noice/view/init.lua

+23-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ local ConfigViews = require("noice.config.views")
55
local Util = require("noice.util")
66
local Object = require("nui.object")
77
local Format = require("noice.text.format")
8-
local Markdown = require("noice.text.markdown")
98

109
---@class NoiceViewBaseOptions
1110
---@field buf_options? table<string,any>
@@ -138,8 +137,10 @@ function View:display()
138137

139138
self._visible = true
140139
else
140+
if self._visible then
141+
self:hide()
142+
end
141143
self._visible = false
142-
self:hide()
143144
end
144145
return true
145146
end
@@ -188,12 +189,17 @@ function View:content()
188189
end
189190

190191
function View:set_win_options(win)
191-
vim.api.nvim_win_set_option(win, "winbar", "")
192-
vim.api.nvim_win_set_option(win, "foldenable", false)
192+
vim.wo[win].winbar = ""
193+
vim.wo[win].foldenable = false
193194
if self._opts.win_options then
194195
require("nui.utils")._.set_win_options(win, self._opts.win_options)
195196
end
196-
vim.api.nvim_win_set_cursor(win, { 1, 0 })
197+
vim.schedule(function()
198+
vim.api.nvim_win_set_cursor(win, { 1, 0 })
199+
vim.api.nvim_win_call(win, function()
200+
vim.cmd([[noautocmd silent! normal! zt]])
201+
end)
202+
end)
197203
end
198204

199205
---@param buf number buffer number
@@ -218,6 +224,13 @@ function View:render(buf, opts)
218224
vim.api.nvim_buf_clear_namespace(buf, Config.ns, linenr - 1, -1)
219225
vim.b[buf].messages = {}
220226

227+
---@type number?
228+
local win = vim.fn.bufwinid(buf)
229+
if win == -1 then
230+
win = nil
231+
end
232+
local cursor = win and vim.api.nvim_win_get_cursor(win)
233+
221234
if not opts.highlight then
222235
vim.api.nvim_buf_set_lines(buf, linenr - 1, -1, false, {})
223236
end
@@ -230,6 +243,11 @@ function View:render(buf, opts)
230243
end
231244
linenr = linenr + m:height()
232245
end
246+
247+
if cursor then
248+
-- restore cursor
249+
pcall(vim.api.nvim_win_set_cursor, win, cursor)
250+
end
233251
end
234252

235253
return View

lua/noice/view/nui.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@ function NuiView:show()
274274
if not self._nui.winid then
275275
return
276276
end
277-
self:set_win_options(self._nui.winid)
278277
self:tag()
279278
if not self._visible then
279+
self:set_win_options(self._nui.winid)
280280
self:update_layout()
281281
self:smart_move()
282282
end

0 commit comments

Comments
 (0)