Merge #11276 from h-michael/module-pattern

Use module pattern with vim/shared.lua
This commit is contained in:
Justin M. Keyes 2019-10-27 15:43:38 -07:00 committed by GitHub
commit 5a212a96a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 73 deletions

View File

@ -4,34 +4,37 @@
-- test-suite. If, in the future, Nvim itself is used to run the test-suite -- test-suite. If, in the future, Nvim itself is used to run the test-suite
-- instead of "vanilla Lua", these functions could move to src/nvim/lua/vim.lua -- instead of "vanilla Lua", these functions could move to src/nvim/lua/vim.lua
local vim = {}
--- Returns a deep copy of the given object. Non-table objects are copied as --- Returns a deep copy of the given object. Non-table objects are copied as
--- in a typical Lua assignment, whereas table objects are copied recursively. --- in a typical Lua assignment, whereas table objects are copied recursively.
--- ---
--@param orig Table to copy --@param orig Table to copy
--@returns New table of copied keys and (nested) values. --@returns New table of copied keys and (nested) values.
local function deepcopy(orig) function vim.deepcopy(orig) end -- luacheck: no unused
error(orig) vim.deepcopy = (function()
end local function _id(v)
local function _id(v) return v
return v end
end
local deepcopy_funcs = { local deepcopy_funcs = {
table = function(orig) table = function(orig)
local copy = {} local copy = {}
for k, v in pairs(orig) do for k, v in pairs(orig) do
copy[deepcopy(k)] = deepcopy(v) copy[vim.deepcopy(k)] = vim.deepcopy(v)
end end
return copy return copy
end, end,
number = _id, number = _id,
string = _id, string = _id,
['nil'] = _id, ['nil'] = _id,
boolean = _id, boolean = _id,
} }
deepcopy = function(orig)
return deepcopy_funcs[type(orig)](orig) return function(orig)
end return deepcopy_funcs[type(orig)](orig)
end
end)()
--- Splits a string at each instance of a separator. --- Splits a string at each instance of a separator.
--- ---
@ -43,7 +46,7 @@ end
--@param sep Separator string or pattern --@param sep Separator string or pattern
--@param plain If `true` use `sep` literally (passed to String.find) --@param plain If `true` use `sep` literally (passed to String.find)
--@returns Iterator over the split components --@returns Iterator over the split components
local function gsplit(s, sep, plain) function vim.gsplit(s, sep, plain)
assert(type(s) == "string", string.format("Expected string, got %s", type(s))) assert(type(s) == "string", string.format("Expected string, got %s", type(s)))
assert(type(sep) == "string", string.format("Expected string, got %s", type(sep))) assert(type(sep) == "string", string.format("Expected string, got %s", type(sep)))
assert(type(plain) == "boolean" or type(plain) == "nil", string.format("Expected boolean or nil, got %s", type(plain))) assert(type(plain) == "boolean" or type(plain) == "nil", string.format("Expected boolean or nil, got %s", type(plain)))
@ -92,8 +95,8 @@ end
--@param sep Separator string or pattern --@param sep Separator string or pattern
--@param plain If `true` use `sep` literally (passed to String.find) --@param plain If `true` use `sep` literally (passed to String.find)
--@returns List-like table of the split components. --@returns List-like table of the split components.
local function split(s,sep,plain) function vim.split(s,sep,plain)
local t={} for c in gsplit(s, sep, plain) do table.insert(t,c) end local t={} for c in vim.gsplit(s, sep, plain) do table.insert(t,c) end
return t return t
end end
@ -102,7 +105,7 @@ end
--@param t Table to check --@param t Table to check
--@param value Value to compare --@param value Value to compare
--@returns true if `t` contains `value` --@returns true if `t` contains `value`
local function tbl_contains(t, value) function vim.tbl_contains(t, value)
assert(type(t) == 'table', string.format("Expected table, got %s", type(t))) assert(type(t) == 'table', string.format("Expected table, got %s", type(t)))
for _,v in ipairs(t) do for _,v in ipairs(t) do
@ -122,7 +125,7 @@ end
--- - "keep": use value from the leftmost map --- - "keep": use value from the leftmost map
--- - "force": use value from the rightmost map --- - "force": use value from the rightmost map
--@param ... Two or more map-like tables. --@param ... Two or more map-like tables.
local function tbl_extend(behavior, ...) function vim.tbl_extend(behavior, ...)
if (behavior ~= 'error' and behavior ~= 'keep' and behavior ~= 'force') then if (behavior ~= 'error' and behavior ~= 'keep' and behavior ~= 'force') then
error('invalid "behavior": '..tostring(behavior)) error('invalid "behavior": '..tostring(behavior))
end end
@ -149,7 +152,7 @@ end
--- ---
--@param t List-like table --@param t List-like table
--@returns Flattened copy of the given list-like table. --@returns Flattened copy of the given list-like table.
local function tbl_flatten(t) function vim.tbl_flatten(t)
-- From https://github.com/premake/premake-core/blob/master/src/base/table.lua -- From https://github.com/premake/premake-core/blob/master/src/base/table.lua
local result = {} local result = {}
local function _tbl_flatten(_t) local function _tbl_flatten(_t)
@ -172,7 +175,7 @@ end
--@see https://www.lua.org/pil/20.2.html --@see https://www.lua.org/pil/20.2.html
--@param s String to trim --@param s String to trim
--@returns String with whitespace removed from its beginning and end --@returns String with whitespace removed from its beginning and end
local function trim(s) function vim.trim(s)
assert(type(s) == 'string', string.format("Expected string, got %s", type(s))) assert(type(s) == 'string', string.format("Expected string, got %s", type(s)))
return s:match('^%s*(.*%S)') or '' return s:match('^%s*(.*%S)') or ''
end end
@ -182,19 +185,9 @@ end
--@see https://github.com/rxi/lume --@see https://github.com/rxi/lume
--@param s String to escape --@param s String to escape
--@returns %-escaped pattern string --@returns %-escaped pattern string
local function pesc(s) function vim.pesc(s)
assert(type(s) == 'string', string.format("Expected string, got %s", type(s))) assert(type(s) == 'string', string.format("Expected string, got %s", type(s)))
return s:gsub('[%(%)%.%%%+%-%*%?%[%]%^%$]', '%%%1') return s:gsub('[%(%)%.%%%+%-%*%?%[%]%^%$]', '%%%1')
end end
local module = { return vim
deepcopy = deepcopy,
gsplit = gsplit,
pesc = pesc,
split = split,
tbl_contains = tbl_contains,
tbl_extend = tbl_extend,
tbl_flatten = tbl_flatten,
trim = trim,
}
return module

