Skip to content

Commit 47a8530

Browse files
authored
fix: adjust to vim.validate deprecation in Nvim 0.11 (#3555)
* fix: adjust to vim.validate deprecation * refactor: pull version check out of compat shims makes it easier to remove later and minimizes branching
1 parent 02ec064 commit 47a8530

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

lua/telescope/actions/utils.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
---@brief ]]
99

1010
local action_state = require "telescope.actions.state"
11+
local validate = require("telescope.utils").validate
1112

1213
local utils = {}
1314

@@ -35,9 +36,8 @@ local utils = {}
3536
---@param prompt_bufnr number: The prompt bufnr
3637
---@param f function: Function to map onto entries of picker that takes (entry, index, row) as viable arguments
3738
function utils.map_entries(prompt_bufnr, f)
38-
vim.validate {
39-
f = { f, "function" },
40-
}
39+
validate("f", f, "function")
40+
4141
local current_picker = action_state.get_current_picker(prompt_bufnr)
4242
local index = 1
4343
-- indices are 1-indexed, rows are 0-indexed
@@ -72,9 +72,8 @@ end
7272
---@param prompt_bufnr number: The prompt bufnr
7373
---@param f function: Function to map onto selection of picker that takes (selection) as a viable argument
7474
function utils.map_selections(prompt_bufnr, f)
75-
vim.validate {
76-
f = { f, "function" },
77-
}
75+
validate("f", f, "function")
76+
7877
local current_picker = action_state.get_current_picker(prompt_bufnr)
7978
for _, selection in ipairs(current_picker:get_multi_selection()) do
8079
f(selection)

lua/telescope/debounce.lua

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44
local M = {}
55

66
---Validates args for `throttle()` and `debounce()`.
7-
local function td_validate(fn, ms)
8-
vim.validate {
9-
fn = { fn, "f" },
10-
ms = {
11-
ms,
12-
function(v)
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)
1312
return type(v) == "number" and v > 0
14-
end,
15-
"number > 0",
16-
},
17-
}
18-
end
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
1927

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

lua/telescope/pickers.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,8 @@ end
816816
--- - `actions.delete_buffer()`
817817
---@param delete_cb function: called for each selection fn(s) -> bool|nil (true|nil removes the entry from the results)
818818
function Picker:delete_selection(delete_cb)
819-
vim.validate { delete_cb = { delete_cb, "f" } }
819+
utils.validate("delete_cb", delete_cb, "function")
820+
820821
local original_selection_strategy = self.selection_strategy
821822
self.selection_strategy = "row"
822823

lua/telescope/utils.lua

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ 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
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
23+
24+
---TODO(clason): remove when dropping support for Nvim 0.10
25+
---@param k string
26+
---@param v any
27+
---@param ty type
28+
utils.validate = utils.nvim011 and vim.validate or function(k, v, ty)
29+
vim.validate { [k] = { v, ty } }
3130
end
3231

32+
---@param t table
33+
---@return table
3334
utils.flatten = function(t)
3435
return vim.iter(t):flatten():totable()
3536
end
@@ -52,9 +53,7 @@ end
5253
---@param path string
5354
---@return string
5455
utils.path_expand = function(path)
55-
vim.validate {
56-
path = { path, { "string" } },
57-
}
56+
utils.validate("path", path, "string")
5857

5958
if utils.is_uri(path) then
6059
return path
@@ -670,15 +669,12 @@ utils.get_devicons = load_once(function()
670669
end
671670
end)
672671

672+
--- TODO(clason): remove when dropping support for Nvim 0.10
673673
--- Checks if treesitter parser for language is installed
674-
---@param lang string
675-
utils.has_ts_parser = function(lang)
676-
if utils.nvim011 then
677-
return vim.treesitter.language.add(lang)
678-
else
674+
utils.has_ts_parser = utils.nvim011 and vim.treesitter.language.add
675+
or function(lang)
679676
return pcall(vim.treesitter.language.add, lang)
680677
end
681-
end
682678

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

0 commit comments

Comments
 (0)