Various fixes

pull/3/head
Iron-E 4 years ago
parent 523261dcbc
commit 45aa9af26e
No known key found for this signature in database
GPG Key ID: 19B71B7B7B021D22

@ -21,7 +21,7 @@ function! libmodal#_inputWith(indicator, completions)
" return the closure that was generated using the completions from lua.
function! LibmodalCompletionsProvider(argLead, cmdLine, cursorPos) abort closure
return luaeval(
\ 'require("libmodal/src/prompt/")._createCompletionsProvider(_A[1])(_A[2], _A[3], _A[4])',
\ 'require("libmodal/src/Prompt/").createCompletionsProvider(_A[1])(_A[2], _A[3], _A[4])',
\ [a:completions, a:argLead, a:cmdLine, a:cursorPos]
\)
endfunction

@ -4,10 +4,11 @@
*/
--]]
local globals = require('libmodal/src/globals')
local utils = require('libmodal/src/utils')
local Stack = require('libmodal/src/collections/Stack')
local Vars = require('libmodal/src/Vars')
local globals = require('libmodal/src/globals')
local Indicator = require('libmodal/src/Indicator')
local Stack = require('libmodal/src/collections/Stack')
local utils = require('libmodal/src/utils')
local Vars = require('libmodal/src/Vars')
local api = utils.api
@ -19,7 +20,7 @@ local api = utils.api
local Mode = {}
Mode.ParseTable = require('libmodal/src/mode/ParseTable')
Mode.ParseTable = require('libmodal/src/Mode/ParseTable')
Mode.Popup = require('libmodal/src/Mode/Popup')
local _HELP = '?'
@ -276,7 +277,7 @@ function Mode.new(name, instruction, ...)
end)(unpack({...}))
-- Define other "session" variables.
self._indicator = utils.Indicator.mode(name)
self._indicator = Indicator.mode(name)
self._instruction = instruction
self._name = name
self._winState = utils.WindowState.new()

@ -4,10 +4,11 @@
*/
--]]
local globals = require('libmodal/src/globals')
local Stack = require('libmodal/src/collections/Stack')
local utils = require('libmodal/src/utils')
local Vars = require('libmodal/src/Vars')
local globals = require('libmodal/src/globals')
local Indicator = require('libmodal/src/Indicator')
local Stack = require('libmodal/src/collections/Stack')
local utils = require('libmodal/src/utils')
local Vars = require('libmodal/src/Vars')
local api = utils.api
@ -162,7 +163,7 @@ function Prompt.new(name, instruction, ...)
self = {}
setmetatable(self, _metaPrompt)
self._indicator = utils.indicator.prompt(name)
self._indicator = Indicator.prompt(name)
self._input = vars.new('input')
self._instruction = instruction
self._name = name

@ -13,10 +13,11 @@ local api = vim.api
*/
--]]
local Vars = {}
Vars.libmodalTimeouts = api.nvim_get_var('libmodalTimeouts')
local _TIMEOUT_GLOBAL_NAME = 'libmodalTimeouts'
local Vars = {
[_TIMEOUT_GLOBAL_NAME] = api.nvim_get_var(_TIMEOUT_GLOBAL_NAME)
}
--[[
/*

@ -23,6 +23,7 @@ mode.ParseTable = Mode.ParseTable
--]]
------------------------
--[[ DEPRECATED. ]]
--[[ SUMMARY:
* Enter a mode.
]]

@ -0,0 +1,38 @@
--[[
/*
* IMPORTS
*/
--]]
local Prompt = require('libmodal/src/Prompt')
--[[
/*
* MODULE
*/
--]]
local prompt = {}
--------------------------
--[[ DEPRECATED. ]]
--[[ SUMMARY:
* Enter a prompt.
]]
--[[ PARAMS:
* `args[1]` => the prompt name.
* `args[2]` => the prompt callback, or mode command table.
* `args[3]` => a completions table.
]]
--------------------------
function prompt.enter(name, instruction, ...)
Prompt.new(name, instruction, ...):enter()
end
--[[
/*
* PUBLICIZE MODULE
*/
--]]
return prompt

@ -7,9 +7,9 @@
local libmodal = {}
libmodal.globals = require('libmodal/src/globals')
libmodal.mode = require('libmodal/src/mode')
libmodal.mode = require('libmodal/src/deprecated/mode')
libmodal.Mode = require('libmodal/src/Mode')
libmodal.prompt = require('libmodal/src/prompt')
libmodal.prompt = require('libmodal/src/deprecated/prompt')
libmodal.Prompt = require('libmodal/src/Prompt')
libmodal.utils = require('libmodal/src/utils')

