refactor: flatten documented callback metadata

This commit is contained in:
Zef Hemel
2026-07-16 09:12:29 +02:00
parent 37d6c59555
commit 2020fbb16d
19 changed files with 1120 additions and 1373 deletions
+164 -206
View File
@@ -40,24 +40,20 @@ export const stringApi = new LuaTable({
}
return new LuaMultiRes(result);
},
documentation: {
description:
"Returns the numeric character codes in the inclusive range from `i` to `j`.",
parameters: [
{ name: "s", type: "string" },
{ name: "i", type: "integer", optional: true },
{ name: "j", type: "integer", optional: true },
],
returns: [{ type: "integer", description: "One result per character." }],
},
description:
"Returns the numeric character codes in the inclusive range from `i` to `j`.",
parameters: [
{ name: "s", type: "string" },
{ name: "i", type: "integer", optional: true },
{ name: "j", type: "integer", optional: true },
],
returns: [{ type: "integer", description: "One result per character." }],
}),
char: new LuaBuiltinFunction({
callback: (_sf, ...args: number[]) => String.fromCharCode(...args),
documentation: {
description: "Creates a string from numeric character codes.",
signatures: ["string.char(...): string"],
returns: [{ type: "string" }],
},
description: "Creates a string from numeric character codes.",
signatures: ["string.char(...): string"],
returns: [{ type: "string" }],
}),
find: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string, init = 1, plain = false) => {
@@ -69,20 +65,18 @@ export const stringApi = new LuaTable({
}
return new LuaMultiRes(result);
},
documentation: {
description:
"Finds the first Lua-pattern match and returns its bounds followed by captures.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
{ name: "init", type: "integer", optional: true },
{ name: "plain", type: "boolean", optional: true },
],
returns: [
{ type: "integer|nil", description: "Start index or `nil`." },
{ type: "integer", description: "End index." },
],
},
description:
"Finds the first Lua-pattern match and returns its bounds followed by captures.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
{ name: "init", type: "integer", optional: true },
{ name: "plain", type: "boolean", optional: true },
],
returns: [
{ type: "integer|nil", description: "Start index or `nil`." },
{ type: "integer", description: "End index." },
],
}),
format: new LuaBuiltinFunction({
callback: (_sf, format: string, ...args: any[]) => {
@@ -91,21 +85,19 @@ export const stringApi = new LuaTable({
}
return luaFormat(format, ...args);
},
documentation: {
description: "Formats values according to a C-style format string.",
signatures: ["string.format(format, ...): string"],
parameters: [
{ name: "format", type: "string" },
{
name: "...",
description: "Values consumed by conversion specifiers.",
},
],
returns: [{ type: "string" }],
examples: [
{ code: 'print(string.format("Name: %s, score: %.1f", "Ada", 9.5))' },
],
},
description: "Formats values according to a C-style format string.",
signatures: ["string.format(format, ...): string"],
parameters: [
{ name: "format", type: "string" },
{
name: "...",
description: "Values consumed by conversion specifiers.",
},
],
returns: [{ type: "string" }],
examples: [
{ code: 'print(string.format("Name: %s, score: %.1f", "Ada", 9.5))' },
],
}),
gmatch: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string, init = 1) => {
@@ -116,21 +108,19 @@ export const stringApi = new LuaTable({
return capturesToLua(caps);
};
},
documentation: {
description:
"Returns an iterator over successive Lua-pattern matches and captures.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
{ name: "init", type: "integer", optional: true },
],
returns: [{ type: "function", description: "Match iterator." }],
examples: [
{
code: 'for word in string.gmatch("hello world", "%w+") do\n print(word)\nend',
},
],
},
description:
"Returns an iterator over successive Lua-pattern matches and captures.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
{ name: "init", type: "integer", optional: true },
],
returns: [{ type: "function", description: "Match iterator." }],
examples: [
{
code: 'for word in string.gmatch("hello world", "%w+") do\n print(word)\nend',
},
],
}),
gsub: new LuaBuiltinFunction({
callback: async (sf, s: string, pattern: string, repl: any, n?: number) => {
@@ -166,49 +156,41 @@ export const stringApi = new LuaTable({
const [result, count] = await patternGsub(s, pattern, callbacks, n);
return new LuaMultiRes([result, count]);
},
documentation: {
description:
"Replaces Lua-pattern matches using a string, table, or function replacement.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
{ name: "replacement", type: "string|table|function" },
{ name: "n", type: "integer", optional: true },
],
returns: [
{ type: "string", description: "Result string." },
{ type: "integer", description: "Number of replacements." },
],
examples: [
{
code: 'local result, count = string.gsub("hello hello", "hello", "hi", 1)\nprint(result, count) -- hi hello 1',
},
],
},
description:
"Replaces Lua-pattern matches using a string, table, or function replacement.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
{ name: "replacement", type: "string|table|function" },
{ name: "n", type: "integer", optional: true },
],
returns: [
{ type: "string", description: "Result string." },
{ type: "integer", description: "Number of replacements." },
],
examples: [
{
code: 'local result, count = string.gsub("hello hello", "hello", "hi", 1)\nprint(result, count) -- hi hello 1',
},
],
}),
len: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.length,
documentation: {
description: "Returns the length of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "integer" }],
},
description: "Returns the length of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "integer" }],
}),
lower: new LuaBuiltinFunction({
callback: (_sf, s: string) => luaToString(s.toLowerCase()),
documentation: {
description: "Returns a copy of a string converted to lowercase.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
description: "Returns a copy of a string converted to lowercase.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
}),
upper: new LuaBuiltinFunction({
callback: (_sf, s: string) => luaToString(s.toUpperCase()),
documentation: {
description: "Returns a copy of a string converted to uppercase.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
description: "Returns a copy of a string converted to uppercase.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
}),
match: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string, init = 1) => {
@@ -216,19 +198,17 @@ export const stringApi = new LuaTable({
if (!caps) return null;
return capturesToLua(caps);
},
documentation: {
description:
"Returns captures from the first Lua-pattern match, or `nil` when none is found.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
{ name: "init", type: "integer", optional: true },
],
returns: [{ description: "Pattern captures, whole match, or `nil`." }],
examples: [
{ code: 'local year, month = string.match("2024-03", "(%d+)%-(%d+)")' },
],
},
description:
"Returns captures from the first Lua-pattern match, or `nil` when none is found.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
{ name: "init", type: "integer", optional: true },
],
returns: [{ description: "Pattern captures, whole match, or `nil`." }],
examples: [
{ code: 'local year, month = string.match("2024-03", "(%d+)%-(%d+)")' },
],
}),
rep: new LuaBuiltinFunction({
callback: (_sf, s: string, n: number, sep?: string) => {
@@ -240,24 +220,20 @@ export const stringApi = new LuaTable({
}
return parts.join(sep);
},
documentation: {
description:
"Returns `n` copies of a string joined by an optional separator.",
parameters: [
{ name: "s", type: "string" },
{ name: "n", type: "integer" },
{ name: "sep", type: "string", optional: true },
],
returns: [{ type: "string" }],
},
description:
"Returns `n` copies of a string joined by an optional separator.",
parameters: [
{ name: "s", type: "string" },
{ name: "n", type: "integer" },
{ name: "sep", type: "string", optional: true },
],
returns: [{ type: "string" }],
}),
reverse: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.split("").reverse().join(""),
documentation: {
description: "Returns a string with its characters in reverse order.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
description: "Returns a string with its characters in reverse order.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
}),
sub: new LuaBuiltinFunction({
callback: (_sf, s: string, i: number, j?: number) => {
@@ -285,34 +261,30 @@ export const stringApi = new LuaTable({
}
return "";
},
documentation: {
description:
"Returns the substring from inclusive index `i` through `j`, supporting negative indices.",
parameters: [
{ name: "s", type: "string" },
{ name: "i", type: "integer" },
{ name: "j", type: "integer", optional: true },
],
returns: [{ type: "string" }],
},
description:
"Returns the substring from inclusive index `i` through `j`, supporting negative indices.",
parameters: [
{ name: "s", type: "string" },
{ name: "i", type: "integer" },
{ name: "j", type: "integer", optional: true },
],
returns: [{ type: "string" }],
}),
split: new LuaBuiltinFunction({
callback: (_sf, s: string, sep: string) => s.split(sep),
documentation: {
description:
"Splits a string on a literal separator and returns the substrings.",
parameters: [
{ name: "s", type: "string" },
{ name: "sep", type: "string" },
],
returns: [{ type: "table" }],
examples: [
{
code: 'for part in each(string.split("a,b,c", ",")) do\n print(part)\nend',
},
],
},
description:
"Splits a string on a literal separator and returns the substrings.",
parameters: [
{ name: "s", type: "string" },
{ name: "sep", type: "string" },
],
returns: [{ type: "table" }],
examples: [
{
code: 'for part in each(string.split("a,b,c", ",")) do\n print(part)\nend',
},
],
}),
pack: strPackFn,
@@ -322,49 +294,39 @@ export const stringApi = new LuaTable({
// Non-standard extensions
startsWith: new LuaBuiltinFunction({
callback: (_sf, s: string, prefix: string) => s.startsWith(prefix),
documentation: {
description: "Returns whether a string starts with a literal prefix.",
parameters: [
{ name: "s", type: "string" },
{ name: "prefix", type: "string" },
],
returns: [{ type: "boolean" }],
},
description: "Returns whether a string starts with a literal prefix.",
parameters: [
{ name: "s", type: "string" },
{ name: "prefix", type: "string" },
],
returns: [{ type: "boolean" }],
}),
endsWith: new LuaBuiltinFunction({
callback: (_sf, s: string, suffix: string) => s.endsWith(suffix),
documentation: {
description: "Returns whether a string ends with a literal suffix.",
parameters: [
{ name: "s", type: "string" },
{ name: "suffix", type: "string" },
],
returns: [{ type: "boolean" }],
},
description: "Returns whether a string ends with a literal suffix.",
parameters: [
{ name: "s", type: "string" },
{ name: "suffix", type: "string" },
],
returns: [{ type: "boolean" }],
}),
trim: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.trim(),
documentation: {
description: "Removes whitespace from both ends of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
description: "Removes whitespace from both ends of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
}),
trimStart: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.trimStart(),
documentation: {
description: "Removes whitespace from the beginning of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
description: "Removes whitespace from the beginning of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
}),
trimEnd: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.trimEnd(),
documentation: {
description: "Removes whitespace from the end of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
description: "Removes whitespace from the end of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
}),
matchRegex: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string) => {
@@ -372,20 +334,18 @@ export const stringApi = new LuaTable({
const result = s.match(regex);
return jsToLuaValue(result);
},
documentation: {
description:
"Matches a string with a JavaScript regular expression and returns the match array.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
],
returns: [{ type: "table|nil" }],
examples: [
{
code: 'local match = string.matchRegex("hello123", "([a-z]+)([0-9]+)")\nprint(match[1], match[2], match[3])',
},
],
},
description:
"Matches a string with a JavaScript regular expression and returns the match array.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
],
returns: [{ type: "table|nil" }],
examples: [
{
code: 'local match = string.matchRegex("hello123", "([a-z]+)([0-9]+)")\nprint(match[1], match[2], match[3])',
},
],
}),
matchRegexAll: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string) => {
@@ -398,21 +358,19 @@ export const stringApi = new LuaTable({
return jsToLuaValue(match);
};
},
documentation: {
description:
"Returns an iterator over all JavaScript regular-expression matches.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
],
returns: [
{ type: "function", description: "Iterator yielding match arrays." },
],
examples: [
{
code: 'for match in string.matchRegexAll("a1b2", "([a-z])([0-9])") do\n print(match[1], match[2], match[3])\nend',
},
],
},
description:
"Returns an iterator over all JavaScript regular-expression matches.",
parameters: [
{ name: "s", type: "string" },
{ name: "pattern", type: "string" },
],
returns: [
{ type: "function", description: "Iterator yielding match arrays." },
],
examples: [
{
code: 'for match in string.matchRegexAll("a1b2", "([a-z])([0-9])") do\n print(match[1], match[2], match[3])\nend',
},
],
}),
});