From 622b1ae38a36c3d26fad19faa788d622f7835921 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Fri, 21 Apr 2023 06:46:18 +0200 Subject: [PATCH] fix(lua): vim.split may trim inner empty items Problem: `vim.split('a:::', ':', {trimempty=true})` trims inner empty items. Regression from 9c49c1047079427ff0a2356cb37302934845108e Solution: Set `empty_start=false` when first non-empty item is found. close #23212 --- runtime/lua/vim/shared.lua | 4 +++- test/functional/lua/vim_spec.lua | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 1a96ef9bc4..08f8afd087 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -138,7 +138,9 @@ function vim.gsplit(s, sep, opts) local seg = _pass(s:find(sep, start, plain)) -- Trim empty segments from start/end. - if trimempty and seg == '' then + if seg ~= '' then + empty_start = false + elseif trimempty then while not done and seg == '' do empty_segs = empty_segs + 1 seg = _pass(s:find(sep, start, plain)) diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index e37d477376..b8cc15b2ca 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -294,9 +294,11 @@ describe('lua stdlib', function() it('vim.gsplit, vim.split', function() local tests = { + -- plain trimempty { 'a,b', ',', false, false, { 'a', 'b' } }, { ':aa::::bb:', ':', false, false, { '', 'aa', '', '', '', 'bb', '' } }, { ':aa::::bb:', ':', false, true, { 'aa', '', '', '', 'bb' } }, + { 'aa::::bb:', ':', false, true, { 'aa', '', '', '', 'bb' } }, { ':aa::bb:', ':', false, true, { 'aa', '', 'bb' } }, { '/a/b:/b/\n', '[:\n]', false, true, { '/a/b', '/b/' } }, { '::ee::ff:', ':', false, false, { '', '', 'ee', '', 'ff', '' } }, @@ -315,7 +317,7 @@ describe('lua stdlib', function() } for _, t in ipairs(tests) do - eq(t[5], vim.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]}), t[1]) end -- Test old signature