Fix loading bugs & add parseTable:get()

pull/3/head
Iron-E 4 years ago
parent 7df77f13fc
commit 742be23d1b
No known key found for this signature in database
GPG Key ID: 19B71B7B7B021D22

@ -3,8 +3,15 @@
* MODULE * MODULE
*/ */
--]] --]]
local globals = {} local globals = {}
--[[
/*
* TABLE `globals`
*/
--]]
globals.ESC_NR = 27 globals.ESC_NR = 27
globals.TYPE_FUNC = 'function' globals.TYPE_FUNC = 'function'
globals.TYPE_NUM = 'number' globals.TYPE_NUM = 'number'
@ -19,5 +26,5 @@ globals.VIM_TRUE = 1
* PUBLICIZE MODULE * PUBLICIZE MODULE
*/ */
--]] --]]
return globals
return globals

@ -3,15 +3,15 @@
* MODULE * MODULE
*/ */
--]] --]]
local base = {} local base = {}
base.globals = require('libmodal/src/base/globals') base.globals = require('libmodal/src/base/globals')
-- TODO
--[[ --[[
/* /*
* PUBLICIZE MODULE * PUBLICIZE MODULE
*/ */
--]] --]]
return base return base

@ -3,13 +3,12 @@
* MODULE * MODULE
*/ */
--]] --]]
local libmodal = require('libmodal/src/base') local libmodal = require('libmodal/src/base')
libmodal.mode = require('libmodal/src/mode') libmodal.mode = require('libmodal/src/mode')
libmodal.prompt = require('libmodal/src/prompt') libmodal.prompt = require('libmodal/src/prompt')
libmodal.utils = require('libmodal/src/utils') libmodal.utils = require('libmodal/src/utils')
-- TODO
--[[ --[[
/* /*
* PUBLICIZE MODULE * PUBLICIZE MODULE

@ -18,18 +18,19 @@ local strings = {} -- not to be returned. Used for split() function.
--[[ --[[
/* /*
* CONSTANTS * CLASS `ParseTable`
*/ */
--]] --]]
-- The number corresponding to <CR> in vim. ------------------------------------
ParseTable.CR = 13 --[[ SUMMARY:
* Split some `str` using a regex `pattern`.
]]
--[[ --[[
/* * `str` => the string to split.
* f(x) * `pattern` => the regex pattern to split `str` with.
*/ ]]
--]] ------------------------------------
function strings.split(str, pattern) function strings.split(str, pattern)
local split = {} local split = {}
for char in string.gmatch(str, pattern) do for char in string.gmatch(str, pattern) do
@ -38,18 +39,77 @@ function strings.split(str, pattern)
return split return split
end end
------------------------- --[[
/*
* CLASS `ParseTable`
*/
--]]
-- The number corresponding to <CR> in vim.
ParseTable.CR = 13
----------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Create a new parse table from a user-defined table. * Create a new parse table from a user-defined table.
]] ]]
--[[ PARAMS: --[[ PARAMS:
* `userTable` => the table of combos defined by the user. * `userTable` => the table of combos defined by the user.
]] ]]
------------------------- ----------------------------------
function ParseTable:new(userTable) function ParseTable.new(userTable)
local parseTable = {} local parseTable = {}
------------------------------- ----------------------------
--[[ SUMMARY:
* Get a value from this `ParseTable`.
]]
--[[ PARAMS:
* `key` => the PARSED key to get.
]]
--[[
* `function` => when `key` is a full match.
* `table` => when the `key` partially mathes.
* `false` => when `key` is not ANYWHERE.
]]
----------------------------
function parseTable:get(key)
local function parseGet(dict, splitKey)
--[[ Get the next character in the combo string. ]]
local k = ''
if #splitKey > 0 then -- There is more input to parse
k = api.nvim_eval("char2nr('" .. table.remove(splitKey) .. "')")
else -- The user input has run out, but there is more in the dictionary.
return dict
end
--[[ Parse the `k`. ]]
-- Make sure the dicitonary has a key for that value.
if dict[k] then
val = dict[k]
local valType = type(val)
if valType == globals.TYPE_TBL then
if val[ParseTable.CR] and #splitKey < 1 then
return val
else
return parseGet(val, splitKey)
end
elseif valType == globals.TYPE_STR and #splitKey < 1 then
return val
end
end
return false
end
-- run the inner recursive function in order to return the desired result
return parseGet(self, strings.split(
string.reverse(key), '.'
))
end
----------------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Put `value` into the parse tree as `key`. * Put `value` into the parse tree as `key`.
]] ]]
@ -57,7 +117,7 @@ function ParseTable:new(userTable)
* `key` => the key that `value` is reffered to by. * `key` => the key that `value` is reffered to by.
* `value` => the value to store as `key`. * `value` => the value to store as `key`.
]] ]]
------------------------------- ----------------------------------------
function parseTable:parsePut(key, value) function parseTable:parsePut(key, value)
-- Internal recursion function. -- Internal recursion function.
local function update(dict, splitKey) -- † local function update(dict, splitKey) -- †
@ -91,14 +151,14 @@ function ParseTable:new(userTable)
)) ))
end end
------------------------------- ---------------------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Create the union of `self` and `tableToUnite` * Create the union of `self` and `tableToUnite`
]] ]]
--[[ PARAMS: --[[ PARAMS:
* `tableToUnite` => the table to unite with `self.` * `tableToUnite` => the table to unite with `self.`
]] ]]
------------------------------- ---------------------------------------------
function parseTable:parsePutAll(tableToUnite) function parseTable:parsePutAll(tableToUnite)
for k, v in pairs(tableToUnite) do for k, v in pairs(tableToUnite) do
self:parsePut(k, v) self:parsePut(k, v)
@ -116,4 +176,5 @@ end
* PUBLICIZE MODULE * PUBLICIZE MODULE
*/ */
--]] --]]
return ParseTable return ParseTable

