Skip to content

Commit

Permalink
fix(compat): in fast vim loop issue (#589)
Browse files Browse the repository at this point in the history
fixes regression introduced in ce8556d
  • Loading branch information
Conni2461 authored May 20, 2024
1 parent b5c8de0 commit a3e3bc8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
17 changes: 17 additions & 0 deletions lua/plenary/compat.lua
Original file line number Diff line number Diff line change
@@ -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
13 changes: 3 additions & 10 deletions lua/plenary/curl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------------------------------------------
-------------------------------------------------------------
Expand All @@ -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
Expand Down Expand Up @@ -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 ------------------------------------------
Expand Down
5 changes: 2 additions & 3 deletions lua/plenary/iterators.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

local co = coroutine
local f = require "plenary.functional"
local compat = require "plenary.compat"

--------------------------------------------------------------------------------
-- Tools
Expand Down Expand Up @@ -38,8 +39,6 @@ function Iterator:__tostring()
return "<iterator>"
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(...)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/plenary/job.lua
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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)
Expand Down
16 changes: 5 additions & 11 deletions lua/plenary/scandir.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/plenary/path_spec.lua
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a3e3bc8

Please sign in to comment.