diff --git a/lua/libmodal/src/Mode.lua b/lua/libmodal/src/Mode.lua index 7b46624..b8bca49 100644 --- a/lua/libmodal/src/Mode.lua +++ b/lua/libmodal/src/Mode.lua @@ -49,6 +49,22 @@ local _metaInputBytes = classes.new(nil, { classes = nil +----------------------------------------------------------- +--[[ SUMMARY: + * Execute some `proposedInstruction` according to a set of determined logic. +]] +--[[ PARAMS: + * `proposedInstruction` => The instruction that is desired to be executed. +]] +----------------------------------------------------------- +function _metaMode._executeInstruction(proposedInstruction) + if type(proposedInstruction) == globals.TYPE_FUNC then + proposedInstruction() + else + api.nvim_command(proposedInstruction) + end +end + ----------------------------------------------- --[[ SUMMARY: * Parse `self.mappings` and see if there is any command to execute. @@ -79,8 +95,6 @@ function _metaMode:_checkInputForMapping() elseif commandType == globals.TYPE_TBL and globals.is_true(self._timeouts.enabled) then - -- Create a new timer - -- start the timer self._flushInputTimer:start( _TIMEOUT.LEN, 0, vim.schedule_wrap(function() @@ -88,7 +102,7 @@ function _metaMode:_checkInputForMapping() _TIMEOUT:SEND() -- if there is a command, execute it. if cmd[ParseTable.CR] then - api.nvim_command(cmd[ParseTable.CR]) + self._executeInstruction(cmd[ParseTable.CR]) end -- clear input inputBytes:clear() @@ -98,7 +112,7 @@ function _metaMode:_checkInputForMapping() -- The command was an actual vim command. else - api.nvim_command(cmd) + self._executeInstruction(cmd) inputBytes:clear() end diff --git a/lua/libmodal/src/Prompt.lua b/lua/libmodal/src/Prompt.lua index 96cfd15..ba7d45d 100644 --- a/lua/libmodal/src/Prompt.lua +++ b/lua/libmodal/src/Prompt.lua @@ -36,6 +36,38 @@ end local _metaPrompt = require('libmodal/src/classes').new(Prompt.TYPE) +--------------------------------------------------- +--[[ SUMMARY: + * Execute the specified instruction based on user input. +]] +--[[ PARAMS: + * `userInput` => the input from the user, used to determine how to execute the `self._instruction`. +]] +--------------------------------------------------- +function _metaPrompt:_executeInstruction(userInput) + -- get the instruction for the mode. + local instruction = self._instruction + + if type(instruction) == globals.TYPE_TBL then -- The instruction is a command table. + if instruction[userInput] then -- There is a defined command for the input. + local to_execute = instruction[userInput] + if type(to_execute) == globals.TYPE_FUNC then + to_execute() + else + api.nvim_command(instruction[userInput]) + end + elseif userInput == _HELP then -- The user did not define a 'help' command, so use the default. + self._help:show() + else -- show an error. + utils.api.nvim_show_err(globals.DEFAULT_ERROR_TITLE, 'Unknown command') + end + elseif type(instruction) == globals.TYPE_STR and vim.fn then -- The instruction is a function. Works on Neovim 0.5+. + vim.fn[instruction]() + else -- attempt to call the instruction. + instruction() + end +end + --------------------------------- --[[ SUMMARY: * Loop to get user input with `input()`. @@ -61,25 +93,10 @@ function _metaPrompt:_inputLoop() api.nvim_call_function('input', {self.indicator}) end - -- get the instruction for the mode. - local instruction = self._instruction - -- determine what to do with the input if string.len(userInput) > 0 then -- The user actually entered something. self.input:nvimSet(userInput) - if type(instruction) == globals.TYPE_TBL then -- The instruction is a command table. - if instruction[userInput] then -- There is a defined command for the input. - api.nvim_command(instruction[userInput]) - elseif userInput == _HELP then -- The user did not define a 'help' command, so use the default. - self._help:show() - else -- show an error. - utils.api.nvim_show_err(globals.DEFAULT_ERROR_TITLE, 'Unknown command') - end - elseif type(instruction) == globals.TYPE_STR and vim.fn then -- The instruction is a function. Works on Neovim 0.5+. - vim.fn[instruction]() - else -- attempt to call the instruction. - instruction() - end + self:_executeInstruction(userInput) else -- indicate we want to leave the prompt return false end