Skip to content

Commit

Permalink
perf(cmdline): prevent unneeded redraws during cmdline preview (subst…
Browse files Browse the repository at this point in the history
…itute). Fixes #803
  • Loading branch information
folke committed May 16, 2024
1 parent 4c26991 commit 8d924eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
15 changes: 14 additions & 1 deletion lua/noice/ui/cmdline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ end

---@type NoiceCmdline[]
M.cmdlines = {}
M.skipped = false

function M.on_show(event, content, pos, firstc, prompt, indent, level)
local c = Cmdline({
Expand All @@ -167,6 +168,14 @@ function M.on_show(event, content, pos, firstc, prompt, indent, level)
indent = indent,
level = level,
})

-- This was triggered by a force redraw, so skip it
if c:get():find(Hacks.SPECIAL, 1, true) then
M.skipped = true
return
end
M.skipped = false

local last = M.cmdlines[level] and M.cmdlines[level].state
if not vim.deep_equal(c.state, last) then
M.active = c
Expand All @@ -189,7 +198,11 @@ function M.on_hide(_, level)
end

function M.on_pos(_, pos, level)
if M.cmdlines[level] and M.cmdlines[level].state.pos ~= pos then
if M.skipped then
return
end
local c = M.cmdlines[level]
if c and c.state.pos ~= pos then
M.cmdlines[level].state.pos = pos
M.update()
end
Expand Down
7 changes: 3 additions & 4 deletions lua/noice/util/hacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,14 @@ function M.fix_cmp()
end)
end

M.SPECIAL = "Þ"
function M.cmdline_force_redraw()
if not require("noice.util.ffi").cmdpreview then
return
end

-- HACK: this will trigger redraw during substitute and cmdpreview,
-- but when moving the cursor, the screen will be cleared until
-- a new character is entered
vim.api.nvim_feedkeys(" " .. Util.BS, "n", true)
-- HACK: this will trigger redraw during substitute and cmdpreview
vim.api.nvim_feedkeys(M.SPECIAL .. Util.BS, "n", true)
end

---@type string?
Expand Down
5 changes: 5 additions & 0 deletions lua/noice/util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ M.ESC = M.t("<esc>")
M.BS = M.t("<bs>")
M.EXIT = M.t("<C-\\><C-n>")
M.LUA_CALLBACK = "\x80\253g"
M.RIGHT = M.t("<right>")
M.LEFT = M.t("<left>")
M.CMD = "\x80\253h"

---@generic F: fun()
Expand Down Expand Up @@ -325,6 +327,9 @@ function M.debug(data)
if not fd then
error(("Could not open file %s for writing"):format(file))
end
if type(data) ~= "string" then
data = vim.inspect(data)
end
fd:write(data .. "\n")
fd:close()
end
Expand Down

0 comments on commit 8d924eb

Please sign in to comment.