From a3e3bc82a3f95c5ed0d7201546d5d2c19b20d683 Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Mon, 20 May 2024 20:33:47 +0200 Subject: [PATCH] fix(compat): in fast vim loop issue (#589) fixes regression introduced in ce8556d --- lua/plenary/compat.lua | 17 +++++++++++++++++ lua/plenary/curl.lua | 13 +++---------- lua/plenary/iterators.lua | 5 ++--- lua/plenary/job.lua | 4 ++-- lua/plenary/scandir.lua | 16 +++++----------- tests/plenary/path_spec.lua | 4 ++-- 6 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 lua/plenary/compat.lua diff --git a/lua/plenary/compat.lua b/lua/plenary/compat.lua new file mode 100644 index 00000000..5e7e24d2 --- /dev/null +++ b/lua/plenary/compat.lua @@ -0,0 +1,17 @@ +local m = {} + +m.flatten = (function() + if vim.fn.has "nvim-0.11" == 1 then + return function(t) + return vim.iter(t):flatten():totable() + end + else + return function(t) + return vim.tbl_flatten(t) + end + end +end)() + +m.islist = vim.islist or vim.tbl_islist + +return m diff --git a/lua/plenary/curl.lua b/lua/plenary/curl.lua index f9efe25c..b7fc9974 100644 --- a/lua/plenary/curl.lua +++ b/lua/plenary/curl.lua @@ -36,14 +36,7 @@ local util, parse = {}, {} local F = require "plenary.functional" local J = require "plenary.job" local P = require "plenary.path" - -local flatten = function(t) - if vim.fn.has "nvim-0.11" == 1 then - return vim.iter(t):flatten():totable() - else - return vim.tbl_flatten(t) - end -end +local compat = require "plenary.compat" -- Utils ---------------------------------------------------- ------------------------------------------------------------- @@ -62,7 +55,7 @@ util.url_encode = function(str) end util.kv_to_list = function(kv, prefix, sep) - return flatten(F.kv_map(function(kvp) + return compat.flatten(F.kv_map(function(kvp) return { prefix, kvp[1] .. sep .. kvp[2] } end, kv)) end @@ -251,7 +244,7 @@ parse.request = function(opts) table.insert(result, { "-o", opts.output }) end table.insert(result, parse.url(opts.url, opts.query)) - return flatten(result), opts + return compat.flatten(result), opts end -- Parse response ------------------------------------------ diff --git a/lua/plenary/iterators.lua b/lua/plenary/iterators.lua index 3dd2ccb5..2aac0d20 100644 --- a/lua/plenary/iterators.lua +++ b/lua/plenary/iterators.lua @@ -7,6 +7,7 @@ local co = coroutine local f = require "plenary.functional" +local compat = require "plenary.compat" -------------------------------------------------------------------------------- -- Tools @@ -38,8 +39,6 @@ function Iterator:__tostring() return "" end -local is_list = vim.islist or vim.tbl_islist - -- A special hack for zip/chain to skip last two state, if a wrapped iterator -- has been passed local numargs = function(...) @@ -109,7 +108,7 @@ local rawiter = function(obj, param, state) end end - if is_list(obj) then + if compat.islist(obj) then return ipairs(obj) else -- hash diff --git a/lua/plenary/job.lua b/lua/plenary/job.lua index b1b83c44..c095c4c1 100644 --- a/lua/plenary/job.lua +++ b/lua/plenary/job.lua @@ -1,6 +1,6 @@ local vim = vim local uv = vim.loop -local is_list = vim.islist or vim.tbl_islist +local compat = require "plenary.compat" local F = require "plenary.functional" @@ -417,7 +417,7 @@ function Job:_execute() if self.writer then if Job.is_job(self.writer) then self.writer:_execute() - elseif type(self.writer) == "table" and is_list(self.writer) then + elseif type(self.writer) == "table" and compat.islist(self.writer) then local writer_len = #self.writer for i, v in ipairs(self.writer) do self.stdin:write(v) diff --git a/lua/plenary/scandir.lua b/lua/plenary/scandir.lua index 0a383581..d5b60ee4 100644 --- a/lua/plenary/scandir.lua +++ b/lua/plenary/scandir.lua @@ -1,15 +1,9 @@ local Path = require "plenary.path" local os_sep = Path.path.sep local F = require "plenary.functional" +local compat = require "plenary.compat" local uv = vim.loop -local flatten = function(t) - if vim.fn.has "nvim-0.11" == 1 then - return vim.iter(t):flatten():totable() - else - return vim.tbl_flatten(t) - end -end local m = {} @@ -158,8 +152,8 @@ m.scan_dir = function(path, opts) opts = opts or {} local data = {} - local base_paths = flatten { path } - local next_dir = flatten { path } + local base_paths = compat.flatten { path } + local next_dir = compat.flatten { path } local gitignore = opts.respect_gitignore and make_gitignore(base_paths) or nil local match_search_pat = opts.search_pattern and gen_search_pat(opts.search_pattern) or nil @@ -211,8 +205,8 @@ m.scan_dir_async = function(path, opts) opts = opts or {} local data = {} - local base_paths = flatten { path } - local next_dir = flatten { path } + local base_paths = compat.flatten { path } + local next_dir = compat.flatten { path } local current_dir = table.remove(next_dir, 1) -- TODO(conni2461): get gitignore is not async diff --git a/tests/plenary/path_spec.lua b/tests/plenary/path_spec.lua index 9192ce2c..8f916a30 100644 --- a/tests/plenary/path_spec.lua +++ b/tests/plenary/path_spec.lua @@ -1,6 +1,6 @@ local Path = require "plenary.path" local path = Path.path -local is_list = vim.islist or vim.tbl_islist +local compat = require "plenary.compat" describe("Path", function() it("should find valid files", function() @@ -593,7 +593,7 @@ describe("Path", function() it("should extract the ancestors of the path", function() local p = Path:new(vim.loop.cwd()) local parents = p:parents() - assert(is_list(parents)) + assert(compat.islist(parents)) for _, parent in pairs(parents) do assert.are.same(type(parent), "string") end