From 4ab48fe83553420c299ede61dbba02e5c7c5baf1 Mon Sep 17 00:00:00 2001 From: alek13 Date: Tue, 23 Sep 2025 04:19:23 +0300 Subject: [PATCH] `ide-helper`: add `string`, `math` & `table` luanti methods annotations. LG-1994 --- lua_ext/math.lua | 35 +++++++++++++++++++++ lua_ext/string.lua | 15 +++++++++ lua_ext/table.lua | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 lua_ext/math.lua create mode 100644 lua_ext/string.lua create mode 100644 lua_ext/table.lua diff --git a/lua_ext/math.lua b/lua_ext/math.lua new file mode 100644 index 0000000..534018c --- /dev/null +++ b/lua_ext/math.lua @@ -0,0 +1,35 @@ +--- @diagnostic disable: missing-return + + +--- Get the hypotenuse of a triangle with legs x and y. Useful for distance calculation. +--- +--- @param x number +--- @param y number +--- +--- @return number +function math.hypot(x, y) end + +--- Get the sign of given number `x`. +--- - returns `-1`, `0` or `1` +--- - if the absolute value of x is within the tolerance or x is NaN, 0 is returned. +--- +--- @param x number +--- @param tolerance? number default: `0.0` +--- +--- @return number +function math.sign(x, tolerance) end + +--- Returns the factorial of `x`. +--- +--- @param x number +--- +--- @return number +function math.factorial(x) end + +--- Returns x rounded to the nearest integer. +--- - At a multiple of 0.5, rounds away from zero. +--- +--- @param x number +--- +--- @return number +function math.round(x) end diff --git a/lua_ext/string.lua b/lua_ext/string.lua new file mode 100644 index 0000000..b82acdf --- /dev/null +++ b/lua_ext/string.lua @@ -0,0 +1,15 @@ +--- @diagnostic disable: missing-return + + +--- e.g. `"a,b":split(",")` returns `{"a","b"}` +--- @param separator? string cannot be empty, default: `","` +--- @param include_empty? boolean default: `false` +--- @param max_splits? number if it's negative, splits aren't limited, default: `-1` +--- @param sep_is_pattern? boolean it specifies whether separator is a plain string or a pattern (regex), default: `false` +--- @return string[] +function string:split(separator, include_empty, max_splits, sep_is_pattern) end + +--- Returns the string without whitespace pre- and suffixes +--- e.g. `"\n \t\tfoo bar\t ":trim()` returns `"foo bar"` +--- @return string +function string:trim() end diff --git a/lua_ext/table.lua b/lua_ext/table.lua new file mode 100644 index 0000000..490371a --- /dev/null +++ b/lua_ext/table.lua @@ -0,0 +1,77 @@ +--- @diagnostic disable: missing-return + + +--- Returns a deep copy of `table` +--- - strips metatables, but this may change in the future +--- +--- @generic T: table +--- @param table T +--- +--- @return T +function table.copy(table) end + +--- Returns a deep copy of `table` with metatable copied. +--- - since `5.12` +--- - `table` can also be non-table value, which will be returned as-is +--- - preserves metatables as they are +--- @since 5.12 +--- @generic T: table +--- @param table T +--- +--- @return T +function table.copy_with_metatables(table) end + +--- Returns the smallest numerical index containing the value `val` in the table list. +--- - Non-numerical indices are ignored. +--- - If `val` could not be found, `-1` is returned. +--- - `list` must not have negative indices. +--- +--- @generic value +--- @param list value[] +--- @param val value +--- +--- @return integer +function table.indexof(list, val) end + +--- Returns the key containing the value val in the table table. +--- - If multiple keys contain `val`, it is unspecified which key will be returned. +--- - If `val` could not be found, `nil` is returned. +--- +--- @generic key, value +--- @param table table +--- @param val value +--- +--- @return key? +function table.keyof(table, val) end + +--- Appends all values in `other_table` to table - uses `#table + 1` to find new indices. +--- +--- @generic value +--- @param table table +--- @param other_table table +--- +--- @return table +function table.insert_all(table, other_table) end + + +--- Returns a table with keys and values swapped +--- - If multiple keys in `t` map to the same value, it is unspecified which value maps to that key. +--- +--- @generic key, value +--- @param t table +--- +--- @return table +function table.key_value_swap(t) end + +--- Shuffles elements `from` to `to` in `table` in place (modifies exectly `table`, use `table.copy()` if needed). +--- * `from` defaults to `1` +--- * `to` defaults to `#table` +--- * `random_func` defaults to `math.random`. This function receives two +--- integers as arguments and should return a random integer inclusively +--- between them. +--- +--- @param table table list to shuffle in. +--- @param from? integer (default: `1`) +--- @param to? integer (default: `#table`) +--- @param random_func? fun(min: integer, max: integer): integer (default: `math.random`) should return a random integer inclusively between `from` & `to`. +function table.shuffle(table, from, to, random_func) end