Refine prompts; completions can't exist until 0.5

pull/3/head release/0.1.0
Iron-E 4 years ago
parent 651f207d09
commit 1e121622f8
No known key found for this signature in database
GPG Key ID: 19B71B7B7B021D22

@ -1,14 +1,4 @@
let libmodal#_replacements = [ let s:winOpenOpts = {
\ '.', ':', '&', '@', ',', '\\/', '?',
\ '(', ')',
\ '{', '}',
\ '[', ']',
\ '+', '\\*', '\\!', '\\^', '\\>', '\\<', '%', '=',
\ '\\$',
\ '\\'
\]
let libmodal#_winOpenOpts = {
\ 'anchor' : 'SW', \ 'anchor' : 'SW',
\ 'col' : &columns - 1, \ 'col' : &columns - 1,
\ 'focusable': v:false, \ 'focusable': v:false,
@ -19,46 +9,6 @@ let libmodal#_winOpenOpts = {
\ 'width' : 25, \ 'width' : 25,
\} \}
" 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
function! libmodal#CreateCompletionsProvider(completions)
function! l:completionsProvider(argLead, cmdLine, cursorPos) abort closure
" replace conjoining characters with spaces.
let l:spacedArgLead = a:argLead
for l:replacement in s:replacements
let l:spacedArgLead = substitute(l:spacedArgLead, l:replacement, ' ', 'g')
endfor
" split the spaced version of `argLead`.
let l:splitArgLead = split(splitArgLead, ' ')
" make sure the user is in a position were this function
" will provide accurate completions.
if len(splitArgLead) > 1 | return v:none | end
" get the word selected by the user.
let l:word = l:splitArgLead[1]
" get all matches from the completions list.
let l:completions = []
for l:completion in s:completions
if stridx(l:completion, l:word) > -1
let l:completions = add(l:completions, l:completion)
endif
endfor
return l:completions
endfunction
return funcref('l:completionsProvider')
endfunction
" PLACEHOLDER. " PLACEHOLDER.
function! libmodal#Enter(...) abort function! libmodal#Enter(...) abort
echo '' echo ''
@ -79,5 +29,5 @@ endfunction
" RETURNS: " RETURNS:
" * A window handle. " * A window handle.
function! libmodal#WinOpen(bufHandle) abort function! libmodal#WinOpen(bufHandle) abort
return nvim_open_win(a:bufHandle, 0, libmodal#_winOpenOpts) return nvim_open_win(a:bufHandle, 0, s:winOpenOpts)
endfunction endfunction

@ -1,14 +1,15 @@
local libmodal = require('libmodal') local libmodal = require('libmodal')
local api = vim.api
local commandList = {'new', 'close', 'last'} local commandList = {'new', 'close', 'last'}
function barMode() function barMode()
local uinput = vim.api.nvim_get_var('tabModeInput') local uinput = vim.api.nvim_get_var('barModeInput')
if uinput == 'new' then if uinput == 'new' then
execute 'tabnew' api.nvim_command('tabnew')
elseif uinput == 'close' then elseif uinput == 'close' then
execute 'tabclose' api.nvim_command('tabclose')
elseif uinput == 'last' then elseif uinput == 'last' then
execute 'tablast' api.nvim_command('tablast')
end end
end end

@ -6,7 +6,7 @@ function barMode()
) )
if uinput == '' then if uinput == '' then
vim.api.nvim_command("echo 'You cant leave using <Esc>.'") vim.api.nvim_command("echom 'You cant leave using <Esc>.'")
elseif uinput == 'q' then elseif uinput == 'q' then
vim.api.nvim_set_var('barModeExit', true) vim.api.nvim_set_var('barModeExit', true)
end end

@ -12,6 +12,7 @@ local globals = {}
*/ */
--]] --]]
globals.DEFAULT_ERROR_MESSAGE = 'vim-libmodal error'
globals.ESC_NR = 27 globals.ESC_NR = 27
globals.TYPE_FUNC = 'function' globals.TYPE_FUNC = 'function'
globals.TYPE_NUM = 'number' globals.TYPE_NUM = 'number'

@ -147,9 +147,7 @@ local function _initCombos(modeName, comboTable)
-- create a floating window -- create a floating window
local buf = api.nvim_create_buf(false, true) local buf = api.nvim_create_buf(false, true)
vars.buffers.instances[modeName] = buf vars.buffers.instances[modeName] = buf
vars.windows.instances[modeName] = api.nvim_eval( vars.windows.instances[modeName] = api.nvim_call_function('libmodal#WinOpen', {buf})
'libmodal#WinOpen(' .. buf .. ')'
)
-- Build the parse tree. -- Build the parse tree.
vars.combos.instances[modeName] = mode.ParseTable.new(comboTable) vars.combos.instances[modeName] = mode.ParseTable.new(comboTable)

