perf(Mode): remove `InputBytes` class

pull/20/head
Iron-E 1 year ago
parent 69beec5e8e
commit 34b94a246d
No known key found for this signature in database
GPG Key ID: 83A6AEB40395D40D

@ -13,23 +13,16 @@ local utils = require 'libmodal/src/utils'
--- @field private help? libmodal.utils.Help --- @field private help? libmodal.utils.Help
--- @field private indicator libmodal.utils.Indicator --- @field private indicator libmodal.utils.Indicator
--- @field private input libmodal.utils.Vars --- @field private input libmodal.utils.Vars
--- @field private input_bytes? number[]
--- @field private instruction fun()|{[string]: fun()|string} --- @field private instruction fun()|{[string]: fun()|string}
--- @field private mappings libmodal.collections.ParseTable --- @field private mappings libmodal.collections.ParseTable
--- @field private name string --- @field private name string
--- @field private popups libmodal.collections.Stack --- @field private popups libmodal.collections.Stack
--- @field private show_name fun()
--- @field private supress_exit boolean --- @field private supress_exit boolean
--- @field private timeouts_enabled boolean --- @field private timeouts_enabled boolean
local Mode = utils.classes.new() local Mode = utils.classes.new()
local InputBytes = utils.classes.new(
{
clear = function(self)
for i, _ in ipairs(self) do
self[i] = nil
end
end
})
local HELP = '?' local HELP = '?'
local TIMEOUT = local TIMEOUT =
{ {
@ -70,7 +63,7 @@ function Mode:check_input_for_mapping()
self.help:show() self.help:show()
end end
self.input_bytes:clear() self.input_bytes = {}
elseif command_type == globals.TYPE_TBL and globals.is_true(self.timeouts_enabled) then -- the command was a table, meaning that it MIGHT match. elseif command_type == globals.TYPE_TBL and globals.is_true(self.timeouts_enabled) then -- the command was a table, meaning that it MIGHT match.
self.flush_input_timer:start( -- start the timer self.flush_input_timer:start( -- start the timer
TIMEOUT.LEN, 0, vim.schedule_wrap(function() TIMEOUT.LEN, 0, vim.schedule_wrap(function()
@ -81,14 +74,14 @@ function Mode:check_input_for_mapping()
self.execute_instruction(cmd[ParseTable.CR]) self.execute_instruction(cmd[ParseTable.CR])
end end
-- clear input -- clear input
self.input_bytes:clear() self.input_bytes = {}
self.popups:peek():refresh(self.input_bytes) self.popups:peek():refresh(self.input_bytes)
end) end)
) )
else -- the command was an actual vim command. else -- the command was an actual vim command.
--- @diagnostic disable-next-line:param-type-mismatch already checked `cmd` != `table` --- @diagnostic disable-next-line:param-type-mismatch already checked `cmd` != `table`
self.execute_instruction(cmd) self.execute_instruction(cmd)
self.input_bytes:clear() self.input_bytes = {}
end end
self.popups:peek():refresh(self.input_bytes) self.popups:peek():refresh(self.input_bytes)
@ -137,7 +130,7 @@ function Mode:get_user_input()
end end
-- echo the indicator. -- echo the indicator.
self:show_name() self.show_name()
-- capture input. -- capture input.
local user_input = vim.fn.getchar() local user_input = vim.fn.getchar()
@ -188,64 +181,63 @@ function Mode:tear_down()
utils.api.redraw() utils.api.redraw()
end end
return --- create a new mode.
{ --- @param name string the name of the mode.
--- create a new mode. --- @param instruction fun()|string|table a Lua function, keymap dictionary, Vimscript command.
--- @param name string the name of the mode. --- @return libmodal.Mode
--- @param instruction fun()|string|table a Lua function, keymap dictionary, Vimscript command. function Mode.new(name, instruction, supress_exit)
--- @return libmodal.Mode name = vim.trim(name)
new = function(name, instruction, supress_exit)
name = vim.trim(name) -- inherit the metatable.
local self = setmetatable(
-- inherit the metatable. {
local self = setmetatable( exit = utils.Vars.new('exit', name),
{ indicator = utils.Indicator.new('LibmodalPrompt', '-- ' .. name .. ' --'),
exit = utils.Vars.new('exit', name), input = utils.Vars.new('input', name),
indicator = utils.Indicator.new('LibmodalPrompt', '-- ' .. name .. ' --'), instruction = instruction,
input = utils.Vars.new('input', name), name = name,
instruction = instruction, },
name = name, Mode
}, )
Mode
) self.show_name = (not vim.o.showmode) and utils.api.redraw or function()
utils.api.redraw()
vim.api.nvim_command('echohl ' .. self.indicator.hl .. " | echon '" .. self.indicator.str .. "'")
vim.api.nvim_command 'echohl None'
end
self.show_name = (not vim.o.showmode) and utils.api.redraw or function() -- define the exit flag
utils.api.redraw() self.supress_exit = supress_exit or false
vim.api.nvim_command('echohl ' .. self.indicator.hl .. " | echon '" .. self.indicator.str .. "'") -- if the user provided keymaps
vim.api.nvim_command 'echohl None' if type(instruction) == globals.TYPE_TBL then
end -- create a timer to perform actions with.
self.flush_input_timer = vim.loop.new_timer()
-- define the exit flag -- determine if a default `Help` should be created.
self.supress_exit = supress_exit or false if not self.instruction[HELP] then
--- @diagnostic disable-next-line:param-type-mismatch we checked that `instruction` is a table above
-- if the user provided keymaps self.help = utils.Help.new(self.instruction, 'KEY MAP')
if type(instruction) == globals.TYPE_TBL then end
-- create a timer to perform actions with.
self.flush_input_timer = vim.loop.new_timer()
-- determine if a default `Help` should be created. self.input_bytes = {}
if not self.instruction[HELP] then
--- @diagnostic disable-next-line:param-type-mismatch we checked that `instruction` is a table above
self.help = utils.Help.new(self.instruction, 'KEY MAP')
end
self.input_bytes = setmetatable({}, InputBytes) -- build the parse tree.
--- @diagnostic disable-next-line:param-type-mismatch already checked `self.instruction` != `table`
self.mappings = ParseTable.new(self.instruction)
-- build the parse tree. -- create a table for mode-specific data.
--- @diagnostic disable-next-line:param-type-mismatch already checked `self.instruction` != `table` self.popups = require('libmodal/src/collections/Stack').new()
self.mappings = ParseTable.new(self.instruction)
-- create a table for mode-specific data. -- create a variable for whether or not timeouts are enabled.
self.popups = require('libmodal/src/collections/Stack').new() self.timeouts = utils.Vars.new('timeouts', self.name)
-- create a variable for whether or not timeouts are enabled. -- read the correct timeout variable.
self.timeouts = utils.Vars.new('timeouts', self.name) self.timeouts_enabled = self.timeouts:get() or vim.g.libmodalTimeouts
end
-- read the correct timeout variable. return self
self.timeouts_enabled = self.timeouts:get() or vim.g.libmodalTimeouts end
end
return self return Mode
end
}

Loading…
Cancel
Save