Refactor `classes`

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

@ -13,7 +13,7 @@ local classes = require('libmodal/src/classes')
*/ */
--]] --]]
local Layer = {} local Layer = {['TYPE'] = 'libmodal-layer'}
local _BUFFER_CURRENT = 0 local _BUFFER_CURRENT = 0
local _RESTORED = nil local _RESTORED = nil
@ -38,7 +38,7 @@ end
*/ */
--]] --]]
local _metaLayer = classes.new({}) local _metaLayer = classes.new(Layer.TYPE)
--------------------------- ---------------------------
--[[ SUMMARY: --[[ SUMMARY:
@ -235,7 +235,6 @@ end
]] ]]
----------------------------------------------------- -----------------------------------------------------
function Layer.new(name, mappings) function Layer.new(name, mappings)
-- TODO: support libmodal-mode and libmodal-prompt layers.
return setmetatable( return setmetatable(
{['_keymap'] = mappings, ['name'] = name}, {['_keymap'] = mappings, ['name'] = name},
_metaLayer _metaLayer

@ -13,7 +13,7 @@ local classes = require('libmodal/src/classes')
*/ */
--]] --]]
local Popup = {} local Popup = {['TYPE'] = 'libmodal-popup'}
local _winOpenOpts = { local _winOpenOpts = {
['anchor'] = 'SW', ['anchor'] = 'SW',
@ -34,7 +34,7 @@ local _winOpenOpts = {
*/ */
--]] --]]
local _metaPopup = classes.new({}) local _metaPopup = classes.new(Popup.TYPE)
--------------------------- ---------------------------
--[[ SUMMARY: --[[ SUMMARY:

@ -19,9 +19,10 @@ local api = utils.api
*/ */
--]] --]]
local Mode = {} local Mode = {
['Popup'] = require('libmodal/src/Mode/Popup'),
Mode.Popup = require('libmodal/src/Mode/Popup') ['TYPE'] = 'libmodal-mode'
}
local _HELP = '?' local _HELP = '?'
local _TIMEOUT = { 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) ['clear'] = function(__self)
for i, _ in ipairs(__self) do for i, _ in ipairs(__self) do
__self[i] = nil __self[i] = nil
@ -64,21 +65,21 @@ function _metaMode:_checkInputForMapping()
inputBytes[#inputBytes + 1] = self.input:nvimGet() inputBytes[#inputBytes + 1] = self.input:nvimGet()
-- Get the command based on the users input. -- 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. -- Get the type of the command.
local commandType = type(cmd) local commandType = type(cmd)
local clearInputBytes = false local clearInputBytes = false
-- if there was no matching command -- if there was no matching command
if cmd == false then if not cmd then
if #inputBytes < 2 and inputBytes[1] == string.byte(_HELP) then if #inputBytes < 2 and inputBytes[1] == string.byte(_HELP) then
self._help:show() self._help:show()
end end
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.is_true(self._timeouts.enabled) and globals.is_true(self._timeouts.enabled)
then then
-- Create a new timer -- Create a new timer

@ -18,7 +18,7 @@ local api = utils.api
*/ */
--]] --]]
local Prompt = {} local Prompt = {['TYPE'] = 'libmodal-prompt'}
local _HELP = 'help' local _HELP = 'help'
local _REPLACEMENTS = { local _REPLACEMENTS = {
@ -36,7 +36,7 @@ end
*/ */
--]] --]]
local _metaPrompt = classes.new({}) local _metaPrompt = classes.new(Prompt.TYPE)
--------------------------------- ---------------------------------
--[[ SUMMARY: --[[ SUMMARY:

@ -17,7 +17,8 @@ local globals = require('libmodal/src/globals')
local _TIMEOUT_GLOBAL_NAME = 'libmodalTimeouts' local _TIMEOUT_GLOBAL_NAME = 'libmodalTimeouts'
local Vars = { 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({}) local _metaVars = classes.new(Vars.TYPE)
-- Instances of variables pertaining to a certain mode.
_metaVars._varName = nil
_metaVars._modeName = nil
--------------------------------- ---------------------------------
--[[ SUMMARY: --[[ SUMMARY:

@ -4,10 +4,29 @@ local classes = {}
--[[ SUMMARY: --[[ SUMMARY:
* Define a class-metatable. * Define a class-metatable.
]] ]]
-------------------------- --[[
function classes.new(base) * `name` => the name of the class.
base.__index = base * `base` => the base class to use (`{}` by default).
return base ]]
--------------------------------
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 end
return classes return classes

@ -14,10 +14,67 @@ local globals = require('libmodal/src/globals')
*/ */
--]] --]]
local ParseTable = {} local _REGEX_ALL = '.'
-- The number corresponding to <CR> in vim. local ParseTable = {
ParseTable.CR = 13 ['CR'] = 13, -- The number corresponding to <CR> 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 --[[ SUMMARY
@ -55,7 +112,7 @@ local function _get(parseTable, splitKey)
return val return val
end end
end end
return false return nil
end end
----------------------------------------- -----------------------------------------
@ -88,47 +145,13 @@ local function _put(parseTable, splitKey, value) -- †
end end
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` * META `ParseTable`
*/ */
--]] --]]
local _metaParseTable = classes.new({}) local _metaParseTable = classes.new(ParseTable.TYPE)
------------------------------------------ ------------------------------------------
--[[ SUMMARY: --[[ SUMMARY:
@ -137,14 +160,38 @@ local _metaParseTable = classes.new({})
--[[ PARAMS: --[[ PARAMS:
* `key` => the PARSED key to get. * `key` => the PARSED key to get.
]] ]]
--[[ --[[ RETURNS:
* `function` => when `key` is a full match. * `function` => when `key` is a full match.
* `table` => when the `key` partially mathes. * `table` => when the `key` partially mathes.
* `false` => when `key` is not ANYWHERE. * `false` => when `key` is not ANYWHERE.
]] ]]
------------------------------------------ ------------------------------------------
function _metaParseTable:parseGet(keyDict) function _metaParseTable:get(keyDict)
return _get(self, _table_reverse(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 end
--------------------------------------------- ---------------------------------------------
@ -157,10 +204,7 @@ end
]] ]]
--------------------------------------------- ---------------------------------------------
function _metaParseTable:parsePut(key, value) function _metaParseTable:parsePut(key, value)
_put(self, _put(self, ParseTable.parse(key), value)
_string_split(string.reverse(key), '.'),
value
)
end end
-------------------------------------------------- --------------------------------------------------

@ -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._len = 0
_metaStack._top = nil _metaStack._top = nil

@ -13,11 +13,14 @@ local globals = {}
--]] --]]
globals.DEFAULT_ERROR_TITLE = 'vim-libmodal error' globals.DEFAULT_ERROR_TITLE = 'vim-libmodal error'
globals.ESC_NR = 27 globals.ESC_NR = 27
globals.TYPE_FUNC = 'function' globals.TYPE_FUNC = 'function'
globals.TYPE_NUM = 'number' globals.TYPE_NUM = 'number'
globals.TYPE_STR = 'string' globals.TYPE_STR = 'string'
globals.TYPE_TBL = 'table' globals.TYPE_TBL = 'table'
globals.VIM_FALSE = 0 globals.VIM_FALSE = 0
globals.VIM_TRUE = 1 globals.VIM_TRUE = 1

@ -6,13 +6,21 @@
local classes = require('libmodal/src/classes') local classes = require('libmodal/src/classes')
--[[
/*
* MODULE
*/
--]]
local Help = {['TYPE'] = 'libmodal-help'}
--[[ --[[
/* /*
* META `Help` * META `Help`
*/ */
--]] --]]
local _metaHelp = classes.new({}) local _metaHelp = classes.new(Help.TYPE)
------------------------- -------------------------
--[[ SUMMARY: --[[ SUMMARY:
@ -32,7 +40,6 @@ end
*/ */
--]] --]]
local Help = {}
---------------------------------------- ----------------------------------------
--[[ SUMMARY: --[[ SUMMARY:

@ -6,6 +6,7 @@
local api = require('libmodal/src/utils/api') local api = require('libmodal/src/utils/api')
local classes = require('libmodal/src/classes') 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 height = 'winheight'
local width = 'winwidth' local width = 'winwidth'
@ -24,7 +25,7 @@ local width = 'winwidth'
*/ */
--]] --]]
local _metaWindowState = classes.new({}) local _metaWindowState = classes.new(WindowState.TYPE)
----------------------------------- -----------------------------------
--[[ SUMMARY --[[ SUMMARY

Loading…
Cancel
Save