From 861e3008b1bc425c9b62f125dddc2bfa8822002e Mon Sep 17 00:00:00 2001 From: Iron-E Date: Mon, 25 May 2020 19:53:07 -0400 Subject: [PATCH] Refactor `classes` --- lua/libmodal/src/Layer.lua | 5 +- lua/libmodal/src/Mode/Popup.lua | 4 +- lua/libmodal/src/Mode/init.lua | 17 +-- lua/libmodal/src/Prompt.lua | 4 +- lua/libmodal/src/PromptLayer.lua | 0 lua/libmodal/src/Vars.lua | 9 +- lua/libmodal/src/classes.lua | 27 +++- lua/libmodal/src/collections/ParseTable.lua | 136 +++++++++++++------- lua/libmodal/src/collections/Stack.lua | 4 +- lua/libmodal/src/globals.lua | 5 +- lua/libmodal/src/utils/Help.lua | 11 +- lua/libmodal/src/utils/WindowState.lua | 5 +- 12 files changed, 149 insertions(+), 78 deletions(-) create mode 100644 lua/libmodal/src/PromptLayer.lua diff --git a/lua/libmodal/src/Layer.lua b/lua/libmodal/src/Layer.lua index 6e4bdab..0bfd248 100644 --- a/lua/libmodal/src/Layer.lua +++ b/lua/libmodal/src/Layer.lua @@ -13,7 +13,7 @@ local classes = require('libmodal/src/classes') */ --]] -local Layer = {} +local Layer = {['TYPE'] = 'libmodal-layer'} local _BUFFER_CURRENT = 0 local _RESTORED = nil @@ -38,7 +38,7 @@ end */ --]] -local _metaLayer = classes.new({}) +local _metaLayer = classes.new(Layer.TYPE) --------------------------- --[[ SUMMARY: @@ -235,7 +235,6 @@ end ]] ----------------------------------------------------- function Layer.new(name, mappings) - -- TODO: support libmodal-mode and libmodal-prompt layers. return setmetatable( {['_keymap'] = mappings, ['name'] = name}, _metaLayer diff --git a/lua/libmodal/src/Mode/Popup.lua b/lua/libmodal/src/Mode/Popup.lua index 8b678e7..8c457a7 100644 --- a/lua/libmodal/src/Mode/Popup.lua +++ b/lua/libmodal/src/Mode/Popup.lua @@ -13,7 +13,7 @@ local classes = require('libmodal/src/classes') */ --]] -local Popup = {} +local Popup = {['TYPE'] = 'libmodal-popup'} local _winOpenOpts = { ['anchor'] = 'SW', @@ -34,7 +34,7 @@ local _winOpenOpts = { */ --]] -local _metaPopup = classes.new({}) +local _metaPopup = classes.new(Popup.TYPE) --------------------------- --[[ SUMMARY: diff --git a/lua/libmodal/src/Mode/init.lua b/lua/libmodal/src/Mode/init.lua index 5197f9e..2a2ed01 100644 --- a/lua/libmodal/src/Mode/init.lua +++ b/lua/libmodal/src/Mode/init.lua @@ -19,9 +19,10 @@ local api = utils.api */ --]] -local Mode = {} - -Mode.Popup = require('libmodal/src/Mode/Popup') +local Mode = { + ['Popup'] = require('libmodal/src/Mode/Popup'), + ['TYPE'] = 'libmodal-mode' +} local _HELP = '?' local _TIMEOUT = { @@ -39,9 +40,9 @@ _TIMEOUT.NR = string.byte(_TIMEOUT.CHAR) */ --]] -local _metaMode = classes.new({}) +local _metaMode = classes.new(Mode.TYPE) -local _metaInputBytes = classes.new({ +local _metaInputBytes = classes.new(nil, { ['clear'] = function(__self) for i, _ in ipairs(__self) do __self[i] = nil @@ -64,21 +65,21 @@ function _metaMode:_checkInputForMapping() inputBytes[#inputBytes + 1] = self.input:nvimGet() -- Get the command based on the users input. - local cmd = self.mappings:parseGet(inputBytes) + local cmd = self.mappings:get(ParseTable.tableReverse(inputBytes)) -- Get the type of the command. local commandType = type(cmd) local clearInputBytes = false -- if there was no matching command - if cmd == false then + if not cmd then if #inputBytes < 2 and inputBytes[1] == string.byte(_HELP) then self._help:show() end inputBytes:clear() -- The command was a table, meaning that it MIGHT match. elseif commandType == globals.TYPE_TBL - and globals.is_true(self._timeouts.enabled) + and globals.is_true(self._timeouts.enabled) then -- Create a new timer diff --git a/lua/libmodal/src/Prompt.lua b/lua/libmodal/src/Prompt.lua index a58a47f..4b44a84 100644 --- a/lua/libmodal/src/Prompt.lua +++ b/lua/libmodal/src/Prompt.lua @@ -18,7 +18,7 @@ local api = utils.api */ --]] -local Prompt = {} +local Prompt = {['TYPE'] = 'libmodal-prompt'} local _HELP = 'help' local _REPLACEMENTS = { @@ -36,7 +36,7 @@ end */ --]] -local _metaPrompt = classes.new({}) +local _metaPrompt = classes.new(Prompt.TYPE) --------------------------------- --[[ SUMMARY: diff --git a/lua/libmodal/src/PromptLayer.lua b/lua/libmodal/src/PromptLayer.lua new file mode 100644 index 0000000..e69de29 diff --git a/lua/libmodal/src/Vars.lua b/lua/libmodal/src/Vars.lua index 25cfb52..2f062cc 100644 --- a/lua/libmodal/src/Vars.lua +++ b/lua/libmodal/src/Vars.lua @@ -17,7 +17,8 @@ local globals = require('libmodal/src/globals') local _TIMEOUT_GLOBAL_NAME = 'libmodalTimeouts' local Vars = { - [_TIMEOUT_GLOBAL_NAME] = api.nvim_get_var(_TIMEOUT_GLOBAL_NAME) + [_TIMEOUT_GLOBAL_NAME] = api.nvim_get_var(_TIMEOUT_GLOBAL_NAME), + ['TYPE'] = 'libmodal-vars' } --[[ @@ -26,11 +27,7 @@ local Vars = { */ --]] -local _metaVars = classes.new({}) - --- Instances of variables pertaining to a certain mode. -_metaVars._varName = nil -_metaVars._modeName = nil +local _metaVars = classes.new(Vars.TYPE) --------------------------------- --[[ SUMMARY: diff --git a/lua/libmodal/src/classes.lua b/lua/libmodal/src/classes.lua index 9cfc53c..42cdea9 100644 --- a/lua/libmodal/src/classes.lua +++ b/lua/libmodal/src/classes.lua @@ -4,10 +4,29 @@ local classes = {} --[[ SUMMARY: * Define a class-metatable. ]] --------------------------- -function classes.new(base) - base.__index = base - return base +--[[ + * `name` => the name of the class. + * `base` => the base class to use (`{}` by default). +]] +-------------------------------- +function classes.new(name, ...) + -- set self to `base`, or `{}` if nil. + local self = unpack({...}) or {} + + -- set `__index`. + if not self.__index then + self.__index = self + end + + -- set `__type`. + self.__type = name + + return self +end + +------------------------ +function classes.type(v) + return v.__type or type(v) end return classes diff --git a/lua/libmodal/src/collections/ParseTable.lua b/lua/libmodal/src/collections/ParseTable.lua index 3ad1075..7deb44f 100644 --- a/lua/libmodal/src/collections/ParseTable.lua +++ b/lua/libmodal/src/collections/ParseTable.lua @@ -14,10 +14,67 @@ local globals = require('libmodal/src/globals') */ --]] -local ParseTable = {} +local _REGEX_ALL = '.' --- The number corresponding to in vim. -ParseTable.CR = 13 +local ParseTable = { + ['CR'] = 13, -- The number corresponding to in vim. + ['TYPE'] = 'libmodal-parse-table' +} + +------------------------------------------- +--[[ SUMMARY: + * Split some `str` over a `regex`. +]] +--[[ PARAMS: + * `str` => the string to split. + * `regex` => the regex to split `str` with. +]] +--[[ RETURNS: + * The split `str`. +]] +------------------------------------------- +function ParseTable.stringSplit(str, regex) + local split = {} + for char in string.gmatch(str, regex) do + split[#split + 1] = char + end + return split +end + +------------------------------------- +--[[ SUMMARY: + * Reverse the elements of some table. +]] +--[[ PARAMS: + * `tbl` => the table to reverse. +]] +--[[ RETURNS: + * The reversed `tbl`. +]] +------------------------------------- +function ParseTable.tableReverse(tbl) + local reversed = {} + while #reversed < #tbl do + -- look, no variables! + reversed[#reversed + 1] = tbl[#tbl - #reversed] + end + return reversed +end + +------------------------------ +--[[ SUMMARY: + * Parse a `key`. +]] +--[[ PARAMS: + * `key` => the key to parse. +]] +--[[ RETURNS: + * The parsed `key`. +]] +------------------------------ +function ParseTable.parse(key) + return ParseTable.stringSplit(string.reverse(key), _REGEX_ALL) +end ----------------------------------------- --[[ SUMMARY @@ -55,7 +112,7 @@ local function _get(parseTable, splitKey) return val end end - return false + return nil end ----------------------------------------- @@ -88,47 +145,13 @@ local function _put(parseTable, splitKey, value) -- † end end -- ‡ --------------------------------------- ---[[ SUMMARY: - * Split some `str` over a `regex`. -]] ---[[ PARAMS: - * `str` => the string to split. - * `regex` => the regex to split `str` with. -]] --------------------------------------- -local function _string_split(str, regex) - local split = {} - for char in string.gmatch(str, regex) do - split[#split + 1] = char - end - return split -end - ----------------------------------- ---[[ SUMMARY: - * Reverse the elements of some table. -]] ---[[ PARAMS: - * `tbl` => the table to reverse. -]] ----------------------------------- -local function _table_reverse(tbl) - local reversed = {} - while #reversed < #tbl do - -- look, no variables! - reversed[#reversed + 1] = tbl[#tbl - #reversed] - end - return reversed -end - --[[ /* * META `ParseTable` */ --]] -local _metaParseTable = classes.new({}) +local _metaParseTable = classes.new(ParseTable.TYPE) ------------------------------------------ --[[ SUMMARY: @@ -137,14 +160,38 @@ local _metaParseTable = classes.new({}) --[[ PARAMS: * `key` => the PARSED key to get. ]] ---[[ +--[[ RETURNS: * `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)) +function _metaParseTable:get(keyDict) + return _get(self, keyDict) +end + +-------------------------------------- +--[[ SUMMARY: + * Get a value from this `ParseTable`. +]] +--[[ PARAMS: + * `key` => the key to get. +]] +--[[ RETURNS: + * `function` => when `key` is a full match. + * `table` => when the `key` partially mathes. + * `false` => when `key` is not ANYWHERE. +]] +-------------------------------------- +function _metaParseTable:parseGet(key) + local parsedTable = ParseTable.parse(key) + + -- convert all of the strings to bytes. + for i, v in ipairs(parsedTable) do + parsedTable[i] = string.byte(v) + end + + return _get(self, parsedTable) end --------------------------------------------- @@ -157,10 +204,7 @@ end ]] --------------------------------------------- function _metaParseTable:parsePut(key, value) - _put(self, - _string_split(string.reverse(key), '.'), - value - ) + _put(self, ParseTable.parse(key), value) end -------------------------------------------------- diff --git a/lua/libmodal/src/collections/Stack.lua b/lua/libmodal/src/collections/Stack.lua index bcd9c3c..7e483ee 100644 --- a/lua/libmodal/src/collections/Stack.lua +++ b/lua/libmodal/src/collections/Stack.lua @@ -12,7 +12,7 @@ local classes = require('libmodal/src/classes') */ --]] -local Stack = {} +local Stack = {['TYPE'] = 'libmodal-stack'} --[[ /* @@ -20,7 +20,7 @@ local Stack = {} */ --]] -local _metaStack = classes.new({}) +local _metaStack = classes.new(Stack.TYPE) _metaStack._len = 0 _metaStack._top = nil diff --git a/lua/libmodal/src/globals.lua b/lua/libmodal/src/globals.lua index dd7be7b..fedcacb 100644 --- a/lua/libmodal/src/globals.lua +++ b/lua/libmodal/src/globals.lua @@ -13,11 +13,14 @@ local globals = {} --]] globals.DEFAULT_ERROR_TITLE = 'vim-libmodal error' + globals.ESC_NR = 27 + globals.TYPE_FUNC = 'function' -globals.TYPE_NUM = 'number' +globals.TYPE_NUM = 'number' globals.TYPE_STR = 'string' globals.TYPE_TBL = 'table' + globals.VIM_FALSE = 0 globals.VIM_TRUE = 1 diff --git a/lua/libmodal/src/utils/Help.lua b/lua/libmodal/src/utils/Help.lua index afd62ff..9f97823 100644 --- a/lua/libmodal/src/utils/Help.lua +++ b/lua/libmodal/src/utils/Help.lua @@ -6,13 +6,21 @@ local classes = require('libmodal/src/classes') +--[[ + /* + * MODULE + */ +--]] + +local Help = {['TYPE'] = 'libmodal-help'} + --[[ /* * META `Help` */ --]] -local _metaHelp = classes.new({}) +local _metaHelp = classes.new(Help.TYPE) ------------------------- --[[ SUMMARY: @@ -32,7 +40,6 @@ end */ --]] -local Help = {} ---------------------------------------- --[[ SUMMARY: diff --git a/lua/libmodal/src/utils/WindowState.lua b/lua/libmodal/src/utils/WindowState.lua index 129b30d..3a8a6ec 100644 --- a/lua/libmodal/src/utils/WindowState.lua +++ b/lua/libmodal/src/utils/WindowState.lua @@ -6,6 +6,7 @@ local api = require('libmodal/src/utils/api') local classes = require('libmodal/src/classes') +local globals = require('libmodal/src/globals') --[[ /* @@ -13,7 +14,7 @@ local classes = require('libmodal/src/classes') */ --]] -local WindowState = {} +local WindowState = {['TYPE'] = 'libmodal-window-state'} local height = 'winheight' local width = 'winwidth' @@ -24,7 +25,7 @@ local width = 'winwidth' */ --]] -local _metaWindowState = classes.new({}) +local _metaWindowState = classes.new(WindowState.TYPE) ----------------------------------- --[[ SUMMARY