Created
April 20, 2025 11:52
-
-
Save SmallJoker/263c1544608d4e0d4554288120d93258 to your computer and use it in GitHub Desktop.
Luanti: Convert lua_api.md to the CudaText format for autocompletion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Copyright (C) SmallJoker 2022 - 2025 | |
-- License: MIT | |
-- This script imports the lua_api.md file from Luanti/Minetest to CudaText for | |
-- auto-completion. Tested with LuaJIT and a Luanti 5.12.0-dev file. | |
-- Usage: | |
-- 1. $ luajit THIS_SCRIPT.lua /path/to/lua_api.md | |
-- 2. Append the generated file to $HOME/.config/cudatext/data/autocomplete/Lua.acp | |
-- 3. Restart CudaText | |
local args = { ... } | |
assert(type(args[1]) == "string", "Arg #1: Need file path") | |
local ENTRIES_LIMIT = 9999 -- debugging | |
local SIG_PATTERN = "%*%s`(%l+)%.([%a_]+)([^`]*)`" | |
local file = io.open(args[1], "r") | |
local output = {} | |
local TABLE_WHITELIST = { | |
core = true, | |
math = true, | |
--minetest = true, -- only for older API files | |
string = true, | |
table = true, | |
vector = true, | |
} | |
local entries = { | |
-- [signature] = { args, desc } | |
} | |
local line | |
local line_handled = true | |
while ENTRIES_LIMIT > 0 do | |
-- Preserve line from previous iteration if needed | |
if line_handled then | |
line = file:read("*l") | |
if not line then | |
break -- end of file | |
end | |
end | |
line_handled = true | |
repeat -- breakable | |
local s_table, s_field, s_args = line:match(SIG_PATTERN) | |
if not s_field then | |
break | |
end | |
if not TABLE_WHITELIST[s_table] then | |
print("Unknown table: " .. s_table) | |
break | |
end | |
local sig = s_table .. "." .. s_field | |
-- table.func_tion(arg1, arg2) | |
-- table.variable | |
local sig_args = s_table .. "." .. s_field .. s_args | |
local is_func = s_args:find('%(') | |
local description = line:match("`:%s*(.+)") | |
if not is_func and entries[sig] and entries[sig][1] then | |
break -- already have a function signature, do not overwrite. | |
end | |
if description then | |
-- * `table.field(arg1, arg2): Description` | |
local fullstop = description:find("%.") | |
while not fullstop do | |
line = file:read("*l") | |
-- Ignore lines with leading '*' | |
local is_different = line:match("^%s*%*(.+)") | |
if is_different then | |
line_handled = false | |
break | |
end | |
line = line:match("%s*(.*)%s*") -- trim spaces | |
fullstop = line:find("%.") | |
description = description .. " " | |
.. (fullstop and line:sub(1, fullstop) or line) | |
end | |
else | |
-- Description line | |
line = file:read("*l") | |
description = line:match("%s*%* (.+)") | |
-- Avoid matching a subsequent new function/variable doc line | |
local _, s_field2, _ = line:match(SIG_PATTERN) | |
if s_field2 or not description then | |
description = "" | |
line_handled = false | |
end | |
end | |
entries[sig] = { is_func and s_args, description } | |
ENTRIES_LIMIT = ENTRIES_LIMIT - 1 | |
until true -- once | |
end | |
local output = {} | |
for k, v in pairs(entries) do | |
if v[1] then | |
table.insert(output, "function " .. k .. v[1] .. " |" .. v[2]) | |
else | |
table.insert(output, "variable " .. k .. " |" .. v[2]) | |
end | |
end | |
entries = nil | |
table.sort(output) | |
--print(table.concat(output, '\n')) | |
local f_out_name = "00_out_Lua_acp_segment.txt" | |
do | |
local f_out = io.open(f_out_name, "w") | |
f_out:write(table.concat(output, '\n')) | |
end | |
print("Wrote to ", f_out_name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment