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*
`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`,
|v:false|, or `false`.
@ -181,16 +181,16 @@ FUNCTIONS *libmodal-lua-globals-functions*
--[[ Test the function. ]]
print(libmodal.globals.isFalse(falseValue))
print(libmodal.globals.isFalse(v_falseValue))
print(libmodal.globals.isFalse(vim_falseValue))
print(libmodal.globals.is_false(falseValue))
print(libmodal.globals.is_false(v_falseValue))
print(libmodal.globals.is_false(vim_falseValue))
print(libmodal.globals.isFalse(trueValue))
print(libmodal.globals.isFalse(v_trueValue))
print(libmodal.globals.isFalse(vim_trueValue))
print(libmodal.globals.is_false(trueValue))
print(libmodal.globals.is_false(v_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`,
|v:true|, or `true`.
@ -221,13 +221,13 @@ FUNCTIONS *libmodal-lua-globals-functions*
--[[ Test the function. ]]
print(libmodal.globals.isTrue(falseValue))
print(libmodal.globals.isTrue(v_falseValue))
print(libmodal.globals.isTrue(vim_falseValue))
print(libmodal.globals.is_true(falseValue))
print(libmodal.globals.is_true(v_falseValue))
print(libmodal.globals.is_true(vim_falseValue))
print(libmodal.globals.isTrue(trueValue))
print(libmodal.globals.isTrue(v_trueValue))
print(libmodal.globals.isTrue(vim_trueValue))
print(libmodal.globals.is_true(trueValue))
print(libmodal.globals.is_true(v_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 fooModeInputHistory = {}
local function clearHistory(indexToCheck)
if #fooModeInputHistory >= indexToCheck then
fooModeInputHistory = {}
local _inputHistory = {}
function _inputHistory:clear(indexToCheck)
if #self >= indexToCheck then
for i, _ in ipairs(self) do
self[i] = nil
end
end
end
function fooMode()
fooModeInputHistory[#fooModeInputHistory + 1] = string.char(
inputHistory[#inputHistory + 1] = string.char(
api.nvim_get_var('fooModeInput')
)
local index = 1
if fooModeInputHistory[1] == 'z' then
if fooModeInputHistory[2] == 'f' then
if fooModeInputHistory[3] == 'o' then
if inputHistory[1] == 'z' then
if inputHistory[2] == 'f' then
if inputHistory[3] == 'o' then
api.nvim_command("echom 'It works!'")
else index = 3 end
else index = 2 end
else index = 3
end
else index = 2
end
end
clearHistory(index)
_inputHistory:clear(index)
end
libmodal.mode.enter('FOO', fooMode)

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

@ -2,7 +2,7 @@ local libmodal = require('libmodal')
local fooModeCombos = {
['zf'] = 'split',
['zfo'] = 'vsplit',
['zfc'] = 'tabnew'
['zfc'] = 'q'
}
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 = {}
_metaPopup.__index = _metaPopup
local _metaPopup = classes.new({})
_metaPopup._buffer = nil
_metaPopup._inputChars = nil

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

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

@ -4,8 +4,9 @@
*/
--]]
local api = vim.api
local classes = require('libmodal/src/classes')
local globals = require('libmodal/src/globals')
local api = vim.api
--[[
/*
@ -25,8 +26,7 @@ local Vars = {
*/
--]]
local _metaVars = {}
_metaVars.__index = _metaVars
local _metaVars = classes.new({})
-- Instances of variables pertaining to a certain mode.
_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 globals = require('libmodal/src/globals')
local api = vim.api
local classes = require('libmodal/src/classes')
local globals = require('libmodal/src/globals')
--[[
/*
@ -15,11 +16,77 @@ local globals = require('libmodal/src/globals')
local ParseTable = {}
--[[
/*
* Utils for `ParseTable`
*/
--]]
-- The number corresponding to <CR> in vim.
ParseTable.CR = 13
-----------------------------------------
--[[ 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:
@ -30,7 +97,7 @@ local ParseTable = {}
* `regex` => the regex to split `str` with.
]]
--------------------------------------
local function _stringSplit(str, regex)
local function _string_split(str, regex)
local split = {}
for char in string.gmatch(str, regex) do
split[#split + 1] = char
@ -38,17 +105,18 @@ local function _stringSplit(str, regex)
return split
end
---------------------------------
----------------------------------
--[[ SUMMARY:
* Reverse the elements of some table.
]]
--[[ PARAMS:
* `tbl` => the table to reverse.
]]
---------------------------------
local function _tableReverse(tbl)
----------------------------------
local function _table_reverse(tbl)
local reversed = {}
while #reversed < #tbl do
-- look, no variables!
reversed[#reversed + 1] = tbl[#tbl - #reversed]
end
return reversed
@ -56,132 +124,80 @@ end
--[[
/*
* CLASS `ParseTable`
* META `ParseTable`
*/
--]]
-- The number corresponding to <CR> in vim.
ParseTable.CR = 13
local _metaParseTable = classes.new({})
----------------------------------
------------------------------------------
--[[ SUMMARY:
* Create a new parse table from a user-defined table.
* Get a value from this `ParseTable`.
]]
--[[ PARAMS:
* `userTable` => the table of combos defined by the user.
* `key` => the PARSED key to get.
]]
----------------------------------
function ParseTable.new(userTable)
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(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
--[[
* `function` => when `key` is a full match.
* `table` => when the `key` partially mathes.
* `false` => when `key` is not ANYWHERE.
]]
------------------------------------------
function _metaParseTable:parseGet(keyDict)
return _get(self, _table_reverse(keyDict))
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
return parseGet(self, reversed)
--------------------------------------------------
--[[ SUMMARY:
* 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
----------------------------------------
--[[ 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 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
--[[
/*
* CLASS `ParseTable`
*/
--]]
---------------------------------------------
--[[ SUMMARY:
* Create the union of `self` and `tableToUnite`
]]
--[[ PARAMS:
* `tableToUnite` => the table to unite with `self.`
]]
---------------------------------------------
function parseTable:parsePutAll(tableToUnite)
for k, v in pairs(tableToUnite) do
self:parsePut(k, v)
end
end
----------------------------------
--[[ SUMMARY:
* Create a new parse table from a user-defined table.
]]
--[[ PARAMS:
* `userTable` => the table of combos defined by the user.
]]
----------------------------------
function ParseTable.new(userTable)
local self = setmetatable({}, _metaParseTable)
-- Parse the passed in table.
parseTable:parsePutAll(userTable)
self:parsePutAll(userTable)
-- Return the new `ParseTable`.
return parseTable
return self
end
--[[

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

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

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

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

Loading…
Cancel
Save