perf(Mode): use dedicated virtual cursor namespace

This commit is contained in:
Iron-E 2024-03-12 12:45:18 -04:00
parent 3cb0a3817f
commit 4801f29bc6
No known key found for this signature in database
GPG Key ID: 569E791B76A42A1A

View File

@ -11,7 +11,6 @@ local utils = require 'libmodal.utils' --- @type libmodal.utils
--- @field private mappings libmodal.collections.ParseTable --- @field private mappings libmodal.collections.ParseTable
--- @field private modeline string[][] --- @field private modeline string[][]
--- @field private name string --- @field private name string
--- @field private ns number the namespace where cursor highlights are drawn on
--- @field private popups libmodal.collections.Stack --- @field private popups libmodal.collections.Stack
--- @field private supress_exit boolean --- @field private supress_exit boolean
--- @field public count libmodal.utils.Var[number] --- @field public count libmodal.utils.Var[number]
@ -27,6 +26,12 @@ local TIMEOUT =
} }
TIMEOUT.CHAR_NUMBER = TIMEOUT.CHAR:byte() TIMEOUT.CHAR_NUMBER = TIMEOUT.CHAR:byte()
--- Namespaces
local NS = {
--- The virtual cursor namespace. Used to workaround neovim/neovim#20793
CURSOR = vim.api.nvim_create_namespace('libmodal-mode-virtual_cursor'),
}
--- Byte for 0 --- Byte for 0
local ZERO = string.byte(0) local ZERO = string.byte(0)
@ -107,7 +112,7 @@ end
--- clears the virtual cursor from the screen --- clears the virtual cursor from the screen
--- @private --- @private
function Mode:clear_virt_cursor() function Mode:clear_virt_cursor()
vim.api.nvim_buf_clear_namespace(0, self.ns, 0, -1); vim.api.nvim_buf_clear_namespace(0, NS.CURSOR, 0, -1);
end end
--- enter this mode. --- enter this mode.
@ -122,7 +127,7 @@ function Mode:enter()
self.count:set(0) self.count:set(0)
self.exit:set(false) self.exit:set(false)
--- HACK: https://github.com/neovim/neovim/issues/20793 --- HACK: neovim/neovim#20793
vim.api.nvim_command 'highlight Cursor blend=100' vim.api.nvim_command 'highlight Cursor blend=100'
vim.schedule(function() vim.opt.guicursor:append { 'a:Cursor/lCursor' } end) vim.schedule(function() vim.opt.guicursor:append { 'a:Cursor/lCursor' } end)
self:render_virt_cursor() self:render_virt_cursor()
@ -206,7 +211,7 @@ end
function Mode:render_virt_cursor() function Mode:render_virt_cursor()
local line_nr, col_nr = unpack(vim.api.nvim_win_get_cursor(0)) local line_nr, col_nr = unpack(vim.api.nvim_win_get_cursor(0))
line_nr = line_nr - 1 -- win_get_cursor returns +1 for our purpose line_nr = line_nr - 1 -- win_get_cursor returns +1 for our purpose
vim.highlight.range(0, self.ns, 'Cursor', { line_nr, col_nr }, { line_nr, col_nr + 1 }, {}) vim.highlight.range(0, NS.CURSOR, 'Cursor', { line_nr, col_nr }, { line_nr, col_nr + 1 }, {})
end end
--- show the mode indicator, if it is enabled --- show the mode indicator, if it is enabled
@ -241,7 +246,7 @@ function Mode:tear_down()
self.popups:pop():close() self.popups:pop():close()
end end
--- HACK: https://github.com/neovim/neovim/issues/20793 --- HACK: neovim/neovim#20793
self:clear_virt_cursor() self:clear_virt_cursor()
vim.schedule(function() vim.opt.guicursor:remove { 'a:Cursor/lCursor' } end) vim.schedule(function() vim.opt.guicursor:remove { 'a:Cursor/lCursor' } end)
vim.api.nvim_command 'highlight Cursor blend=0' vim.api.nvim_command 'highlight Cursor blend=0'
@ -272,7 +277,6 @@ function Mode.new(name, instruction, supress_exit)
input = utils.Var.new(name, 'input'), input = utils.Var.new(name, 'input'),
instruction = instruction, instruction = instruction,
name = name, name = name,
ns = vim.api.nvim_create_namespace('libmodal' .. name),
modeline = {{'-- ' .. name .. ' --', 'LibmodalPrompt'}}, modeline = {{'-- ' .. name .. ' --', 'LibmodalPrompt'}},
}, },
Mode Mode