diff options
author | Folke Lemaitre <folke.lemaitre@gmail.com> | 2022-09-28 13:22:08 +0200 |
---|---|---|
committer | Lewis Russell <lewis6991@gmail.com> | 2022-10-06 15:42:21 +0100 |
commit | 1da7b4eb699fb04cc97dec389470fd0fbd64091d (patch) | |
tree | 6c060f502d43e88cc7d69fe37e31b8a65316c5b2 /scripts/lua2dox.lua | |
parent | 24a1c7f556bba35a9c31c2fdd19cf4b8c00a4395 (diff) | |
download | rneovim-1da7b4eb699fb04cc97dec389470fd0fbd64091d.tar.gz rneovim-1da7b4eb699fb04cc97dec389470fd0fbd64091d.tar.bz2 rneovim-1da7b4eb699fb04cc97dec389470fd0fbd64091d.zip |
feat: added support for specifying types for lua2dox
Diffstat (limited to 'scripts/lua2dox.lua')
-rw-r--r-- | scripts/lua2dox.lua | 69 |
1 files changed, 41 insertions, 28 deletions
diff --git a/scripts/lua2dox.lua b/scripts/lua2dox.lua index d93bb6080f..674b42a699 100644 --- a/scripts/lua2dox.lua +++ b/scripts/lua2dox.lua @@ -159,6 +159,7 @@ end --! \param Str --! \param Pattern --! \returns table of string fragments +---@return string[] local function string_split(Str, Pattern) local splitStr = {} local fpat = "(.-)" .. Pattern @@ -369,6 +370,9 @@ local function checkComment4fn(Fn_magic,MagicLines) return fn_magic end + +local types = {"number", "string", "table", "list", "boolean", "function"} + --! \brief run the filter function TLua2DoX_filter.readfile(this,AppStamp,Filename) local inStream = TStream_Read() @@ -408,6 +412,19 @@ function TLua2DoX_filter.readfile(this,AppStamp,Filename) local magic = string.sub(line, 4 + offset) local magic_split = string_split(magic, ' ') + if magic_split[1] == 'param' then + for _, type in ipairs(types) do + magic = magic:gsub("^param%s+([a-zA-Z_?]+)%s+.*%((" .. type .. ")%)", "param %1 %2" ) + magic = magic:gsub("^param%s+([a-zA-Z_?]+)%s+.*%((" .. type .. "|nil)%)", "param %1 %2" ) + end + magic_split = string_split(magic, ' ') + elseif magic_split[1] == 'return' then + for _, type in ipairs(types) do + magic = magic:gsub("^return%s+.*%((" .. type .. ")%)", "return %1" ) + magic = magic:gsub("^return%s+.*%((" .. type .. "|nil)%)", "return %1" ) + end + magic_split = string_split(magic, ' ') + end if magic_split[1] == "generic" then local generic_name, generic_type = line:match("@generic%s*(%w+)%s*:?%s*(.*)") @@ -415,39 +432,35 @@ function TLua2DoX_filter.readfile(this,AppStamp,Filename) generic_type = "any" end generic[generic_name] = generic_type - end - - local type_index = 2 - if magic_split[1] == 'param' then - type_index = type_index + 1 - if magic_split[type_index] and magic_split[2]:find("%?$") then - magic_split[type_index] = magic_split[type_index] .. "|nil" - magic_split[2] = magic_split[2]:sub(1, -2) + else + local type_index = 2 + if magic_split[1] == 'param' then + type_index = type_index + 1 end - end - if magic_split[type_index] then - for k, v in pairs(generic) do - magic_split[type_index] = magic_split[type_index]:gsub(k, v) + if magic_split[type_index] then + -- fix optional parameters + if magic_split[type_index] and magic_split[2]:find("%?$") then + if not magic_split[type_index]:find("nil") then + magic_split[type_index] = magic_split[type_index] .. "|nil" + end + magic_split[2] = magic_split[2]:sub(1, -2) + end + -- replace generic types + if magic_split[type_index] then + for k, v in pairs(generic) do + magic_split[type_index] = magic_split[type_index]:gsub(k, v) + end + end + -- surround some types by () + for _, type in ipairs(types) do + magic_split[type_index] = magic_split[type_index]:gsub("^(" .. type .. "|nil):?$", "(%1)") + magic_split[type_index] = magic_split[type_index]:gsub("^(" .. type .. "):?$", "(%1)") + end end - end - if magic_split[type_index] == 'number' or - magic_split[type_index] == 'number|nil' or - magic_split[type_index] == 'string' or - magic_split[type_index] == 'string|nil' or - magic_split[type_index] == 'table' or - magic_split[type_index] == 'table|nil' or - magic_split[type_index] == 'boolean' or - magic_split[type_index] == 'boolean|nil' or - magic_split[type_index] == 'function' or - magic_split[type_index] == 'function|nil' - then - magic_split[type_index] = '(' .. magic_split[type_index] .. ')' - end - magic = table.concat(magic_split, ' ') + magic = table.concat(magic_split, ' ') - if magic_split[1] ~= "generic" then outStream:writeln('/// @' .. magic) fn_magic = checkComment4fn(fn_magic,magic) end |