@ -24,6 +24,52 @@ local prompt = {}
*/ */
--]] --]]
-------------------------------------------------------
--[[ 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)
-- replace conjoining characters with spaces.
local spacedArgLead = argLead
for _, v in ipairs(_replacements) do
spacedArgLead = string.gsub(spacedArgLead, v, ' ')
end
-- split the spaced version of `argLead`.
local splitArgLead = utils.strings.split(splitArgLead, ' ')
-- 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.
local word = splitArgLead[1]
-- get all matches from the completions list.
local matches = {}
local i = 1
for _, v in ipairs(completions) do
if string.match(word, completion) then
matches[i] = completion
i = i + 1
end
end
return matches
end
end
-------------------------- --------------------------
--[[ SUMMARY: --[[ SUMMARY:
@ -69,24 +115,29 @@ function prompt.enter(...)
api.nvim_redraw() api.nvim_redraw()
-- compose prompt command -- compose prompt command
local cmd = "input('" .. indicator .. "', ''" --[[ TODO: completions won't work until neovim 0.5
if completions then cmd = look into v:lua when it drops.
cmd .. ", 'customlist,funcref(\"libmodal#CreateCompletionsProvider\", ["
.. completions .. local cmd = "input('" .. indicator .. "', ''"
"])'" if completions then cmd =
end cmd .. ", 'customlist,funcref(\"libmodal#CreateCompletionsProvider\", ["
.. completions ..
"])'"
end
-- get input from prompt
local uinput = api.nvim_eval(cmd .. ')') -- closing bracket ends beginning 'input('
--]]
-- get input from prompt local uinput = api.nvim_call_function('input', {indicator})
local uinput = api.nvim_eval(cmd .. ')') -- closing bracket ends beginning 'input('
-- if a:2 is a function then call it. -- if a:2 is a function then call it.
if string.len(uinput) > 0 then if string.len(uinput) > 0 then
vars.nvim_set(vars.input, modeName, uinput) vars.nvim_set(vars.input, modeName, uinput)
if completions then if type(args[2]) == globals.TYPE_TBL then
if args[2][uinput] then if args[2][uinput] then
api.nvim_command(args[2][uinput]) api.nvim_command(args[2][uinput])
else else
api.nvim_show_err('Unknown command.') api.nvim_show_err(globals.DEFAULT_ERROR_MESSAGE, 'Unknown command')
end end
else else
args[2]() args[2]()

@ -36,7 +36,7 @@ function api.nvim_echo(str)
api.nvim_command("echo " .. tostring(str)) api.nvim_command("echo " .. tostring(str))
end end
----------------------------------- ------------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Check whether or not some variable exists. * Check whether or not some variable exists.
]] ]]
@ -44,9 +44,9 @@ end
* `scope` => The scope of the variable (i.e. `g`, `l`, etc.) * `scope` => The scope of the variable (i.e. `g`, `l`, etc.)
* `var` => the variable to check for. * `var` => the variable to check for.
]] ]]
----------------------------------- ------------------------------------
function api.nvim_exists(scope, var) function api.nvim_exists(scope, var)
return api.nvim_eval("exists('" .. scope .. ":" .. var .. "')") == globals.VIM_TRUE return api.nvim_call_function('exists', {scope .. ':' .. var}) == globals.VIM_TRUE
end end
------------------------- -------------------------
@ -68,15 +68,13 @@ end
'"endwhile"' '"endwhile"'
} }
return tonumber(vim.api.nvim_eval( return tonumber(vim.api.nvim_call_function("execute",cmd))
"execute([" .. table.concat(cmd, ',') .. "])"
))
``` ```
However, I'm not sure if it would accidentally affect text. However, I'm not sure if it would accidentally affect text.
]] ]]
------------------------- -------------------------
function api.nvim_input() function api.nvim_input()
return api.nvim_eval('getchar()') return api.nvim_call_function('getchar', {})
end end
--------------------------------- ---------------------------------
@ -91,11 +89,11 @@ end
local resetHighlight = Entry.new('None', '') local resetHighlight = Entry.new('None', '')
function api.nvim_lecho(hlTables) function api.nvim_lecho(hlTables)
api.nvim_redraw() api.nvim_redraw()
table.insert(hlTables, resetHighlight) hlTables[#hlTables+1] = resetHighlight
for _, hlTable in ipairs(hlTables) do for _, hlTable in ipairs(hlTables) do
api.nvim_command( api.nvim_command(
-- `:echohl` the hlgroup and then `:echon` the string. -- `:echohl` the hlgroup and then `:echon` the string.
"echohl " .. hlTable.hl .. " | echon '" .. hlTable.str .. "'" "echohl " .. tostring(hlTable.hl) .. " | echon '" .. tostring(hlTable.str) .. "'"
) )
end end
end end
@ -119,13 +117,14 @@ end
* `msg` => the message of the error. * `msg` => the message of the error.
]] ]]
-------------------------------------- --------------------------------------
local returnEntry = Entry.new('Question', '\nPress any key to return.')
function api.nvim_show_err(title, msg) function api.nvim_show_err(title, msg)
api.nvim_lecho({ api.nvim_lecho({
Entry.new('Title', title .. '\n'), Entry.new('Title', tostring(title) .. '\n'),
Entry.new('Error', msg), Entry.new('Error', tostring(msg)),
Entry.new('Question', '\n[Press any key to return]') returnEntry
}) })
api.nvim_command('call getchar()') api.nvim_call_function('getchar', {})
end end
--[[ --[[

@ -1,3 +1,11 @@
--[[
/*
* IMPORTS
*/
--]]
local globals = require('libmodal/src/base/globals')
--[[ --[[
/* /*
* MODULE * MODULE
@ -20,12 +28,12 @@ utils.WindowState = require('libmodal/src/utils/WindowState')
function utils.showError(pcallErr) function utils.showError(pcallErr)
utils.api.nvim_bell() utils.api.nvim_bell()
utils.api.nvim_show_err( 'vim-libmodal error', utils.api.nvim_show_err(globals.DEFAULT_ERROR_MESSAGE,
utils.api.nvim_get_vvar('throwpoint') utils.api.nvim_get_vvar('throwpoint')
.. '\n' .. .. '\n' ..
utils.api.nvim_get_vvar('exception') utils.api.nvim_get_vvar('exception')
.. '\n' .. .. '\n' ..
pcallErr tostring(pcallErr)
) )
end end

Loading…
Cancel
Save