Skip to content

Commit 34b94a2

Browse files
committed
perf(Mode): remove InputBytes class
1 parent 69beec5 commit 34b94a2

File tree

1 file changed

+56
-64
lines changed

1 file changed

+56
-64
lines changed

lua/libmodal/src/Mode.lua

Lines changed: 56 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,16 @@ local utils = require 'libmodal/src/utils'
1313
--- @field private help? libmodal.utils.Help
1414
--- @field private indicator libmodal.utils.Indicator
1515
--- @field private input libmodal.utils.Vars
16+
--- @field private input_bytes? number[]
1617
--- @field private instruction fun()|{[string]: fun()|string}
1718
--- @field private mappings libmodal.collections.ParseTable
1819
--- @field private name string
1920
--- @field private popups libmodal.collections.Stack
21+
--- @field private show_name fun()
2022
--- @field private supress_exit boolean
2123
--- @field private timeouts_enabled boolean
2224
local Mode = utils.classes.new()
2325

24-
local InputBytes = utils.classes.new(
25-
{
26-
clear = function(self)
27-
for i, _ in ipairs(self) do
28-
self[i] = nil
29-
end
30-
end
31-
})
32-
3326
local HELP = '?'
3427
local TIMEOUT =
3528
{
@@ -70,7 +63,7 @@ function Mode:check_input_for_mapping()
7063
self.help:show()
7164
end
7265

73-
self.input_bytes:clear()
66+
self.input_bytes = {}
7467
elseif command_type == globals.TYPE_TBL and globals.is_true(self.timeouts_enabled) then -- the command was a table, meaning that it MIGHT match.
7568
self.flush_input_timer:start( -- start the timer
7669
TIMEOUT.LEN, 0, vim.schedule_wrap(function()
@@ -81,14 +74,14 @@ function Mode:check_input_for_mapping()
8174
self.execute_instruction(cmd[ParseTable.CR])
8275
end
8376
-- clear input
84-
self.input_bytes:clear()
77+
self.input_bytes = {}
8578
self.popups:peek():refresh(self.input_bytes)
8679
end)
8780
)
8881
else -- the command was an actual vim command.
8982
--- @diagnostic disable-next-line:param-type-mismatch already checked `cmd` != `table`
9083
self.execute_instruction(cmd)
91-
self.input_bytes:clear()
84+
self.input_bytes = {}
9285
end
9386

9487
self.popups:peek():refresh(self.input_bytes)
@@ -137,7 +130,7 @@ function Mode:get_user_input()
137130
end
138131

139132
-- echo the indicator.
140-
self:show_name()
133+
self.show_name()
141134

142135
-- capture input.
143136
local user_input = vim.fn.getchar()
@@ -188,64 +181,63 @@ function Mode:tear_down()
188181
utils.api.redraw()
189182
end
190183

191-
return
192-
{
193-
--- create a new mode.
194-
--- @param name string the name of the mode.
195-
--- @param instruction fun()|string|table a Lua function, keymap dictionary, Vimscript command.
196-
--- @return libmodal.Mode
197-
new = function(name, instruction, supress_exit)
198-
name = vim.trim(name)
199-
200-
-- inherit the metatable.
201-
local self = setmetatable(
202-
{
203-
exit = utils.Vars.new('exit', name),
204-
indicator = utils.Indicator.new('LibmodalPrompt', '-- ' .. name .. ' --'),
205-
input = utils.Vars.new('input', name),
206-
instruction = instruction,
207-
name = name,
208-
},
209-
Mode
210-
)
184+
--- create a new mode.
185+
--- @param name string the name of the mode.
186+
--- @param instruction fun()|string|table a Lua function, keymap dictionary, Vimscript command.
187+
--- @return libmodal.Mode
188+
function Mode.new(name, instruction, supress_exit)
189+
name = vim.trim(name)
190+
191+
-- inherit the metatable.
192+
local self = setmetatable(
193+
{
194+
exit = utils.Vars.new('exit', name),
195+
indicator = utils.Indicator.new('LibmodalPrompt', '-- ' .. name .. ' --'),
196+
input = utils.Vars.new('input', name),
197+
instruction = instruction,
198+
name = name,
199+
},
200+
Mode
201+
)
202+
203+
self.show_name = (not vim.o.showmode) and utils.api.redraw or function()
204+
utils.api.redraw()
205+
206+
vim.api.nvim_command('echohl ' .. self.indicator.hl .. " | echon '" .. self.indicator.str .. "'")
207+
vim.api.nvim_command 'echohl None'
208+
end
211209

212-
self.show_name = (not vim.o.showmode) and utils.api.redraw or function()
213-
utils.api.redraw()
210+
-- define the exit flag
211+
self.supress_exit = supress_exit or false
214212

215-
vim.api.nvim_command('echohl ' .. self.indicator.hl .. " | echon '" .. self.indicator.str .. "'")
216-
vim.api.nvim_command 'echohl None'
217-
end
213+
-- if the user provided keymaps
214+
if type(instruction) == globals.TYPE_TBL then
215+
-- create a timer to perform actions with.
216+
self.flush_input_timer = vim.loop.new_timer()
218217

219-
-- define the exit flag
220-
self.supress_exit = supress_exit or false
221-
222-
-- if the user provided keymaps
223-
if type(instruction) == globals.TYPE_TBL then
224-
-- create a timer to perform actions with.
225-
self.flush_input_timer = vim.loop.new_timer()
218+
-- determine if a default `Help` should be created.
219+
if not self.instruction[HELP] then
220+
--- @diagnostic disable-next-line:param-type-mismatch we checked that `instruction` is a table above
221+
self.help = utils.Help.new(self.instruction, 'KEY MAP')
222+
end
226223

227-
-- determine if a default `Help` should be created.
228-
if not self.instruction[HELP] then
229-
--- @diagnostic disable-next-line:param-type-mismatch we checked that `instruction` is a table above
230-
self.help = utils.Help.new(self.instruction, 'KEY MAP')
231-
end
224+
self.input_bytes = {}
232225

233-
self.input_bytes = setmetatable({}, InputBytes)
226+
-- build the parse tree.
227+
--- @diagnostic disable-next-line:param-type-mismatch already checked `self.instruction` != `table`
228+
self.mappings = ParseTable.new(self.instruction)
234229

235-
-- build the parse tree.
236-
--- @diagnostic disable-next-line:param-type-mismatch already checked `self.instruction` != `table`
237-
self.mappings = ParseTable.new(self.instruction)
230+
-- create a table for mode-specific data.
231+
self.popups = require('libmodal/src/collections/Stack').new()
238232

239-
-- create a table for mode-specific data.
240-
self.popups = require('libmodal/src/collections/Stack').new()
233+
-- create a variable for whether or not timeouts are enabled.
234+
self.timeouts = utils.Vars.new('timeouts', self.name)
241235

242-
-- create a variable for whether or not timeouts are enabled.
243-
self.timeouts = utils.Vars.new('timeouts', self.name)
236+
-- read the correct timeout variable.
237+
self.timeouts_enabled = self.timeouts:get() or vim.g.libmodalTimeouts
238+
end
244239

245-
-- read the correct timeout variable.
246-
self.timeouts_enabled = self.timeouts:get() or vim.g.libmodalTimeouts
247-
end
240+
return self
241+
end
248242

249-
return self
250-
end
251-
}
243+
return Mode

0 commit comments

Comments
 (0)