diff options
author | Gregory Anders <greg@gpanders.com> | 2021-07-29 14:48:04 -0600 |
---|---|---|
committer | Gregory Anders <greg@gpanders.com> | 2021-09-25 20:11:30 -0600 |
commit | 5fa26e2c2fcc208ca31187de4338d5b6f746f2e1 (patch) | |
tree | 328ac265480e0351c8270a6d4dc6cddecb5f9357 /runtime/lua/vim/shared.lua | |
parent | 05d685be5244ec9f0a8bc042154d0da3449ba2f3 (diff) | |
download | rneovim-5fa26e2c2fcc208ca31187de4338d5b6f746f2e1.tar.gz rneovim-5fa26e2c2fcc208ca31187de4338d5b6f746f2e1.tar.bz2 rneovim-5fa26e2c2fcc208ca31187de4338d5b6f746f2e1.zip |
feat: add trimempty optional parameter to vim.split
The `split()` VimL function trims empty items from the returned list by
default, so that, e.g.
split("\nhello\nworld\n\n", "\n")
returns
["hello", "world"]
The Lua implementation of vim.split does not do this. For example,
vim.split("\nhello\nworld\n\n", "\n")
returns
{'', 'hello', 'world', '', ''}
Add an optional parameter to the vim.split function that, when true,
trims these empty elements from the front and back of the returned
table. This is only possible for vim.split and not vim.gsplit; because
vim.gsplit is an iterator, there is no way for it to know if the current
item is the last non-empty item.
Note that in order to preserve backward compatibility, the parameter for
the Lua vim.split function is `trimempty`, while the VimL function uses
`keepempty` (i.e. they are opposites). This means there is a disconnect
between these two functions that may surprise users.
Diffstat (limited to 'runtime/lua/vim/shared.lua')
-rw-r--r-- | runtime/lua/vim/shared.lua | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 18c1e21049..e18dec9a45 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -98,17 +98,41 @@ end --- <pre> --- split(":aa::b:", ":") --> {'','aa','','b',''} --- split("axaby", "ab?") --> {'','x','y'} ---- split(x*yz*o, "*", true) --> {'x','yz','o'} +--- split("x*yz*o", "*", true) --> {'x','yz','o'} +--- split("|x|y|z|", "|", true, true) --> {'x', 'y', 'z'} --- </pre> --- +--- ---@see |vim.gsplit()| --- ---@param s String to split ---@param sep Separator string or pattern ---@param plain If `true` use `sep` literally (passed to String.find) +---@param trimempty If `true` remove empty items from the front and back of the list ---@returns List-like table of the split components. -function vim.split(s,sep,plain) - local t={} for c in vim.gsplit(s, sep, plain) do table.insert(t,c) end +function vim.split(s, sep, plain, trimempty) + -- Only need to validate trimempty since the rest are validated by vim.gsplit + vim.validate{trimempty={trimempty, 'b', true}} + local t = {} + local skip = trimempty + for c in vim.gsplit(s, sep, plain) do + if c ~= "" then + skip = false + end + + if not skip then + table.insert(t, c) + 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 end |