@ -4,8 +4,8 @@
*/ */
--]] --]]
local globals = require('libmodal/src/base/globals') local globals = require('libmodal/src/base/globals')
local utils = require('libmodal/src/utils') local utils = require('libmodal/src/utils')
local api = utils.api local api = utils.api
local vars = utils.vars local vars = utils.vars
@ -19,6 +19,12 @@ local vars = utils.vars
local mode = {} local mode = {}
mode.ParseTable = require('libmodal/src/mode/ParseTable') mode.ParseTable = require('libmodal/src/mode/ParseTable')
--[[
/*
* LIBRARY `mode`
*/
--]]
------------------------------------ ------------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Parse the `comboDict` and see if there is any command to execute. * Parse the `comboDict` and see if there is any command to execute.
@ -28,8 +34,20 @@ mode.ParseTable = require('libmodal/src/mode/ParseTable')
]] ]]
------------------------------------ ------------------------------------
function mode._comboSelect(modeName) function mode._comboSelect(modeName)
local comboDict = vars.combos.instances[modeName] -- Stop any running timers
-- TODO translate `LibmodalEnterWithCombos` if vars.timers.instances[modeName] then
vars.timers.instances[modeName]:stop()
vars.timers.instances[modeName] = nil
end
-- Append the latest input to the locally stored input history.
table.insert(
vars.input.instances[modeName],
vars.nvim_get(vars.input, modeName)
)
-- Get the combo dict.
local comboTable = vars.combos.instances[modeName]
end end
------------------------ ------------------------
@ -64,17 +82,19 @@ function mode.enter(...)
-- Determine whether a callback was specified, or a combo table. -- Determine whether a callback was specified, or a combo table.
if type(args[2]) == globals.TYPE_TBL then if type(args[2]) == globals.TYPE_TBL then
mode._initTimeouts(modeName) mode._initTimeouts(modeName, args[2])
end end
--[[ MODE LOOP. ]] --[[ MODE LOOP. ]]
while true do local continueMode = true
while continueMode do
-- Try (using pcall) to use the mode. -- Try (using pcall) to use the mode.
local noErrors = pcall(function() local noErrors = pcall(function()
-- If the mode is not handling exit events automatically and the global exit var is true. -- If the mode is not handling exit events automatically and the global exit var is true.
if not handleExitEvents and var.nvim_get(vars.exit, modeName) then if not handleExitEvents and var.nvim_get(vars.exit, modeName) then
break continueMode = false
return
end end
-- Echo the indicator. -- Echo the indicator.
@ -86,7 +106,8 @@ function mode.enter(...)
-- Make sure that the user doesn't want to exit. -- Make sure that the user doesn't want to exit.
if handleExitEvents and uinput == globals.ESC_NR then if handleExitEvents and uinput == globals.ESC_NR then
break continueMode = false
return
-- If the second argument was a dict, parse it. -- If the second argument was a dict, parse it.
elseif type(args[2]) == globals.TYPE_TBL then elseif type(args[2]) == globals.TYPE_TBL then
mode._comboSelect(modeName) mode._comboSelect(modeName)
@ -95,12 +116,12 @@ function mode.enter(...)
args[2]() args[2]()
end end
end)() end)
-- If there were errors, handle them. -- If there were errors, handle them.
if not noErrors then if not noErrors then
mode._showError() mode._showError()
break continueMode = false
end end
end end
@ -111,7 +132,7 @@ function mode.enter(...)
winState:restore() winState:restore()
end end
function mode._initTimeouts(modeName) function mode._initTimeouts(modeName, comboTable)
-- Placeholder for timeout value. -- Placeholder for timeout value.
local doTimeout = nil local doTimeout = nil
@ -124,7 +145,7 @@ function mode._initTimeouts(modeName)
vars.timeout.instances[modeName] = doTimeout vars.timeout.instances[modeName] = doTimeout
-- Build the parse tree. -- Build the parse tree.
vars.combos.instances[modeName] = mode.ParseTable:new(args[2]) vars.combos.instances[modeName] = mode.ParseTable.new(comboTable)
-- Initialize the input history variable. -- Initialize the input history variable.
vars.input.instances[modeName] = {} vars.input.instances[modeName] = {}
@ -144,6 +165,7 @@ end
* PUBLICIZE MODULE * PUBLICIZE MODULE
*/ */
--]] --]]
mode.enter('test', {}) mode.enter('test', {})
return mode return mode