View File

@ -543,7 +543,6 @@ function TLua2DoX_filter.readfile(this,AppStamp,Filename)
local fn = TString_removeCommentFromLine(string_trim(string.sub(line,pos_fn+8))) local fn = TString_removeCommentFromLine(string_trim(string.sub(line,pos_fn+8)))
if fn_magic then if fn_magic then
fn = fn_magic fn = fn_magic
fn_magic = nil
end end
if string.sub(fn,1,1)=='(' then if string.sub(fn,1,1)=='(' then
@ -554,49 +553,20 @@ function TLua2DoX_filter.readfile(this,AppStamp,Filename)
-- want to fix for iffy declarations -- want to fix for iffy declarations
local open_paren = string.find(fn,'[%({]') local open_paren = string.find(fn,'[%({]')
local fn0 = fn
if open_paren then if open_paren then
fn0 = string.sub(fn,1,open_paren-1)
-- we might have a missing close paren -- we might have a missing close paren
if not string.find(fn,'%)') then if not string.find(fn,'%)') then
fn = fn .. ' ___MissingCloseParenHere___)' fn = fn .. ' ___MissingCloseParenHere___)'
end end
end end
local dot = string.find(fn0,'[%.:]') -- add vanilla function
if dot then -- it's a method outStream:writeln(fn_type .. 'function ' .. fn .. '{}')
local klass = string.sub(fn,1,dot-1)
local method = string.sub(fn,dot+1)
--TCore_IO_writeln('function ' .. klass .. '::' .. method .. ftail .. '{}')
--TCore_IO_writeln(klass .. '::' .. method .. ftail .. '{}')
outStream:writeln(
'/*! \\memberof ' .. klass .. ' */ '
.. method .. '{}'
)
else
-- add vanilla function
outStream:writeln(fn_type .. 'function ' .. fn .. '{}')
end
end end
else else
this:warning(inStream:getLineNo(),'something weird here') this:warning(inStream:getLineNo(),'something weird here')
end end
fn_magic = nil -- mustn't indavertently use it again fn_magic = nil -- mustn't indavertently use it again
elseif string.find(line,'=%s*class%(') then
state = 'in_class' -- it's a class declaration
local tailComment
line,tailComment = TString_removeCommentFromLine(line)
local equals = string.find(line,'=')
local klass = string_trim(string.sub(line,1,equals-1))
local tail = string_trim(string.sub(line,equals+1))
-- class(wibble wibble)
-- ....v.
local parent = string.sub(tail,7,-2)
if #parent>0 then
parent = ' :public ' .. parent
end
outStream:writeln('class ' .. klass .. parent .. '{};')
else else
state = '' -- unknown state = '' -- unknown
if #line>0 then -- we don't know what this line means, so just comment it out if #line>0 then -- we don't know what this line means, so just comment it out