Consistency improvements

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

@ -149,7 +149,7 @@ VARIABLES *libmodal-lua-globals-variables*
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-globals-functions* FUNCTIONS *libmodal-lua-globals-functions*
`globals`.isFalse({val}) *libmodal-lua-globals.isFalse()* `globals`.is_false({val}) *libmodal-lua-globals.is_false()*
Determine whether or not some {val} is equal to `globals.VIM_FALSE`, Determine whether or not some {val} is equal to `globals.VIM_FALSE`,
|v:false|, or `false`. |v:false|, or `false`.
@ -181,16 +181,16 @@ FUNCTIONS *libmodal-lua-globals-functions*
--[[ Test the function. ]] --[[ Test the function. ]]
print(libmodal.globals.isFalse(falseValue)) print(libmodal.globals.is_false(falseValue))
print(libmodal.globals.isFalse(v_falseValue)) print(libmodal.globals.is_false(v_falseValue))
print(libmodal.globals.isFalse(vim_falseValue)) print(libmodal.globals.is_false(vim_falseValue))
print(libmodal.globals.isFalse(trueValue)) print(libmodal.globals.is_false(trueValue))
print(libmodal.globals.isFalse(v_trueValue)) print(libmodal.globals.is_false(v_trueValue))
print(libmodal.globals.isFalse(vim_trueValue)) print(libmodal.globals.is_false(vim_trueValue))
< <
`globals`.isTrue({val}) *libmodal-lua-globals.isTrue()* `globals`.is_true({val}) *libmodal-lua-globals.is_true()*
Determine whether or not some {val} is equal to `globals.VIM_TRUE`, Determine whether or not some {val} is equal to `globals.VIM_TRUE`,
|v:true|, or `true`. |v:true|, or `true`.
@ -221,13 +221,13 @@ FUNCTIONS *libmodal-lua-globals-functions*
--[[ Test the function. ]] --[[ Test the function. ]]
print(libmodal.globals.isTrue(falseValue)) print(libmodal.globals.is_true(falseValue))
print(libmodal.globals.isTrue(v_falseValue)) print(libmodal.globals.is_true(v_falseValue))
print(libmodal.globals.isTrue(vim_falseValue)) print(libmodal.globals.is_true(vim_falseValue))
print(libmodal.globals.isTrue(trueValue)) print(libmodal.globals.is_true(trueValue))
print(libmodal.globals.isTrue(v_trueValue)) print(libmodal.globals.is_true(v_trueValue))
print(libmodal.globals.isTrue(vim_trueValue)) print(libmodal.globals.is_true(vim_trueValue))
< <
============================================================================== ==============================================================================

@ -1,28 +1,33 @@
local api = vim.api local api = vim.api
local libmodal = require('libmodal') local libmodal = require('libmodal')
local fooModeInputHistory = {}
local function clearHistory(indexToCheck) local _inputHistory = {}
if #fooModeInputHistory >= indexToCheck then
fooModeInputHistory = {} function _inputHistory:clear(indexToCheck)
if #self >= indexToCheck then
for i, _ in ipairs(self) do
self[i] = nil
end
end end
end end
function fooMode() function fooMode()
fooModeInputHistory[#fooModeInputHistory + 1] = string.char( inputHistory[#inputHistory + 1] = string.char(
api.nvim_get_var('fooModeInput') api.nvim_get_var('fooModeInput')
) )
local index = 1 local index = 1
if fooModeInputHistory[1] == 'z' then if inputHistory[1] == 'z' then
if fooModeInputHistory[2] == 'f' then if inputHistory[2] == 'f' then
if fooModeInputHistory[3] == 'o' then if inputHistory[3] == 'o' then
api.nvim_command("echom 'It works!'") api.nvim_command("echom 'It works!'")
else index = 3 end else index = 3
else index = 2 end end
else index = 2
end
end end
clearHistory(index) _inputHistory:clear(index)
end end
libmodal.mode.enter('FOO', fooMode) libmodal.mode.enter('FOO', fooMode)

@ -1,6 +1,7 @@
local libmodal = require('libmodal') local libmodal = require('libmodal')
local fooModeRecurse = 0 local fooModeRecurse = 0
local fooModeCombos = { local fooModeCombos = {
['z'] = 'lua fooMode()' ['z'] = 'lua fooMode()'
} }

