From eee6a017782bf798cee1fdaf0d535a7cca07896c Mon Sep 17 00:00:00 2001 From: Iron-E Date: Thu, 21 May 2020 15:49:53 -0400 Subject: [PATCH] Consistency improvements --- doc/libmodal-lua.txt | 28 +-- examples/lua/key-combos-manually.lua | 29 ++- examples/lua/key-combos-submode.lua | 3 +- examples/lua/key-combos.lua | 2 +- lua/libmodal/src/Mode/Popup.lua | 6 +- lua/libmodal/src/Mode/init.lua | 16 +- lua/libmodal/src/Prompt.lua | 4 +- lua/libmodal/src/Vars.lua | 6 +- lua/libmodal/src/classes.lua | 13 + lua/libmodal/src/collections/ParseTable.lua | 258 +++++++++++--------- lua/libmodal/src/collections/Stack.lua | 11 +- lua/libmodal/src/globals.lua | 4 +- lua/libmodal/src/init.lua | 1 + lua/libmodal/src/utils/Help.lua | 25 +- lua/libmodal/src/utils/WindowState.lua | 6 +- 15 files changed, 229 insertions(+), 183 deletions(-) create mode 100644 lua/libmodal/src/classes.lua diff --git a/doc/libmodal-lua.txt b/doc/libmodal-lua.txt index 7547379..4909338 100644 --- a/doc/libmodal-lua.txt +++ b/doc/libmodal-lua.txt @@ -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)) < ============================================================================== diff --git a/examples/lua/key-combos-manually.lua b/examples/lua/key-combos-manually.lua index 16b6f94..798f30b 100644 --- a/examples/lua/key-combos-manually.lua +++ b/examples/lua/key-combos-manually.lua @@ -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) diff --git a/examples/lua/key-combos-submode.lua b/examples/lua/key-combos-submode.lua index 8e362bd..df97e55 100644 --- a/examples/lua/key-combos-submode.lua +++ b/examples/lua/key-combos-submode.lua @@ -1,6 +1,7 @@ local libmodal = require('libmodal') + local fooModeRecurse = 0 -local fooModeCombos = { +local fooModeCombos = { ['z'] = 'lua fooMode()' } diff --git a/examples/lua/key-combos.lua b/examples/lua/key-combos.lua index 90b10e4..df72a0b 100644 --- a/examples/lua/key-combos.lua +++ b/examples/lua/key-combos.lua @@ -2,7 +2,7 @@ local libmodal = require('libmodal') local fooModeCombos = { ['zf'] = 'split', ['zfo'] = 'vsplit', - ['zfc'] = 'tabnew' + ['zfc'] = 'q' } libmodal.mode.enter('FOO', fooModeCombos) diff --git a/lua/libmodal/src/Mode/Popup.lua b/lua/libmodal/src/Mode/Popup.lua index 03ebcd2..03b96bc 100644 --- a/lua/libmodal/src/Mode/Popup.lua +++ b/lua/libmodal/src/Mode/Popup.lua @@ -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 diff --git a/lua/libmodal/src/Mode/init.lua b/lua/libmodal/src/Mode/init.lua index 5c8983f..a474a93 100644 --- a/lua/libmodal/src/Mode/init.lua +++ b/lua/libmodal/src/Mode/init.lua @@ -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 diff --git a/lua/libmodal/src/Prompt.lua b/lua/libmodal/src/Prompt.lua index c9625af..2211b89 100644 --- a/lua/libmodal/src/Prompt.lua +++ b/lua/libmodal/src/Prompt.lua @@ -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: diff --git a/lua/libmodal/src/Vars.lua b/lua/libmodal/src/Vars.lua index c9843f1..25cfb52 100644 --- a/lua/libmodal/src/Vars.lua +++ b/lua/libmodal/src/Vars.lua @@ -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 diff --git a/lua/libmodal/src/classes.lua b/lua/libmodal/src/classes.lua new file mode 100644 index 0000000..9cfc53c --- /dev/null +++ b/lua/libmodal/src/classes.lua @@ -0,0 +1,13 @@ +local classes = {} + +-------------------------- +--[[ SUMMARY: + * Define a class-metatable. +]] +-------------------------- +function classes.new(base) + base.__index = base + return base +end + +return classes diff --git a/lua/libmodal/src/collections/ParseTable.lua b/lua/libmodal/src/collections/ParseTable.lua index 575e448..11f3fe8 100644 --- a/lua/libmodal/src/collections/ParseTable.lua +++ b/lua/libmodal/src/collections/ParseTable.lua @@ -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 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 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 --[[ diff --git a/lua/libmodal/src/collections/Stack.lua b/lua/libmodal/src/collections/Stack.lua index ad54bbd..bcd9c3c 100644 --- a/lua/libmodal/src/collections/Stack.lua +++ b/lua/libmodal/src/collections/Stack.lua @@ -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 diff --git a/lua/libmodal/src/globals.lua b/lua/libmodal/src/globals.lua index ce20645..dd7be7b 100644 --- a/lua/libmodal/src/globals.lua +++ b/lua/libmodal/src/globals.lua @@ -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 diff --git a/lua/libmodal/src/init.lua b/lua/libmodal/src/init.lua index 5721287..9a931ac 100644 --- a/lua/libmodal/src/init.lua +++ b/lua/libmodal/src/init.lua @@ -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') diff --git a/lua/libmodal/src/utils/Help.lua b/lua/libmodal/src/utils/Help.lua index 8bbea78..127f67a 100644 --- a/lua/libmodal/src/utils/Help.lua +++ b/lua/libmodal/src/utils/Help.lua @@ -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] = '--------------' diff --git a/lua/libmodal/src/utils/WindowState.lua b/lua/libmodal/src/utils/WindowState.lua index 5aef1e7..129b30d 100644 --- a/lua/libmodal/src/utils/WindowState.lua +++ b/lua/libmodal/src/utils/WindowState.lua @@ -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