From 1933c1d6dc0c6c6146931dcd8f9644da9fc6fef1 Mon Sep 17 00:00:00 2001 From: Iron-E Date: Wed, 16 Feb 2022 11:07:44 -0500 Subject: [PATCH] perf: use `vim.ui` instead of `vim.fn` --- autoload/libmodal.vim | 18 ------------------ lua/libmodal/src/Prompt.lua | 32 ++++++++++++++++---------------- 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/autoload/libmodal.vim b/autoload/libmodal.vim index 457ceca..3ca8f84 100644 --- a/autoload/libmodal.vim +++ b/autoload/libmodal.vim @@ -1,21 +1,3 @@ -" SUMMARY: -" * Get user input with some `completions`. -" PARAMS: -" * `indicator` => the prompt string. -" * `completions` => the list of completions to provide. -" RETURNS: -" * Input from `input()`. -function! libmodal#_inputWith(indicator, completions) - function! LibmodalCompletionsProvider(argLead, cmdLine, cursorPos) abort closure - return luaeval( - \ 'require("libmodal/src/Prompt").createCompletionsProvider(_A[1])(_A[2], _A[3], _A[4])', - \ [a:completions, a:argLead, a:cmdLine, a:cursorPos] - \) - endfunction - - return input(a:indicator, '', 'customlist,LibmodalCompletionsProvider') -endfunction - " SUMMARY: " * Runs the nvim-libmodal command prompt loop. The function takes an optional " argument specifying how many times to run (runs until exiting by default). diff --git a/lua/libmodal/src/Prompt.lua b/lua/libmodal/src/Prompt.lua index 00b1195..0e70ba1 100644 --- a/lua/libmodal/src/Prompt.lua +++ b/lua/libmodal/src/Prompt.lua @@ -72,28 +72,28 @@ function _metaPrompt:_inputLoop() -- clear previous `echo`s. utils.api.nvim_redraw() - -- define a placeholder for user input - local userInput = '' + -- determine what to do with the input + local function userInputCallback(userInput) + if userInput and string.len(userInput) > 0 then -- The user actually entered something. + self.input:nvimSet(userInput) + self:_executeInstruction(userInput) + else -- indicate we want to leave the prompt + return false + end + + return true + end -- echo the highlighting vim.api.nvim_command('echohl ' .. self.indicator.hl) -- set the user input variable - if self._completions then userInput = - vim.fn['libmodal#_inputWith'](self.indicator.str, self._completions) - else userInput = - vim.fn.input(self.indicator) + if self._completions then + vim.api.nvim_command('echo "' .. self.indicator.str .. '"') + return vim.ui.select(self._completions, {}, userInputCallback) + else + return vim.ui.input({prompt = self.indicator.str}, userInputCallback) end - - -- determine what to do with the input - if string.len(userInput) > 0 then -- The user actually entered something. - self.input:nvimSet(userInput) - self:_executeInstruction(userInput) - else -- indicate we want to leave the prompt - return false - end - - return true end ----------------------------