@ -2,7 +2,7 @@ local libmodal = require('libmodal')
local fooModeCombos = { local fooModeCombos = {
['zf'] = 'split', ['zf'] = 'split',
['zfo'] = 'vsplit', ['zfo'] = 'vsplit',
['zfc'] = 'tabnew' ['zfc'] = 'q'
} }
libmodal.mode.enter('FOO', fooModeCombos) libmodal.mode.enter('FOO', fooModeCombos)

@ -4,7 +4,8 @@
*/ */
--]] --]]
local api = vim.api local api = vim.api
local classes = require('libmodal/src/classes')
--[[ --[[
/* /*
@ -33,8 +34,7 @@ local _winOpenOpts = {
*/ */
--]] --]]
local _metaPopup = {} local _metaPopup = classes.new({})
_metaPopup.__index = _metaPopup
_metaPopup._buffer = nil _metaPopup._buffer = nil
_metaPopup._inputChars = nil _metaPopup._inputChars = nil

@ -4,6 +4,7 @@
*/ */
--]] --]]
local classes = require('libmodal/src/classes')
local globals = require('libmodal/src/globals') local globals = require('libmodal/src/globals')
local Indicator = require('libmodal/src/Indicator') local Indicator = require('libmodal/src/Indicator')
local collections = require('libmodal/src/collections') local collections = require('libmodal/src/collections')
@ -38,18 +39,15 @@ _TIMEOUT.NR = string.byte(_TIMEOUT.CHAR)
*/ */
--]] --]]
local _metaMode = {} local _metaMode = classes.new({})
_metaMode.__index = _metaMode
local _metaInputBytes = { local _metaInputBytes = classes.new({
['clear'] = function(__self) ['clear'] = function(__self)
for i, _ in ipairs(__self) do for i, _ in ipairs(__self) do
__self[i] = nil __self[i] = nil
end end
end end
} })
_metaInputBytes.__index = _metaInputBytes
----------------------------------------------- -----------------------------------------------
--[[ SUMMARY: --[[ SUMMARY:
@ -80,7 +78,7 @@ function _metaMode:_checkInputForMapping()
inputBytes:clear() inputBytes:clear()
-- The command was a table, meaning that it MIGHT match. -- The command was a table, meaning that it MIGHT match.
elseif commandType == globals.TYPE_TBL elseif commandType == globals.TYPE_TBL
and globals.isTrue(self._timeouts.enabled) and globals.is_true(self._timeouts.enabled)
then then
-- Create a new timer -- Create a new timer
@ -184,7 +182,7 @@ end
function _metaMode:_inputLoop() function _metaMode:_inputLoop()
-- 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 self._exit.supress if self._exit.supress
and globals.isTrue(self._exit:nvimGet()) and globals.is_true(self._exit:nvimGet())
then then
return false return false
end end
@ -266,7 +264,7 @@ function Mode.new(name, instruction, ...)
-- Define the exit flag -- Define the exit flag
self._exit.supress = (function(optionalValue) self._exit.supress = (function(optionalValue)
if optionalValue then if optionalValue then
return globals.isTrue(optionalValue) return globals.is_true(optionalValue)
else else
return false return false
end end

@ -4,6 +4,7 @@
*/ */
--]] --]]
local classes = require('libmodal/src/classes')
local globals = require('libmodal/src/globals') local globals = require('libmodal/src/globals')
local Indicator = require('libmodal/src/Indicator') local Indicator = require('libmodal/src/Indicator')
local utils = require('libmodal/src/utils') local utils = require('libmodal/src/utils')
@ -35,8 +36,7 @@ end
*/ */
--]] --]]
local _metaPrompt = {} local _metaPrompt = classes.new({})
_metaPrompt.__index = _metaPrompt
--------------------------------- ---------------------------------
--[[ SUMMARY: --[[ SUMMARY:

@ -4,8 +4,9 @@
*/ */
--]] --]]
local api = vim.api
local classes = require('libmodal/src/classes')
local globals = require('libmodal/src/globals') local globals = require('libmodal/src/globals')
local api = vim.api
--[[ --[[
/* /*
@ -25,8 +26,7 @@ local Vars = {
*/ */
--]] --]]
local _metaVars = {} local _metaVars = classes.new({})
_metaVars.__index = _metaVars
-- Instances of variables pertaining to a certain mode. -- Instances of variables pertaining to a certain mode.
_metaVars._varName = nil _metaVars._varName = nil

