Home Explore Blog CI



neovim

31th chunk of `runtime/doc/lua.txt`
bfa71c1308d13f568ff81ad51408587bf2831da325fcc00b0000000100000fc0
 tables

    Return: ~
        (`table`) Merged table

    See also: ~
      • |extend()|

vim.tbl_filter({func}, {t})                                 *vim.tbl_filter()*
    Filter a table using a predicate function

    Parameters: ~
      • {func}  (`function`) Function
      • {t}     (`table`) Table

    Return: ~
        (`any[]`) Table of filtered values

vim.tbl_get({o}, {...})                                        *vim.tbl_get()*
    Index into a table (first argument) via string keys passed as subsequent
    arguments. Return `nil` if the key does not exist.

    Examples: >lua
        vim.tbl_get({ key = { nested_key = true }}, 'key', 'nested_key') == true
        vim.tbl_get({ key = {}}, 'key', 'nested_key') == nil
<

    Parameters: ~
      • {o}    (`table`) Table to index
      • {...}  (`any`) Optional keys (0 or more, variadic) via which to index
               the table

    Return: ~
        (`any`) Nested value indexed by key (if it exists), else nil

vim.tbl_isempty({t})                                       *vim.tbl_isempty()*
    Checks if a table is empty.

    Parameters: ~
      • {t}  (`table`) Table to check

    Return: ~
        (`boolean`) `true` if `t` is empty

    See also: ~
      • https://github.com/premake/premake-core/blob/master/src/base/table.lua

vim.tbl_keys({t})                                             *vim.tbl_keys()*
    Return a list of all keys used in a table. However, the order of the
    return table of keys is not guaranteed.

    Parameters: ~
      • {t}  (`table`) Table

    Return: ~
        (`any[]`) List of keys

    See also: ~
      • From
        https://github.com/premake/premake-core/blob/master/src/base/table.lua

vim.tbl_map({func}, {t})                                       *vim.tbl_map()*
    Apply a function to all values of a table.

    Parameters: ~
      • {func}  (`fun(value: T): any`) Function
      • {t}     (`table<any, T>`) Table

    Return: ~
        (`table`) Table of transformed values

vim.tbl_values({t})                                         *vim.tbl_values()*
    Return a list of all values used in a table. However, the order of the
    return table of values is not guaranteed.

    Parameters: ~
      • {t}  (`table`) Table

    Return: ~
        (`any[]`) List of values

vim.trim({s})                                                     *vim.trim()*
    Trim whitespace (Lua pattern "%s") from both sides of a string.

    Parameters: ~
      • {s}  (`string`) String to trim

    Return: ~
        (`string`) String with whitespace removed from its beginning and end

    See also: ~
      • |lua-pattern|s
      • https://www.lua.org/pil/20.2.html

                                                              *vim.validate()*
vim.validate({name}, {value}, {validator}, {optional}, {message})
    Validate function arguments.

    This function has two valid forms:
    1. `vim.validate(name, value, validator[, optional][, message])`
       Validates that argument {name} with value {value} satisfies
       {validator}. If {optional} is given and is `true`, then {value} may be
       `nil`. If {message} is given, then it is used as the expected type in
       the error message.
       Example: >lua
         function vim.startswith(s, prefix)
          vim.validate('s', s, 'string')
          vim.validate('prefix', prefix, 'string')
          -- ...
        end
<
    2. `vim.validate(spec)` (deprecated) where `spec` is of type
       `table<string,[value:any, validator: vim.validate.Validator, optional_or_msg? : boolean|string]>)`
       Validates a argument specification. Specs are evaluated in alphanumeric
       order, until the first failure.
       Example: >lua
         function user.new(name, age, hobbies)
          vim.validate{
            name={name, 'string'},
            age={age, 'number'},
            hobbies={hobbies, 'table'},
          }
          -- ...
        end
<

    Examples with explicit argument values (can be run directly):

Title: Lua API: Table Functions and Argument Validation
Summary
This section details several table manipulation functions in the Neovim Lua API, including `vim.tbl_filter()`, `vim.tbl_get()`, `vim.tbl_isempty()`, `vim.tbl_keys()`, `vim.tbl_map()`, and `vim.tbl_values()`. It also covers `vim.trim()` for removing whitespace from strings and `vim.validate()` for validating function arguments, with examples illustrating their usage.