mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 11:28:22 +00:00
refactor: use kwargs parameter in vim.split
This commit is contained in:
@@ -1373,23 +1373,25 @@ pesc({s}) *vim.pesc()*
|
|||||||
See also: ~
|
See also: ~
|
||||||
https://github.com/rxi/lume
|
https://github.com/rxi/lume
|
||||||
|
|
||||||
split({s}, {sep}, {plain}, {trimempty}) *vim.split()*
|
split({s}, {sep}, {kwargs}) *vim.split()*
|
||||||
Splits a string at each instance of a separator.
|
Splits a string at each instance of a separator.
|
||||||
|
|
||||||
Examples: >
|
Examples: >
|
||||||
|
|
||||||
split(":aa::b:", ":") --> {'','aa','','b',''}
|
split(":aa::b:", ":") --> {'','aa','','b',''}
|
||||||
split("axaby", "ab?") --> {'','x','y'}
|
split("axaby", "ab?") --> {'','x','y'}
|
||||||
split("x*yz*o", "*", true) --> {'x','yz','o'}
|
split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
||||||
split("|x|y|z|", "|", true, true) --> {'x', 'y', 'z'}
|
split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
||||||
<
|
<
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
{s} String to split
|
{s} String to split
|
||||||
{sep} Separator string or pattern
|
{sep} Separator string or pattern
|
||||||
{plain} If `true` use `sep` literally (passed to
|
{kwargs} Keyword arguments:
|
||||||
String.find)
|
• plain: (boolean) If `true` use `sep` literally
|
||||||
{trimempty} If `true` remove empty items from the front
|
(passed to string.find)
|
||||||
and back of the list
|
• trimempty: (boolean) If `true` remove empty
|
||||||
|
items from the front and back of the list
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
List-like table of the split components.
|
List-like table of the split components.
|
||||||
|
@@ -98,20 +98,32 @@ end
|
|||||||
--- <pre>
|
--- <pre>
|
||||||
--- split(":aa::b:", ":") --> {'','aa','','b',''}
|
--- split(":aa::b:", ":") --> {'','aa','','b',''}
|
||||||
--- split("axaby", "ab?") --> {'','x','y'}
|
--- split("axaby", "ab?") --> {'','x','y'}
|
||||||
--- split("x*yz*o", "*", true) --> {'x','yz','o'}
|
--- split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
||||||
--- split("|x|y|z|", "|", true, true) --> {'x', 'y', 'z'}
|
--- split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
||||||
--- </pre>
|
--- </pre>
|
||||||
---
|
---
|
||||||
---@see |vim.gsplit()|
|
---@see |vim.gsplit()|
|
||||||
---
|
---
|
||||||
---@param s String to split
|
---@param s String to split
|
||||||
---@param sep Separator string or pattern
|
---@param sep Separator string or pattern
|
||||||
---@param plain If `true` use `sep` literally (passed to String.find)
|
---@param kwargs Keyword arguments:
|
||||||
---@param trimempty If `true` remove empty items from the front and back of the list
|
--- - plain: (boolean) If `true` use `sep` literally (passed to string.find)
|
||||||
|
--- - trimempty: (boolean) If `true` remove empty items from the front
|
||||||
|
--- and back of the list
|
||||||
---@returns List-like table of the split components.
|
---@returns List-like table of the split components.
|
||||||
function vim.split(s, sep, plain, trimempty)
|
function vim.split(s, sep, kwargs)
|
||||||
-- Only need to validate trimempty since the rest are validated by vim.gsplit
|
local plain
|
||||||
vim.validate{trimempty={trimempty, 'b', true}}
|
local trimempty = false
|
||||||
|
if type(kwargs) == 'boolean' then
|
||||||
|
-- Support old signature for backward compatibility
|
||||||
|
plain = kwargs
|
||||||
|
else
|
||||||
|
vim.validate { kwargs = {kwargs, 't', true} }
|
||||||
|
kwargs = kwargs or {}
|
||||||
|
plain = kwargs.plain
|
||||||
|
trimempty = kwargs.trimempty
|
||||||
|
end
|
||||||
|
|
||||||
local t = {}
|
local t = {}
|
||||||
local skip = trimempty
|
local skip = trimempty
|
||||||
for c in vim.gsplit(s, sep, plain) do
|
for c in vim.gsplit(s, sep, plain) do
|
||||||
|
@@ -237,8 +237,8 @@ describe('lua stdlib', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it("vim.split", function()
|
it("vim.split", function()
|
||||||
local split = function(str, sep, plain, trimempty)
|
local split = function(str, sep, kwargs)
|
||||||
return exec_lua('return vim.split(...)', str, sep, plain, trimempty)
|
return exec_lua('return vim.split(...)', str, sep, kwargs)
|
||||||
end
|
end
|
||||||
|
|
||||||
local tests = {
|
local tests = {
|
||||||
@@ -259,9 +259,12 @@ describe('lua stdlib', function()
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, t in ipairs(tests) do
|
for _, t in ipairs(tests) do
|
||||||
eq(t[5], split(t[1], t[2], t[3], t[4]))
|
eq(t[5], split(t[1], t[2], {plain=t[3], trimempty=t[4]}))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Test old signature
|
||||||
|
eq({'x', 'yz', 'oo', 'l'}, split("x*yz*oo*l", "*", true))
|
||||||
|
|
||||||
local loops = {
|
local loops = {
|
||||||
{ "abc", ".-" },
|
{ "abc", ".-" },
|
||||||
}
|
}
|
||||||
@@ -285,16 +288,10 @@ describe('lua stdlib', function()
|
|||||||
vim/shared.lua:0: in function <vim/shared.lua:0>]]),
|
vim/shared.lua:0: in function <vim/shared.lua:0>]]),
|
||||||
pcall_err(split, 'string', 1))
|
pcall_err(split, 'string', 1))
|
||||||
eq(dedent([[
|
eq(dedent([[
|
||||||
Error executing lua: vim/shared.lua:0: plain: expected boolean, got number
|
Error executing lua: vim/shared.lua:0: kwargs: expected table, got number
|
||||||
stack traceback:
|
stack traceback:
|
||||||
vim/shared.lua:0: in function 'gsplit'
|
|
||||||
vim/shared.lua:0: in function <vim/shared.lua:0>]]),
|
vim/shared.lua:0: in function <vim/shared.lua:0>]]),
|
||||||
pcall_err(split, 'string', 'string', 1))
|
pcall_err(split, 'string', 'string', 1))
|
||||||
eq(dedent([[
|
|
||||||
Error executing lua: vim/shared.lua:0: trimempty: expected boolean, got number
|
|
||||||
stack traceback:
|
|
||||||
vim/shared.lua:0: in function <vim/shared.lua:0>]]),
|
|
||||||
pcall_err(split, 'string', 'string', false, 42))
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('vim.trim', function()
|
it('vim.trim', function()
|
||||||
|
Reference in New Issue
Block a user