~fgaz/minetest-lls-definitions

fb0cec50232e610da2ec1febda8a1f3c1ecdc9da — Francesco Gazzetta a month ago 9c2438b master
Add table functions
3 files changed, 92 insertions(+), 2 deletions(-)

M gen.tcl
A library/table.lua
A templates/table.lua
M gen.tcl => gen.tcl +2 -2
@@ 11,7 11,7 @@ while {[gets $md line] >= 0} {
  if {[regexp {^    (.*)} $line _ docline]} then {
    if {$is_fun} then {dict append fun_dict $fun_name "\n--- $docline"}
  } else {
    set is_fun [regexp {^\* `((?:minetest|vector)\.\w+)\(} $line _ fun_name]
    set is_fun [regexp {^\* `((?:minetest|vector|table)\.\w+)\(} $line _ fun_name]
    if {$is_fun} then {dict set fun_dict $fun_name [list]}
  }
}


@@ 23,7 23,7 @@ foreach file [glob "templates/*.lua"] {
  set in [open $file]
  set out [open "library/[file tail $file]" w]
  while {[gets $in line] >= 0} {
    if {[regexp {^function ((?:minetest|vector)\.\w+)\(} $line _ fun_name]} then {
    if {[regexp {^function ((?:minetest|vector|table)\.\w+)\(} $line _ fun_name]} then {
      set docstring [string trim [dict get $fun_dict $fun_name]]
      if {$docstring != ""} then {puts $out [string trim [dict get $fun_dict $fun_name]]}
    }

A library/table.lua => library/table.lua +52 -0
@@ 0,0 1,52 @@
---@meta

-- We follow sections and order from lua_api.md

-- NOTE: `table` is already defined in the lua standard library, so there is no
-- need to set it to {}

-- Helper functions
-------------------

---@generic T : table
---@param table T
---@return T
---@nodiscard
--- * returns a deep copy of `table`
function table.copy(table) end

---@generic V
---@param list V[]
---@param val V
---@return integer
---@nodiscard
---   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.
function table.indexof(list, val) end

---@param table table
---@param other_table table
--- * Appends all values in `other_table` to `table` - uses `#table + 1` to
---   find new indices.
function table.insert_all(table, other_table) end

---@generic K, V
---@param t table<K, V>
---@return table<V, K>
---@nodiscard
--- * If multiple keys in `t` map to the same value, it is unspecified which
---   value maps to that key.
function table.key_value_swap(t) end

---@param table
---@param from?
---@param to?
---@param random_func?
--- * Shuffles elements `from` to `to` in `table` in place
--- * `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.
function table.shuffle(table, [from], [to], [random_func]) end

A templates/table.lua => templates/table.lua +38 -0
@@ 0,0 1,38 @@
---@meta

-- We follow sections and order from lua_api.md

-- NOTE: `table` is already defined in the lua standard library, so there is no
-- need to set it to {}

-- Helper functions
-------------------

---@generic T : table
---@param table T
---@return T
---@nodiscard
function table.copy(table) end

---@generic V
---@param list V[]
---@param val V
---@return integer
---@nodiscard
function table.indexof(list, val) end

---@param table table
---@param other_table table
function table.insert_all(table, other_table) end

---@generic K, V
---@param t table<K, V>
---@return table<V, K>
---@nodiscard
function table.key_value_swap(t) end

---@param table
---@param from?
---@param to?
---@param random_func?
function table.shuffle(table, [from], [to], [random_func]) end