@ -3,18 +3,24 @@
* MODULE * MODULE
*/ */
--]] --]]
local prompt = {} local prompt = {}
---------------------------------- --[[
/*
* LIB `prompt`
*/
--]]
--------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Enter a prompt. * Enter a prompt.
]] ]]
--[[ PARAMS:
--[[ PARAMETERS:
* `args[1]` => the prompt name. * `args[1]` => the prompt name.
* `args[2]` => the prompt callback, or mode command table. * `args[2]` => the prompt callback, or mode command table.
]] ]]
---------------------------------- --------------------------
function prompt.enter(...) function prompt.enter(...)
local args = {...} local args = {...}
end end

@ -0,0 +1,54 @@
--[[
/*
* MODULE
*/
--]]
local DateTime = {}
--[[
/*
* CLASS `DateTime`
*/
--]]
-----------------------
--[[ SUMMARY:
* Get the current date.
]]
-----------------------
function DateTime.now()
dateTime = os.date("*t")
-------------------------------
--[[ SUMMARY:
* Add some `duration` to `self`.
]]
--[[ PARAMS:
* `duration` => the duration of time to add.
]]
--[[ RETURNS:
* A new `DateTime` with the added seconds.
]]
-------------------------------
function dateTime:add(seconds)
local time
-- Copy `self` to `time`
for k,v in pairs(self) do
time[k] = v
end
-- Add `seconds` to `time`
time.sec = time.sec + seconds
return time
end
return dateTime
end
--[[
/*
* PUBLICIZE MODULE
*/
--]]
return DateTime

@ -3,6 +3,7 @@
* MODULE * MODULE
*/ */
--]] --]]
local Entry = {} local Entry = {}
--[[ --[[
@ -11,16 +12,15 @@ local Entry = {}
*/ */
--]] --]]
-------------------------------------------------------- --------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Create a new `Indicator.Entry`. * Create a new `Indicator.Entry`.
]] ]]
--[[ PARAMS: --[[ PARAMS:
* `hlgroup` => The `highlight-group` to be used for this `Indicator.Entry`. * `hlgroup` => The `highlight-group` to be used for this `Indicator.Entry`.
* `str` => The text for this `Indicator.Entry`. * `str` => The text for this `Indicator.Entry`.
]] ]]
-------------------------------------------------------- --------------------------------
function Entry.new(hlgroup, str) function Entry.new(hlgroup, str)
return { return {
['hl'] = hlgroup, ['hl'] = hlgroup,
@ -33,4 +33,5 @@ end
* PUBLICIZE MODULE * PUBLICIZE MODULE
*/ */
--]] --]]
return Entry return Entry