@ -1,177 +0,0 @@
--[[
/*
* IMPORTS
*/
--]]
local globals = require('libmodal/src/globals')
local utils = require('libmodal/src/utils')
local api = utils.api
local vars = utils.vars
--[[
/*
* MODULE
*/
--]]
local prompt = {}
--[[
/*
* LIB `prompt`
*/
--]]
local _HELP = 'help'
local _replacements = {
'(', ')', '[', ']', '{', '}',
'=', '+', '<', '>', '^',
',', '/', ':', '?', '@', '!', '$', '*', '.', '%', '&', '\\',
}
-------------------------------------------------------
--[[ SUMMARY:
* Provide completions for a `libmodal.prompt`.
]]
--[[ PARAMS:
* `completions` => the list of completions.
]]
--[[ RETURNS:
* A function that accepts:
* `argLead` => the current line being edited, stops at the cursor.
* `cmdLine` => the current line being edited
* `cursorPos` => the position of the cursor
* Used for `input()` VimL.
]]
-------------------------------------------------------
function prompt._createCompletionsProvider(completions)
return function(argLead, cmdLine, cursorPos)
if string.len(cmdLine) < 1 then
return completions
end
-- replace conjoining characters with spaces.
local spacedArgLead = argLead
for _, v in ipairs(_replacements) do
spacedArgLead, _ = string.gsub(spacedArgLead, vim.pesc(v), ' ')
end
-- split the spaced version of `argLead`.
local splitArgLead = vim.split(spacedArgLead, ' ', true)
-- make sure the user is in a position were this function
-- will provide accurate completions.
if #splitArgLead > 1 then
return nil
end
-- get the word selected by the user. (don't compare case)
local word = string.upper(splitArgLead[1])
-- get all matches from the completions list.
local matches = {}
for _, v in ipairs(completions) do
-- test if `word` is inside of `completions`:`v`, ignoring case.
if string.match(vim.pesc(string.upper(v)), word) then
matches[#matches + 1] = v -- preserve case when providing completions.
end
end
return matches
end
end
--------------------------
--[[ SUMMARY:
* Enter a prompt.
]]
--[[ PARAMS:
* `args[1]` => the prompt name.
* `args[2]` => the prompt callback, or mode command table.
* `args[3]` => a completions table.
]]
--------------------------
function prompt.enter(...)
-- get the arguments
local args = {...}
-- create an indicator
local indicator = utils.Indicator.prompt(args[1])
-- lowercase of the passed mode name.
local modeName = string.lower(args[1])
-- get the completion list.
local completions = nil
if type(args[2]) == globals.TYPE_TBL then -- unload the keys of the mode command table.
completions = {}
local containedHelp = false
for k, _ in pairs(args[2]) do
completions[#completions + 1] = k
if k == _HELP then containedHelp = true
end
end
if not containedHelp then -- assign it.
completions[#completions + 1] = _HELP
vars.help.instances[modeName] = utils.Help.new(args[2], 'COMMAND')
end
elseif #args > 2 then -- assign completions as the custom completions table provided.
completions = args[3]
end
-- enter the mode using a loop.
local continueMode = true
while continueMode == true do
local noErrors, err = pcall(function()
-- clear previous `echo`s.
api.nvim_redraw()
-- get user input based on `args[2]`.
local userInput = ''
if completions then userInput =
api.nvim_call_function('libmodal#_inputWith', {indicator, completions})
else userInput =
api.nvim_call_function('input', {indicator})
end
-- if a:2 is a function then call it.
if string.len(userInput) > 0 then
vars.nvim_set(vars.input, modeName, userInput)
if type(args[2]) == globals.TYPE_TBL then
if args[2][userInput] then -- there is a defined command for the input.
api.nvim_command(args[2][userInput])
elseif userInput == _HELP then -- the user did not define a 'help' command, so use the default.
vars.help.instances[modeName]:show()
else -- show an error.
api.nvim_show_err(globals.DEFAULT_ERROR_TITLE, 'Unknown command')
end
else
args[2]()
end
else
continueMode = false
end
end)
-- if there were errors.
if noErrors == false then
utils.showError(err)
continueMode = false
end
end
-- delete temporary variables created for this mode.
vars:tearDown(modeName)
end
--[[
/*
* PUBLICIZE MODULE
*/
--]]
return prompt
Loading…
Cancel
Save