mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 03:18:16 +00:00
feat(vim.gsplit): gain features of vim.split
Problem:
- vim.split has more features than vim.gsplit.
- Cannot inspect the "separator" segments of vim.split or vim.gsplit.
Solution:
- Move common implementation from vim.split into vim.gsplit.
- TODO: deprecate vim.split in favor of vim.totable(vim.gsplit())?
- Introduce `keepsep` parameter.
Related: 84f66909e4
This commit is contained in:
@@ -1649,14 +1649,25 @@ endswith({s}, {suffix}) *vim.endswith()*
|
|||||||
Return: ~
|
Return: ~
|
||||||
(boolean) `true` if `suffix` is a suffix of `s`
|
(boolean) `true` if `suffix` is a suffix of `s`
|
||||||
|
|
||||||
gsplit({s}, {sep}, {plain}) *vim.gsplit()*
|
gsplit({s}, {sep}, {opts}) *vim.gsplit()*
|
||||||
Splits a string at each instance of a separator.
|
Splits a string at each instance of a separator.
|
||||||
|
|
||||||
|
Example: >lua
|
||||||
|
|
||||||
|
for s in vim.gsplit(':aa::b:', ':', {plain=true}) do
|
||||||
|
print(s)
|
||||||
|
end
|
||||||
|
<
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {s} (string) String to split
|
• {s} (string) String to split
|
||||||
• {sep} (string) Separator or pattern
|
• {sep} (string) Separator or pattern
|
||||||
• {plain} (boolean|nil) If `true` use `sep` literally (passed to
|
• {opts} (table|nil) Keyword arguments |kwargs|:
|
||||||
string.find)
|
• keepsep: (boolean) Return segments matching `sep` instead of
|
||||||
|
discarding them.
|
||||||
|
• plain: (boolean) Use `sep` literally (as in string.find).
|
||||||
|
• trimempty: (boolean) Discard empty segments at start and end
|
||||||
|
of the sequence.
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
(function) Iterator over the split components
|
(function) Iterator over the split components
|
||||||
@@ -1729,7 +1740,7 @@ spairs({t}) *vim.spairs()*
|
|||||||
See also: ~
|
See also: ~
|
||||||
• Based on https://github.com/premake/premake-core/blob/master/src/base/table.lua
|
• Based on https://github.com/premake/premake-core/blob/master/src/base/table.lua
|
||||||
|
|
||||||
split({s}, {sep}, {kwargs}) *vim.split()*
|
split({s}, {sep}, {opts}) *vim.split()*
|
||||||
Splits a string at each instance of a separator.
|
Splits a string at each instance of a separator.
|
||||||
|
|
||||||
Examples: >lua
|
Examples: >lua
|
||||||
@@ -1738,16 +1749,14 @@ split({s}, {sep}, {kwargs}) *vim.split()*
|
|||||||
split("axaby", "ab?") --> {'','x','y'}
|
split("axaby", "ab?") --> {'','x','y'}
|
||||||
split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
||||||
split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
||||||
|
split("|x|y|z|", "|", {keepsep=true}) --> {'|', 'x', '|', 'y', '|', 'z', '|'}
|
||||||
<
|
<
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {s} (string) String to split
|
• {s} (string) String to split
|
||||||
• {sep} (string) Separator or pattern
|
• {sep} (string) Separator or pattern
|
||||||
• {kwargs} (table|nil) Keyword arguments:
|
• {opts} (table|nil) Keyword arguments |kwargs| accepted by
|
||||||
• plain: (boolean) If `true` use `sep` literally (passed to
|
|vim.gsplit()|
|
||||||
string.find)
|
|
||||||
• trimempty: (boolean) If `true` remove empty items from the
|
|
||||||
front and back of the list
|
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
string[] List of split components
|
string[] List of split components
|
||||||
|
@@ -132,6 +132,8 @@ The following new APIs or features were added.
|
|||||||
• |vim.fs.dir()| now has a `opts` argument with a depth field to allow
|
• |vim.fs.dir()| now has a `opts` argument with a depth field to allow
|
||||||
recursively searching a directory tree.
|
recursively searching a directory tree.
|
||||||
|
|
||||||
|
• |vim.gsplit()| supports all features of |vim.split()|.
|
||||||
|
|
||||||
• |vim.secure.read()| reads a file and prompts the user if it should be
|
• |vim.secure.read()| reads a file and prompts the user if it should be
|
||||||
trusted and, if so, returns the file's contents.
|
trusted and, if so, returns the file's contents.
|
||||||
|
|
||||||
|
@@ -59,6 +59,13 @@ end)()
|
|||||||
|
|
||||||
--- Splits a string at each instance of a separator.
|
--- Splits a string at each instance of a separator.
|
||||||
---
|
---
|
||||||
|
--- Example:
|
||||||
|
--- <pre>lua
|
||||||
|
--- for s in vim.gsplit(':aa::b:', ':', {plain=true}) do
|
||||||
|
--- print(s)
|
||||||
|
--- end
|
||||||
|
--- </pre>
|
||||||
|
---
|
||||||
---@see |vim.split()|
|
---@see |vim.split()|
|
||||||
---@see |luaref-patterns|
|
---@see |luaref-patterns|
|
||||||
---@see https://www.lua.org/pil/20.2.html
|
---@see https://www.lua.org/pil/20.2.html
|
||||||
@@ -66,17 +73,40 @@ end)()
|
|||||||
---
|
---
|
||||||
---@param s string String to split
|
---@param s string String to split
|
||||||
---@param sep string Separator or pattern
|
---@param sep string Separator or pattern
|
||||||
---@param plain (boolean|nil) If `true` use `sep` literally (passed to string.find)
|
---@param opts (table|nil) Keyword arguments |kwargs|:
|
||||||
---@return fun():string (function) Iterator over the split components
|
--- - keepsep: (boolean) Include segments matching `sep` instead of discarding them.
|
||||||
function vim.gsplit(s, sep, plain)
|
--- - plain: (boolean) Use `sep` literally (as in string.find).
|
||||||
vim.validate({ s = { s, 's' }, sep = { sep, 's' }, plain = { plain, 'b', true } })
|
--- - trimempty: (boolean) Discard empty segments at start and end of the sequence.
|
||||||
|
---@return fun():string|nil (function) Iterator over the split components
|
||||||
|
function vim.gsplit(s, sep, opts)
|
||||||
|
local plain
|
||||||
|
local trimempty = false
|
||||||
|
local keepsep = false
|
||||||
|
if type(opts) == 'boolean' then
|
||||||
|
plain = opts -- For backwards compatibility.
|
||||||
|
else
|
||||||
|
vim.validate({ s = { s, 's' }, sep = { sep, 's' }, opts = { opts, 't', true } })
|
||||||
|
opts = opts or {}
|
||||||
|
plain, trimempty, keepsep = opts.plain, opts.trimempty, opts.keepsep
|
||||||
|
assert(not trimempty or not keepsep, 'keepsep+trimempty not supported')
|
||||||
|
end
|
||||||
|
|
||||||
local start = 1
|
local start = 1
|
||||||
local done = false
|
local done = false
|
||||||
|
local sepseg = nil -- Last matched `sep` segment.
|
||||||
|
local sepesc = plain and vim.pesc(sep) or sep
|
||||||
|
|
||||||
|
-- For `trimempty`:
|
||||||
|
local empty_start = true -- Only empty segments seen so far.
|
||||||
|
local empty_segs = 0 -- Empty segments found between non-empty segments.
|
||||||
|
local nonemptyseg = nil
|
||||||
|
|
||||||
local function _pass(i, j, ...)
|
local function _pass(i, j, ...)
|
||||||
if i then
|
if i then
|
||||||
assert(j + 1 > start, 'Infinite loop detected')
|
assert(j + 1 > start, 'Infinite loop detected')
|
||||||
|
if keepsep then
|
||||||
|
sepseg = s:match(sepesc, start)
|
||||||
|
end
|
||||||
local seg = s:sub(start, i - 1)
|
local seg = s:sub(start, i - 1)
|
||||||
start = j + 1
|
start = j + 1
|
||||||
return seg, ...
|
return seg, ...
|
||||||
@@ -87,16 +117,48 @@ function vim.gsplit(s, sep, plain)
|
|||||||
end
|
end
|
||||||
|
|
||||||
return function()
|
return function()
|
||||||
if done or (s == '' and sep == '') then
|
if trimempty and empty_segs > 0 then
|
||||||
return
|
-- trimempty: Pop the collected empty segments.
|
||||||
end
|
empty_segs = empty_segs - 1
|
||||||
if sep == '' then
|
return ''
|
||||||
|
elseif trimempty and nonemptyseg then
|
||||||
|
local seg = nonemptyseg
|
||||||
|
nonemptyseg = nil
|
||||||
|
return seg
|
||||||
|
elseif keepsep and sepseg then
|
||||||
|
local seg = sepseg
|
||||||
|
sepseg = nil
|
||||||
|
return seg
|
||||||
|
elseif done or (s == '' and sep == '') then
|
||||||
|
return nil
|
||||||
|
elseif sep == '' then
|
||||||
if start == #s then
|
if start == #s then
|
||||||
done = true
|
done = true
|
||||||
end
|
end
|
||||||
return _pass(start + 1, start)
|
return _pass(start + 1, start)
|
||||||
end
|
end
|
||||||
return _pass(s:find(sep, start, plain))
|
|
||||||
|
local seg = _pass(s:find(sep, start, plain))
|
||||||
|
|
||||||
|
-- Trim empty segments from start/end.
|
||||||
|
if trimempty and seg == '' then
|
||||||
|
while not done and seg == '' do
|
||||||
|
empty_segs = empty_segs + 1
|
||||||
|
seg = _pass(s:find(sep, start, plain))
|
||||||
|
end
|
||||||
|
if done and seg == '' then
|
||||||
|
return nil
|
||||||
|
elseif empty_start then
|
||||||
|
empty_start = false
|
||||||
|
empty_segs = 0
|
||||||
|
return seg
|
||||||
|
end
|
||||||
|
nonemptyseg = seg ~= '' and seg or nil
|
||||||
|
seg = ''
|
||||||
|
empty_segs = empty_segs - 1
|
||||||
|
end
|
||||||
|
|
||||||
|
return seg
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -108,51 +170,21 @@ end
|
|||||||
--- split("axaby", "ab?") --> {'','x','y'}
|
--- split("axaby", "ab?") --> {'','x','y'}
|
||||||
--- split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
--- split("x*yz*o", "*", {plain=true}) --> {'x','yz','o'}
|
||||||
--- split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
--- split("|x|y|z|", "|", {trimempty=true}) --> {'x', 'y', 'z'}
|
||||||
|
--- split("|x|y|z|", "|", {keepsep=true}) --> {'|', 'x', '|', 'y', '|', 'z', '|'}
|
||||||
--- </pre>
|
--- </pre>
|
||||||
---
|
---
|
||||||
---@see |vim.gsplit()|
|
---@see |vim.gsplit()|
|
||||||
---
|
---
|
||||||
---@param s string String to split
|
---@param s string String to split
|
||||||
---@param sep string Separator or pattern
|
---@param sep string Separator or pattern
|
||||||
---@param kwargs (table|nil) Keyword arguments:
|
---@param opts (table|nil) Keyword arguments |kwargs| accepted by |vim.gsplit()|
|
||||||
--- - 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
|
|
||||||
---@return string[] List of split components
|
---@return string[] List of split components
|
||||||
function vim.split(s, sep, kwargs)
|
function vim.split(s, sep, opts)
|
||||||
local plain
|
-- TODO(justinmk): deprecate vim.split in favor of vim.totable(vim.gsplit())
|
||||||
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
|
for c in vim.gsplit(s, sep, opts) do
|
||||||
for c in vim.gsplit(s, sep, plain) do
|
table.insert(t, c)
|
||||||
if c ~= '' then
|
|
||||||
skip = false
|
|
||||||
end
|
|
||||||
|
|
||||||
if not skip then
|
|
||||||
table.insert(t, c)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if trimempty then
|
|
||||||
for i = #t, 1, -1 do
|
|
||||||
if t[i] ~= '' then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
table.remove(t, i)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@@ -292,51 +292,62 @@ describe('lua stdlib', function()
|
|||||||
]]}
|
]]}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("vim.split", function()
|
it('vim.gsplit, vim.split', function()
|
||||||
local split = function(str, sep, kwargs)
|
|
||||||
return exec_lua('return vim.split(...)', str, sep, kwargs)
|
|
||||||
end
|
|
||||||
|
|
||||||
local tests = {
|
local tests = {
|
||||||
{ "a,b", ",", false, false, { 'a', 'b' } },
|
{ 'a,b', ',', false, false, { 'a', 'b' } },
|
||||||
{ ":aa::bb:", ":", false, false, { '', 'aa', '', 'bb', '' } },
|
{ ':aa::::bb:', ':', false, false, { '', 'aa', '', '', '', 'bb', '' } },
|
||||||
{ ":aa::bb:", ":", false, true, { 'aa', '', 'bb' } },
|
{ ':aa::::bb:', ':', false, true, { 'aa', '', '', '', 'bb' } },
|
||||||
{ "::ee::ff:", ":", false, false, { '', '', 'ee', '', 'ff', '' } },
|
{ ':aa::bb:', ':', false, true, { 'aa', '', 'bb' } },
|
||||||
{ "::ee::ff:", ":", false, true, { 'ee', '', 'ff' } },
|
{ '/a/b:/b/\n', '[:\n]', false, true, { '/a/b', '/b/' } },
|
||||||
{ "ab", ".", false, false, { '', '', '' } },
|
{ '::ee::ff:', ':', false, false, { '', '', 'ee', '', 'ff', '' } },
|
||||||
{ "a1b2c", "[0-9]", false, false, { 'a', 'b', 'c' } },
|
{ '::ee::ff::', ':', false, true, { 'ee', '', 'ff' } },
|
||||||
{ "xy", "", false, false, { 'x', 'y' } },
|
{ 'ab', '.', false, false, { '', '', '' } },
|
||||||
{ "here be dragons", " ", false, false, { "here", "be", "dragons"} },
|
{ 'a1b2c', '[0-9]', false, false, { 'a', 'b', 'c' } },
|
||||||
{ "axaby", "ab?", false, false, { '', 'x', 'y' } },
|
{ 'xy', '', false, false, { 'x', 'y' } },
|
||||||
{ "f v2v v3v w2w ", "([vw])2%1", false, false, { 'f ', ' v3v ', ' ' } },
|
{ 'here be dragons', ' ', false, false, { 'here', 'be', 'dragons'} },
|
||||||
{ "", "", false, false, {} },
|
{ 'axaby', 'ab?', false, false, { '', 'x', 'y' } },
|
||||||
{ "", "a", false, false, { '' } },
|
{ 'f v2v v3v w2w ', '([vw])2%1', false, false, { 'f ', ' v3v ', ' ' } },
|
||||||
{ "x*yz*oo*l", "*", true, false, { 'x', 'yz', 'oo', 'l' } },
|
{ '', '', false, false, {} },
|
||||||
|
{ '', '', false, true, {} },
|
||||||
|
{ '\n', '[:\n]', false, true, {} },
|
||||||
|
{ '', 'a', false, false, { '' } },
|
||||||
|
{ 'x*yz*oo*l', '*', true, false, { 'x', 'yz', 'oo', 'l' } },
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t in ipairs(tests) do
|
for _, t in ipairs(tests) do
|
||||||
eq(t[5], split(t[1], t[2], {plain=t[3], trimempty=t[4]}))
|
eq(t[5], vim.split(t[1], t[2], {plain=t[3], trimempty=t[4]}))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Test old signature
|
-- Test old signature
|
||||||
eq({'x', 'yz', 'oo', 'l'}, split("x*yz*oo*l", "*", true))
|
eq({'x', 'yz', 'oo', 'l'}, vim.split("x*yz*oo*l", "*", true))
|
||||||
|
|
||||||
local loops = {
|
local loops = {
|
||||||
{ "abc", ".-" },
|
{ "abc", ".-" },
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t in ipairs(loops) do
|
for _, t in ipairs(loops) do
|
||||||
matches("Infinite loop detected", pcall_err(split, t[1], t[2]))
|
matches("Infinite loop detected", pcall_err(vim.split, t[1], t[2]))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- `keepsep`
|
||||||
|
eq({ '', '.', '', '.', 'aa', '.', 'bb', '.', 'cc', '.', 'dd', '.', 'ee', '.', '', },
|
||||||
|
vim.split('..aa.bb.cc.dd.ee.', '%.', {keepsep=true}))
|
||||||
|
eq({ '..aa', '1', '.bb', '2', '', '2', '.cc.', '9', '', },
|
||||||
|
vim.split('..aa1.bb22.cc.9', '%d', {keepsep=true}))
|
||||||
|
eq({ '..aa', '1', '.bb', '22', '.cc.', '9', '', },
|
||||||
|
vim.split('..aa1.bb22.cc.9', '%d+', {keepsep=true}))
|
||||||
|
|
||||||
-- Validates args.
|
-- Validates args.
|
||||||
eq(true, pcall(split, 'string', 'string'))
|
eq(true, pcall(vim.split, 'string', 'string'))
|
||||||
matches('s: expected string, got number',
|
matches('s: expected string, got number',
|
||||||
pcall_err(split, 1, 'string'))
|
pcall_err(vim.split, 1, 'string'))
|
||||||
matches('sep: expected string, got number',
|
matches('sep: expected string, got number',
|
||||||
pcall_err(split, 'string', 1))
|
pcall_err(vim.split, 'string', 1))
|
||||||
matches('kwargs: expected table, got number',
|
matches('opts: expected table, got number',
|
||||||
pcall_err(split, 'string', 'string', 1))
|
pcall_err(vim.split, 'string', 'string', 1))
|
||||||
|
-- Not supported (yet).
|
||||||
|
matches('keepsep%+trimempty not supported',
|
||||||
|
pcall_err(vim.split, 'foo bar', ' ', {keepsep=true, trimempty=true}))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('vim.trim', function()
|
it('vim.trim', function()
|
||||||
|
Reference in New Issue
Block a user