@ -3,6 +3,7 @@
* IMPORTS * IMPORTS
*/ */
--]] --]]
local Entry = require('libmodal/src/utils/Indicator/Entry') local Entry = require('libmodal/src/utils/Indicator/Entry')
--[[ --[[
@ -10,6 +11,7 @@ local Entry = require('libmodal/src/utils/Indicator/Entry')
* MODULE * MODULE
*/ */
--]] --]]
local Indicator = {} local Indicator = {}
--[[ --[[
@ -18,15 +20,14 @@ local Indicator = {}
*/ */
--]] --]]
----------------------------------------------- --------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Create a new `Indicator`. * Create a new `Indicator`.
]] ]]
--[[ PARAMS: --[[ PARAMS:
* `modeName` => the name of the mode that this `Indicator` is for. * `modeName` => the name of the mode that this `Indicator` is for.
]] ]]
----------------------------------------------- --------------------------------
function Indicator:new(modeName) function Indicator:new(modeName)
return { return {
Entry.new('LibmodalStar', '*'), Entry.new('LibmodalStar', '*'),
@ -41,4 +42,5 @@ end
* PUBLICIZE MODULE * PUBLICIZE MODULE
*/ */
--]] --]]
return Indicator return Indicator

@ -3,6 +3,7 @@
* IMPORTS * IMPORTS
*/ */
--]] --]]
local api = vim.api local api = vim.api
--[[ --[[
@ -10,6 +11,7 @@ local api = vim.api
* MODULE * MODULE
*/ */
--]] --]]
local WindowState = {} local WindowState = {}
local height = 'winheight' local height = 'winheight'
@ -43,4 +45,5 @@ end
* PUBLICIZE MODULE * PUBLICIZE MODULE
*/ */
--]] --]]
return WindowState return WindowState

@ -5,7 +5,7 @@
--]] --]]
local globals = require('libmodal/src/base/globals') local globals = require('libmodal/src/base/globals')
local Entry = require('libmodal/src/utils/Indicator/Entry.lua') local Entry = require('libmodal/src/utils/Indicator/Entry')
--[[ --[[
/* /*
@ -41,7 +41,7 @@ end
--[[ SUMMARY: --[[ SUMMARY:
* Check whether or not some variable exists. * Check whether or not some variable exists.
]] ]]
--[[ --[[ PARAMS:
* `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.
]] ]]
@ -59,7 +59,7 @@ function api.nvim_input()
return api.nvim_eval('getchar()') return api.nvim_eval('getchar()')
end end
------------------------ ---------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Echo a table of {`hlgroup`, `str`} tables. * Echo a table of {`hlgroup`, `str`} tables.
* Meant to be read as "nvim list echo". * Meant to be read as "nvim list echo".
@ -67,13 +67,13 @@ end
--[[ PARAMS: --[[ PARAMS:
* `hlTables` => the tables to echo with highlights. * `hlTables` => the tables to echo with highlights.
]] ]]
------------------------ ---------------------------------
function api.nvim_lecho(hlTables) function api.nvim_lecho(hlTables)
api.nvim_redraw() api.nvim_redraw()
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.
"execute(['echohl " .. hlTable.hl .. "', 'echon " .. hlTable.str .. "'])" "echon execute(['echohl \"" .. hlTable.hl .. "\"', 'echon \"" .. hlTable.str .. "\"'])"
) )
end end
api.nvim_command('echohl None') api.nvim_command('echohl None')
@ -89,7 +89,7 @@ function api.nvim_redraw()
api.nvim_command('mode') api.nvim_command('mode')
end end
------------------------------- --------------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Show a `title` error. * Show a `title` error.
]] ]]
@ -97,7 +97,7 @@ end
* `title` => the title of the error. * `title` => the title of the error.
* `msg` => the message of the error. * `msg` => the message of the error.
]] ]]
------------------------------- --------------------------------------
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', title .. '\n'),

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

@ -76,10 +76,12 @@ end
* VARS * VARS
*/ */
--]] --]]
new('combos' , 'ModeCombos')
new('exit' , 'ModeExit') new('combos' , 'ModeCombos' )
new('input' , 'ModeInput') new('exit' , 'ModeExit' )
new('input' , 'ModeInput' )
new('timeout' , 'ModeTimeout') new('timeout' , 'ModeTimeout')
new('timer' , 'ModeTimer' )
--[[ --[[
/* /*

Loading…
Cancel
Save