Finish intial implemention of prompts

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

@ -9,14 +9,25 @@ let s:winOpenOpts = {
\ 'width' : 25,
\}
" PLACEHOLDER.
function! libmodal#Enter(...) abort
echo ''
endfunction
" 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)
" TODO: 0.5 — return input(a:indicator, '', 'customlist,v:lua.require("libmodal/src/prompt")…')
" 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])',
\ [a:completions, a:argLead, a:cmdLine, a:cursorPos]
\)
endfunction
" PLACEHOLDER.
function! libmodal#Prompt(...) abort
echo ''
echohl LibmodalStar
return input(a:indicator, '', 'customlist,LibmodalCompletionsProvider')
endfunction
" SUMMARY:
@ -28,6 +39,6 @@ endfunction
" * `bufHandle` => the buffer to spawn the window for.
" RETURNS:
" * A window handle.
function! libmodal#WinOpen(bufHandle) abort
function! libmodal#_winOpen(bufHandle) abort
return nvim_open_win(a:bufHandle, 0, s:winOpenOpts)
endfunction

@ -6,7 +6,6 @@
local api = vim.api
local globals = require('libmodal/src/base/globals')
local strings = require('libmodal/src/utils/strings')
--[[
/*
@ -149,8 +148,8 @@ function ParseTable.new(userTable)
end -- ‡
-- Run the recursive function.
update(self, strings.split(
string.reverse(key), '.'
update(self, vim.split(
string.reverse(key), '.', false
))
end

@ -147,7 +147,7 @@ local function _initCombos(modeName, comboTable)
-- create a floating window
local buf = api.nvim_create_buf(false, true)
vars.buffers.instances[modeName] = buf
vars.windows.instances[modeName] = api.nvim_call_function('libmodal#WinOpen', {buf})
vars.windows.instances[modeName] = api.nvim_call_function('libmodal#_winOpen', {buf})
-- Build the parse tree.
vars.combos.instances[modeName] = mode.ParseTable.new(comboTable)
@ -172,11 +172,7 @@ local function _modeEnterTeardown(modeName, winState)
)
end
for _, v in pairs(vars) do
if type(v) == globals.TYPE_TBL and v.instances[modeName] then
v.instances[modeName] = nil
end
end
vars:tearDown(modeName)
api.nvim_command("mode | echo '' | call garbagecollect()")
winState:restore()
end

@ -24,6 +24,12 @@ local prompt = {}
*/
--]]
local _replacements = {
'(', ')', '[', ']', '{', '}',
'=', '+', '<', '>', '^',
',', '/', ':', '?', '@', '!', '$', '*', '.', '%', '&', '\\',
}
-------------------------------------------------------
--[[ SUMMARY:
* Provide completions for a `libmodal.prompt`.
@ -44,15 +50,17 @@ function prompt._createCompletionsProvider(completions)
-- replace conjoining characters with spaces.
local spacedArgLead = argLead
for _, v in ipairs(_replacements) do
spacedArgLead = string.gsub(spacedArgLead, v, ' ')
spacedArgLead, _ = string.gsub(spacedArgLead, vim.pesc(v), ' ')
end
-- split the spaced version of `argLead`.
local splitArgLead = utils.strings.split(splitArgLead, ' ')
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
if #splitArgLead > 1 then
return nil
end
-- get the word selected by the user.
local word = splitArgLead[1]
@ -61,8 +69,8 @@ function prompt._createCompletionsProvider(completions)
local matches = {}
local i = 1
for _, v in ipairs(completions) do
if string.match(word, completion) then
matches[i] = completion
if string.match(vim.pesc(v), word) then
matches[i] = v
i = i + 1
end
end
@ -101,11 +109,8 @@ function prompt.enter(...)
i = i + 1
end
-- assign completions as the custom completions table provided.
elseif #args > 2 then completions = args[3] end
-- make the completions dict vim-compatable
if completions then completions =
'[' .. table.concat(completions, ',') .. ']'
elseif #args > 2 then
completions = args[3]
end
local continueMode = true
@ -114,21 +119,14 @@ function prompt.enter(...)
-- echo the indicator
api.nvim_redraw()
-- compose prompt command
--[[ TODO: completions won't work until neovim 0.5
look into v:lua when it drops.
local cmd = "input('" .. indicator .. "', ''"
if completions then cmd =
cmd .. ", 'customlist,funcref(\"libmodal#CreateCompletionsProvider\", ["
.. completions ..
"])'"
end
-- get input from prompt
local uinput = api.nvim_eval(cmd .. ')') -- closing bracket ends beginning 'input('
--]]
local uinput = api.nvim_call_function('input', {indicator})
local uinput = ''
if completions then
uinput = api.nvim_call_function(
'libmodal#_inputWith', {indicator, completions}
)
else
uinput = api.nvim_call_function('input', {indicator})
end
-- if a:2 is a function then call it.
if string.len(uinput) > 0 then
@ -152,6 +150,8 @@ function prompt.enter(...)
continueMode = false
end
end
vars:tearDown(modeName)
end
--[[

@ -91,9 +91,10 @@ function api.nvim_lecho(hlTables)
api.nvim_redraw()
hlTables[#hlTables+1] = resetHighlight
for _, hlTable in ipairs(hlTables) do
-- `:echohl` the hlgroup and then `:echon` the string
api.nvim_command(
-- `:echohl` the hlgroup and then `:echon` the string.
"echohl " .. tostring(hlTable.hl) .. " | echon '" .. tostring(hlTable.str) .. "'"
"echohl " .. tostring(hlTable['hl'])
.. " | echon '" .. tostring(hlTable['str']) .. "'"
)
end
end

@ -16,7 +16,6 @@ local utils = {}
utils.api = require('libmodal/src/utils/api')
utils.DateTime = require('libmodal/src/utils/DateTime')
utils.Indicator = require('libmodal/src/utils/Indicator')
utils.strings = require('libmodal/src/utils/strings')
utils.vars = require('libmodal/src/utils/vars')
utils.WindowState = require('libmodal/src/utils/WindowState')

@ -1,22 +0,0 @@
local strings = {}
------------------------------------
--[[ SUMMARY:
* Split some `str` using a regex `pattern`.
]]
--[[ PARAMS:
* `str` => the string to split.
* `pattern` => the regex pattern to split `str` with.
]]
------------------------------------
function strings.split(str, pattern)
local split = {}
local i = 1
for char in string.gmatch(str, pattern) do
split[i] = char
i = i + 1
end
return split
end
return strings

@ -4,6 +4,7 @@
*/
--]]
local globals = require('libmodal/src/base/globals')
local api = vim.api
--[[
@ -12,12 +13,8 @@ local api = vim.api
*/
--]]
local vars = {
combos = {},
input = {},
libmodalTimeout = api.nvim_get_var('libmodalTimeouts'),
timeout = {}
}
local vars = {}
vars.libmodalTimeout = api.nvim_get_var('libmodalTimeouts')
--[[
/*
@ -73,19 +70,28 @@ function vars.nvim_set(var, modeName, val)
api.nvim_set_var(var:name(modeName), val)
end
function vars:tearDown(modeName)
for _, v in pairs(self) do
if type(v) == globals.TYPE_TBL and v.instances[modeName] then
v.instances[modeName] = nil
end
end
end
--[[
/*
* VARS
*/
--]]
new('buffers')
new('combos' )
new('exit' )
new('input' )
new('timeout')
new('timer' )
new('windows')
new('buffers' )
new('combos' )
new('completions' )
new('exit' )
new('input' )
new('timeout' )
new('timer' )
new('windows' )
--[[
/*

@ -14,4 +14,4 @@ endif
" The default highlight groups (for colors) are specified below.
" Change these default colors by defining or linking the corresponding highlight group.
highlight default link LibmodalPrompt ModeMsg
highlight default link LibmodalStar StatusLine
highlight default LibmodalStar cterm=underline ctermfg=Cyan ctermbg=NONE gui=underline guifg=#55CCFF guibg=NONE guisp=#FFFFFF

Loading…
Cancel
Save