Skip to content

Commit

Permalink
fix: make split views behave with splitkeep and restore cursor positi…
Browse files Browse the repository at this point in the history
…on after re-render
  • Loading branch information
folke committed Nov 12, 2022
1 parent 6824794 commit 0b1fb33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions lua/noice/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ local ConfigViews = require("noice.config.views")
local Util = require("noice.util")
local Object = require("nui.object")
local Format = require("noice.text.format")
local Markdown = require("noice.text.markdown")

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

self._visible = true
else
if self._visible then
self:hide()
end
self._visible = false
self:hide()
end
return true
end
Expand Down Expand Up @@ -188,12 +189,17 @@ function View:content()
end

function View:set_win_options(win)
vim.api.nvim_win_set_option(win, "winbar", "")
vim.api.nvim_win_set_option(win, "foldenable", false)
vim.wo[win].winbar = ""
vim.wo[win].foldenable = false
if self._opts.win_options then
require("nui.utils")._.set_win_options(win, self._opts.win_options)
end
vim.api.nvim_win_set_cursor(win, { 1, 0 })
vim.schedule(function()
vim.api.nvim_win_set_cursor(win, { 1, 0 })
vim.api.nvim_win_call(win, function()
vim.cmd([[noautocmd silent! normal! zt]])
end)
end)
end

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

---@type number?
local win = vim.fn.bufwinid(buf)
if win == -1 then
win = nil
end
local cursor = win and vim.api.nvim_win_get_cursor(win)

if not opts.highlight then
vim.api.nvim_buf_set_lines(buf, linenr - 1, -1, false, {})
end
Expand All @@ -230,6 +243,11 @@ function View:render(buf, opts)
end
linenr = linenr + m:height()
end

if cursor then
-- restore cursor
pcall(vim.api.nvim_win_set_cursor, win, cursor)
end
end

return View
2 changes: 1 addition & 1 deletion lua/noice/view/nui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ function NuiView:show()
if not self._nui.winid then
return
end
self:set_win_options(self._nui.winid)
self:tag()
if not self._visible then
self:set_win_options(self._nui.winid)
self:update_layout()
self:smart_move()
end
Expand Down

0 comments on commit 0b1fb33

Please sign in to comment.