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 _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

@ -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:

@ -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

@ -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:

@ -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:

@ -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

@ -14,10 +14,67 @@ local globals = require('libmodal/src/globals')
*/
--]]
local ParseTable = {}
local _REGEX_ALL = '.'
-- The number corresponding to <CR> in vim.
ParseTable.CR = 13
local ParseTable = {
['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
@ -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
--------------------------------------------------

@ -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

@ -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

@ -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:

@ -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

Loading…
Cancel
Save