Home Explore Blog CI



neovim

29th chunk of `runtime/doc/lua.txt`
6511e761384175f6abfc632f508916ff7331cf9efb108e000000000100000fed
 string

    See also: ~
      • https://github.com/rxi/lume

vim.ringbuf({size})                                            *vim.ringbuf()*
    Create a ring buffer limited to a maximal number of items. Once the buffer
    is full, adding a new entry overrides the oldest entry. >lua
        local ringbuf = vim.ringbuf(4)
        ringbuf:push("a")
        ringbuf:push("b")
        ringbuf:push("c")
        ringbuf:push("d")
        ringbuf:push("e")    -- overrides "a"
        print(ringbuf:pop()) -- returns "b"
        print(ringbuf:pop()) -- returns "c"

        -- Can be used as iterator. Pops remaining items:
        for val in ringbuf do
          print(val)
        end
<

    Returns a Ringbuf instance with the following methods:
    • |Ringbuf:push()|
    • |Ringbuf:pop()|
    • |Ringbuf:peek()|
    • |Ringbuf:clear()|

    Parameters: ~
      • {size}  (`integer`)

    Return: ~
        (`vim.Ringbuf`) ringbuf See |vim.Ringbuf|.

vim.spairs({t})                                                 *vim.spairs()*
    Enumerates key-value pairs of a table, ordered by key.

    Parameters: ~
      • {t}  (`table`) Dict-like table

    Return (multiple): ~
        (`fun(table: table<K, V>, index?: K):K, V`) |for-in| iterator over
        sorted keys and their values
        (`table`)

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

vim.split({s}, {sep}, {opts})                                    *vim.split()*
    Splits a string at each instance of a separator and returns the result as
    a table (unlike |vim.gsplit()|).

    Examples: >lua
        split(":aa::b:", ":")                   --> {'','aa','','b',''}
        split("axaby", "ab?")                   --> {'','x','y'}
        split("x*yz*o", "*", {plain=true})      --> {'x','yz','o'}
        split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
<

    Parameters: ~
      • {s}     (`string`) String to split
      • {sep}   (`string`) Separator or pattern
      • {opts}  (`table?`) Keyword arguments |kwargs|:
                • {plain}? (`boolean`) Use `sep` literally (as in
                  string.find).
                • {trimempty}? (`boolean`) Discard empty segments at start and
                  end of the sequence.

    Return: ~
        (`string[]`) List of split components

    See also: ~
      • |vim.gsplit()|
      • |string.gmatch()|

vim.startswith({s}, {prefix})                               *vim.startswith()*
    Tests if `s` starts with `prefix`.

    Parameters: ~
      • {s}       (`string`) String
      • {prefix}  (`string`) Prefix to match

    Return: ~
        (`boolean`) `true` if `prefix` is a prefix of `s`

vim.tbl_contains({t}, {value}, {opts})                    *vim.tbl_contains()*
    Checks if a table contains a given value, specified either directly or via
    a predicate that is checked for each value.

    Example: >lua
        vim.tbl_contains({ 'a', { 'b', 'c' } }, function(v)
          return vim.deep_equal(v, { 'b', 'c' })
        end, { predicate = true })
        -- true
<

    Parameters: ~
      • {t}      (`table`) Table to check
      • {value}  (`any`) Value to compare or predicate function reference
      • {opts}   (`table?`) Keyword arguments |kwargs|:
                 • {predicate}? (`boolean`) `value` is a function reference to
                   be checked (default false)

    Return: ~
        (`boolean`) `true` if `t` contains `value`

    See also: ~
      • |vim.list_contains()| for checking values in list-like tables

vim.tbl_count({t})                                           *vim.tbl_count()*
    Counts the number of non-nil values in table `t`. >lua
        vim.tbl_count({ a=1, b=2 })  --> 2
        vim.tbl_count({ 1, 2 })      --> 2
<

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

    Return: ~
        (`integer`) Number of non-nil values in table

    See also: ~
      • https://github.com/Tieske/Penlight/blob/master/lua/pl/tablex.lua

vim.tbl_deep_extend({behavior},

Title: Lua API: vim.spairs(), vim.split(), vim.startswith(), vim.tbl_contains(), vim.tbl_count()
Summary
This section details several table and string manipulation functions within the Neovim Lua API. It describes `vim.spairs()` for iterating over key-value pairs of a table ordered by key, `vim.split()` for splitting a string into a table based on a separator, `vim.startswith()` for testing if a string starts with a given prefix, `vim.tbl_contains()` for checking if a table contains a specific value (or if any value satisfies a given predicate), and `vim.tbl_count()` which counts the number of non-nil values in a table.