@ -0,0 +1,13 @@
local classes = {}
--------------------------
--[[ SUMMARY:
* Define a class-metatable.
]]
--------------------------
function classes.new(base)
base.__index = base
return base
end
return classes

@ -4,8 +4,9 @@
*/ */
--]] --]]
local api = vim.api local api = vim.api
local globals = require('libmodal/src/globals') local classes = require('libmodal/src/classes')
local globals = require('libmodal/src/globals')
--[[ --[[
/* /*
@ -15,11 +16,77 @@ local globals = require('libmodal/src/globals')
local ParseTable = {} local ParseTable = {}
--[[ -- The number corresponding to <CR> in vim.
/* ParseTable.CR = 13
* Utils for `ParseTable`
*/ -----------------------------------------
--]] --[[ SUMMARY
* Get `splitKey` from some `parseTable`.
]]
--[[ PARAMS:
* `parseTable` => the table to fetch `splitKey` from.
* `splitKey` => the key split into groups.
]]
-----------------------------------------
local function _get(parseTable, splitKey)
--[[ Get the next character in the combo string. ]]
local k = ''
if #splitKey > 0 then -- There is more input to parse
k = table.remove(splitKey) -- the table should already be `char2nr()`'d
else -- the user input has run out, but there is more in the `parseTable`.
return parseTable
end
--[[ Parse the `k`. ]]
-- Make sure the dicitonary has a key for that value.
if parseTable[k] then
val = parseTable[k]
local valType = type(val)
if valType == globals.TYPE_TBL then
if val[ParseTable.CR] and #splitKey < 1 then
return val
else
return _get(val, splitKey)
end
elseif valType == globals.TYPE_STR and #splitKey < 1 then
return val
end
end
return false
end
-----------------------------------------
--[[ SUMMARY:
* Update the values of some `dict` using a `splitKey`.
]]
--[[ PARAMS:
* `parseTable` => the parseTable to update.
* `splitKey` => the key split into groups.
]]
-----------------------------------------
local function _put(parseTable, splitKey) -- †
--[[ Get the next character in the table. ]]
local k = string.byte(table.remove(splitKey))
if #splitKey > 0 then -- there are still characters left in the key.
if not parseTable[k] then parseTable[k] = {}
-- If there is a previous command mapping in place
elseif type(parseTable[k]) == globals.TYPE_STR then
-- Swap the mapping to a `CR`
parseTable[k] = {[ParseTable.CR] = parseTable[k]}
end
-- run _update() again
_put(parseTable[k], splitKey)
-- If parseTable[k] is a pre-existing table, don't clobber the table— clobber the `CR` value.
elseif type(parseTable[k]) == globals.TYPE_TBL then
parseTable[k][ParseTable.CR] = value
else parseTable[k] = value -- parseTable[k] is not a table, go ahead and clobber the value.
end
end -- ‡
-------------------------------------- --------------------------------------
--[[ SUMMARY: --[[ SUMMARY:
@ -30,7 +97,7 @@ local ParseTable = {}
* `regex` => the regex to split `str` with. * `regex` => the regex to split `str` with.
]] ]]
-------------------------------------- --------------------------------------
local function _stringSplit(str, regex) local function _string_split(str, regex)
local split = {} local split = {}
for char in string.gmatch(str, regex) do for char in string.gmatch(str, regex) do
split[#split + 1] = char split[#split + 1] = char
@ -38,17 +105,18 @@ local function _stringSplit(str, regex)
return split return split
end end
--------------------------------- ----------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Reverse the elements of some table. * Reverse the elements of some table.
]] ]]
--[[ PARAMS: --[[ PARAMS:
* `tbl` => the table to reverse. * `tbl` => the table to reverse.
]] ]]
--------------------------------- ----------------------------------
local function _tableReverse(tbl) local function _table_reverse(tbl)
local reversed = {} local reversed = {}
while #reversed < #tbl do while #reversed < #tbl do
-- look, no variables!
reversed[#reversed + 1] = tbl[#tbl - #reversed] reversed[#reversed + 1] = tbl[#tbl - #reversed]
end end
return reversed return reversed
@ -56,132 +124,80 @@ end
--[[ --[[
/* /*
* CLASS `ParseTable` * META `ParseTable`
*/ */
--]] --]]
-- The number corresponding to <CR> in vim. local _metaParseTable = classes.new({})
ParseTable.CR = 13
---------------------------------- ------------------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Create a new parse table from a user-defined table. * Get a value from this `ParseTable`.
]] ]]
--[[ PARAMS: --[[ PARAMS:
* `userTable` => the table of combos defined by the user. * `key` => the PARSED key to get.
]] ]]
---------------------------------- --[[
function ParseTable.new(userTable) * `function` => when `key` is a full match.
local parseTable = {} * `table` => when the `key` partially mathes.
* `false` => when `key` is not ANYWHERE.
-------------------------------- ]]
--[[ SUMMARY: ------------------------------------------
* Get a value from this `ParseTable`. function _metaParseTable:parseGet(keyDict)
]] return _get(self, _table_reverse(keyDict))
--[[ PARAMS: end
* `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(keyDict)
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 = table.remove(splitKey) -- the dict should already be `char2nr()`'d
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
--[[ Reverse the dict. ]] ---------------------------------------------
local reversed = _tableReverse(keyDict) --[[ SUMMARY:
* Put `value` into the parse tree as `key`.
]]
--[[ PARAMS:
* `key` => the key that `value` is reffered to by.
* `value` => the value to store as `key`.
]]
---------------------------------------------
function _metaParseTable:parsePut(key, value)
_put(self, _stringSplit(
string.reverse(key), '.'
))
end
--[[ Get return value. ]] --------------------------------------------------
-- run the inner recursive function in order to return the desired result --[[ SUMMARY:
return parseGet(self, reversed) * Create the union of `self` and `tableToUnite`
]]
--[[ PARAMS:
* `tableToUnite` => the table to unite with `self.`
]]
--------------------------------------------------
function _metaParseTable:parsePutAll(tableToUnite)
for k, v in pairs(tableToUnite) do
self:parsePut(k, v)
end end
end
---------------------------------------- --[[
--[[ SUMMARY: /*
* Put `value` into the parse tree as `key`. * CLASS `ParseTable`
]] */
--[[ PARAMS: --]]
* `key` => the key that `value` is reffered to by.
* `value` => the value to store as `key`.
]]
----------------------------------------
function parseTable:parsePut(key, value)
-- Internal recursion function.
local function update(dict, splitKey) -- †
--[[ Get the next character in the table. ]]
local k = string.byte(table.remove(splitKey))
if #splitKey > 0 then -- there are still kacters left in the key.
if not dict[k] then dict[k] = {}
-- If there is a previous command mapping in place
elseif type(dict[k]) == globals.TYPE_STR then
-- Swap the mapping to a `CR`
dict[k] = {[ParseTable.CR] = dict[k]}
end
-- run update() again
update(dict[k], splitKey)
-- If dict[k] is a pre-existing table, don't clobber the table— clobber the `CR` value.
elseif type(dict[k]) == globals.TYPE_TBL then
dict[k][ParseTable.CR] = value
else dict[k] = value -- dict[k] is not a table, go ahead and clobber the value.
end
end -- ‡
-- Run the recursive function.
update(self, _stringSplit(
string.reverse(key), '.')
)
end
--------------------------------------------- ----------------------------------
--[[ SUMMARY: --[[ SUMMARY:
* Create the union of `self` and `tableToUnite` * Create a new parse table from a user-defined table.
]] ]]
--[[ PARAMS: --[[ PARAMS:
* `tableToUnite` => the table to unite with `self.` * `userTable` => the table of combos defined by the user.
]] ]]
--------------------------------------------- ----------------------------------
function parseTable:parsePutAll(tableToUnite) function ParseTable.new(userTable)
for k, v in pairs(tableToUnite) do local self = setmetatable({}, _metaParseTable)
self:parsePut(k, v)
end
end
-- Parse the passed in table. -- Parse the passed in table.
parseTable:parsePutAll(userTable) self:parsePutAll(userTable)
-- Return the new `ParseTable`. -- Return the new `ParseTable`.
return parseTable return self
end end
--[[ --[[

@ -1,3 +1,11 @@
--[[
/*
* IMPORTS
*/
--]]
local classes = require('libmodal/src/classes')
--[[ --[[
/* /*
* MODULE `Stack` * MODULE `Stack`
@ -12,8 +20,7 @@ local Stack = {}
*/ */
--]] --]]
local _metaStack = {} local _metaStack = classes.new({})
_metaStack.__index = _metaStack
_metaStack._len = 0 _metaStack._len = 0
_metaStack._top = nil _metaStack._top = nil

@ -21,11 +21,11 @@ globals.TYPE_TBL = 'table'
globals.VIM_FALSE = 0 globals.VIM_FALSE = 0
globals.VIM_TRUE = 1 globals.VIM_TRUE = 1
function globals.isFalse(val) function globals.is_false(val)
return val == false or val == globals.VIM_FALSE return val == false or val == globals.VIM_FALSE
end end
function globals.isTrue(val) function globals.is_true(val)
return val == true or val == globals.VIM_TRUE return val == true or val == globals.VIM_TRUE
end end

@ -6,6 +6,7 @@
local libmodal = {} local libmodal = {}
libmodal.classes = require('libmodal/src/classes')
libmodal.collection = require('libmodal/src/collections') libmodal.collection = require('libmodal/src/collections')
libmodal.globals = require('libmodal/src/globals') libmodal.globals = require('libmodal/src/globals')
libmodal.Indicator = require('libmodal/src/Indicator') libmodal.Indicator = require('libmodal/src/Indicator')

@ -1,11 +1,18 @@
--[[
/*
* IMPORTS
*/
--]]
local classes = require('libmodal/src/classes')
--[[ --[[
/* /*
* META `Help` * META `Help`
*/ */
--]] --]]
local _metaHelp = {} local _metaHelp = classes.new({})
_metaHelp.__index = _metaHelp
------------------------- -------------------------
--[[ SUMMARY: --[[ SUMMARY:
@ -54,7 +61,7 @@ function Help.new(commandsOrMaps, title)
end end
-- define the separator for entries in the help table. -- define the separator for entries in the help table.
local SEPARATOR = '' local SEPARATOR_TEMPLATE = {'', '\n'}
---------------------- ----------------------
--[[ SUMMARY: --[[ SUMMARY:
@ -71,20 +78,18 @@ function Help.new(commandsOrMaps, title)
local toPrint = {} local toPrint = {}
for k, v in pairs(tbl) do for k, v in pairs(tbl) do
toPrint[#toPrint + 1] = k toPrint[#toPrint + 1] = k
local i = longestKey - string.len(k) for i = longestKey, string.len(k) do
while i > 0 do
toPrint[#toPrint + 1] = ' ' toPrint[#toPrint + 1] = ' '
i = i - 1
end end
toPrint[#toPrint + 1] = SEPARATOR .. v .. '\n' toPrint[#toPrint + 1] = table.concat(SEPARATOR_TEMPLATE, v)
end end
return toPrint return toPrint
end end
-- define the separator for the help table. -- define the separator for the help table.
local helpSeparator = {} local helpSeparator = {}
while #helpSeparator < string.len(title) do for i = 1, string.len(title) do
helpSeparator[#helpSeparator + 1] = '-' helpSeparator[i] = '-'
end end
helpSeparator = table.concat(helpSeparator) helpSeparator = table.concat(helpSeparator)
@ -93,7 +98,7 @@ function Help.new(commandsOrMaps, title)
{ {
[1] = ' ', [1] = ' ',
[2] = table.concat(tabAlign({ [2] = table.concat(tabAlign({
[title] = 'VIM EXPRESSION', [title] = 'VIM EXPRESSION'
})), })),
[3] = table.concat(tabAlign({ [3] = table.concat(tabAlign({
[helpSeparator] = '--------------' [helpSeparator] = '--------------'

@ -4,7 +4,8 @@
*/ */
--]] --]]
local api = require('libmodal/src/utils/api') local api = require('libmodal/src/utils/api')
local classes = require('libmodal/src/classes')
--[[ --[[
/* /*
@ -23,8 +24,7 @@ local width = 'winwidth'
*/ */
--]] --]]
local _metaWindowState = {} local _metaWindowState = classes.new({})
_metaWindowState.__index = _metaWindowState
----------------------------------- -----------------------------------
--[[ SUMMARY --[[ SUMMARY

Loading…
Cancel
Save