Skip to content

Commit 6d39931

Browse files
committed
refactor: pull version check out of compat shims
makes it easier to remove later and minimizes branching
1 parent f8bde03 commit 6d39931

File tree

2 files changed

+33
-45
lines changed

2 files changed

+33
-45
lines changed

lua/telescope/debounce.lua

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33

44
local M = {}
55

6-
---TODO(clason): duplicate for performance reason
7-
---remove when dropping support for Nvim 0.10
8-
local nvim011 = vim.fn.has "nvim-0.11" == 1
9-
local validate = function(k, v, ty, errmsg)
10-
if nvim011 then
11-
vim.validate(k, v, ty, errmsg)
12-
else
13-
vim.validate { [k] = { v, ty, errmsg } }
14-
end
15-
end
16-
176
---Validates args for `throttle()` and `debounce()`.
18-
local function td_validate(fn, ms)
19-
validate("fn", fn, "function")
20-
validate("ms", ms, function(v)
21-
return type(v) == "number" and v > 0
22-
end, "number > 0")
23-
end
7+
---TODO(clason): remove shim when dropping support for Nvim 0.10
8+
local td_validate = vim.fn.has "nvim-0.11" == 1
9+
and function(fn, ms)
10+
vim.validate("fn", fn, "function")
11+
vim.validate("ms", ms, function(v)
12+
return type(v) == "number" and v > 0
13+
end, "number > 0")
14+
end
15+
or function(fn, ms)
16+
vim.validate {
17+
fn = { fn, "f" },
18+
ms = {
19+
ms,
20+
function(v)
21+
return type(v) == "number" and v > 0
22+
end,
23+
"number > 0",
24+
},
25+
}
26+
end
2427

2528
--- Throttles a function on the leading edge. Automatically `schedule_wrap()`s.
2629
---@param fn fun(...) Function to throttle

lua/telescope/utils.lua

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,21 @@ local utils = {}
1818
utils.iswin = vim.uv.os_uname().sysname == "Windows_NT"
1919
utils.nvim011 = vim.fn.has "nvim-0.11" == 1
2020

21-
---@param s string
22-
---@param i number
23-
---@param encoding "utf-8" | "utf-16" | "utf-32"
24-
---@return integer
25-
utils.str_byteindex = function(s, i, encoding)
26-
if utils.nvim011 then
27-
return vim.str_byteindex(s, encoding, i, false)
28-
else
29-
return vim.lsp.util._str_byteindex_enc(s, i, encoding)
30-
end
31-
end
32-
33-
---@param t table
34-
utils.flatten = function(t)
35-
return vim.iter(t):flatten():totable()
36-
end
21+
---TODO(clason): remove when dropping support for Nvim 0.10
22+
utils.str_byteindex = utils.nvim011 and vim.str_byteindex or vim.lsp.util._str_byteindex_enc
3723

3824
---TODO(clason): remove when dropping support for Nvim 0.10
3925
---@param k string
4026
---@param v any
4127
---@param ty type
42-
utils.validate = function(k, v, ty)
43-
if utils.nvim011 then
44-
vim.validate(k, v, ty)
45-
else
46-
vim.validate { [k] = { v, ty } }
47-
end
28+
utils.validate = utils.nvim011 and vim.validate or function(k, v, ty)
29+
vim.validate { [k] = { v, ty } }
30+
end
31+
32+
---@param t table
33+
---@return table
34+
utils.flatten = function(t)
35+
return vim.iter(t):flatten():totable()
4836
end
4937

5038
--- Hybrid of `vim.fn.expand()` and custom `vim.fs.normalize()`
@@ -681,15 +669,12 @@ utils.get_devicons = load_once(function()
681669
end
682670
end)
683671

672+
--- TODO(clason): remove when dropping support for Nvim 0.10
684673
--- Checks if treesitter parser for language is installed
685-
---@param lang string
686-
utils.has_ts_parser = function(lang)
687-
if utils.nvim011 then
688-
return vim.treesitter.language.add(lang)
689-
else
674+
utils.has_ts_parser = utils.nvim011 and vim.treesitter.language.add
675+
or function(lang)
690676
return pcall(vim.treesitter.language.add, lang)
691677
end
692-
end
693678

694679
--- Telescope Wrapper around vim.notify
695680
---@param funname string: name of the function that will be

0 commit comments

Comments
 (0)