Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lua/telescope/actions/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
---@brief ]]

local action_state = require "telescope.actions.state"
local validate = require("telescope.utils").validate

local utils = {}

Expand Down Expand Up @@ -35,9 +36,8 @@ local utils = {}
---@param prompt_bufnr number: The prompt bufnr
---@param f function: Function to map onto entries of picker that takes (entry, index, row) as viable arguments
function utils.map_entries(prompt_bufnr, f)
vim.validate {
f = { f, "function" },
}
validate("f", f, "function")

local current_picker = action_state.get_current_picker(prompt_bufnr)
local index = 1
-- indices are 1-indexed, rows are 0-indexed
Expand Down Expand Up @@ -72,9 +72,8 @@ end
---@param prompt_bufnr number: The prompt bufnr
---@param f function: Function to map onto selection of picker that takes (selection) as a viable argument
function utils.map_selections(prompt_bufnr, f)
vim.validate {
f = { f, "function" },
}
validate("f", f, "function")

local current_picker = action_state.get_current_picker(prompt_bufnr)
for _, selection in ipairs(current_picker:get_multi_selection()) do
f(selection)
Expand Down
30 changes: 19 additions & 11 deletions lua/telescope/debounce.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
local M = {}

---Validates args for `throttle()` and `debounce()`.
local function td_validate(fn, ms)
vim.validate {
fn = { fn, "f" },
ms = {
ms,
function(v)
---TODO(clason): remove shim when dropping support for Nvim 0.10
local td_validate = vim.fn.has "nvim-0.11" == 1
and function(fn, ms)
vim.validate("fn", fn, "function")
vim.validate("ms", ms, function(v)
return type(v) == "number" and v > 0
end,
"number > 0",
},
}
end
end, "number > 0")
end
or function(fn, ms)
vim.validate {
fn = { fn, "f" },
ms = {
ms,
function(v)
return type(v) == "number" and v > 0
end,
"number > 0",
},
}
end

--- Throttles a function on the leading edge. Automatically `schedule_wrap()`s.
---@param fn fun(...) Function to throttle
Expand Down
3 changes: 2 additions & 1 deletion lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,8 @@ end
--- - `actions.delete_buffer()`
---@param delete_cb function: called for each selection fn(s) -> bool|nil (true|nil removes the entry from the results)
function Picker:delete_selection(delete_cb)
vim.validate { delete_cb = { delete_cb, "f" } }
utils.validate("delete_cb", delete_cb, "function")

local original_selection_strategy = self.selection_strategy
self.selection_strategy = "row"

Expand Down
34 changes: 15 additions & 19 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ local utils = {}
utils.iswin = vim.uv.os_uname().sysname == "Windows_NT"
utils.nvim011 = vim.fn.has "nvim-0.11" == 1

---@param s string
---@param i number
---@param encoding "utf-8" | "utf-16" | "utf-32"
---@return integer
utils.str_byteindex = function(s, i, encoding)
if utils.nvim011 then
return vim.str_byteindex(s, encoding, i, false)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil is falsy in Lua, so we can use the same signature for both branches.

else
return vim.lsp.util._str_byteindex_enc(s, i, encoding)
end
---TODO(clason): remove when dropping support for Nvim 0.10
utils.str_byteindex = utils.nvim011 and vim.str_byteindex or vim.lsp.util._str_byteindex_enc

---TODO(clason): remove when dropping support for Nvim 0.10
---@param k string
---@param v any
---@param ty type
utils.validate = utils.nvim011 and vim.validate or function(k, v, ty)
vim.validate { [k] = { v, ty } }
end

---@param t table
---@return table
utils.flatten = function(t)
return vim.iter(t):flatten():totable()
end
Expand All @@ -52,9 +53,7 @@ end
---@param path string
---@return string
utils.path_expand = function(path)
vim.validate {
path = { path, { "string" } },
}
utils.validate("path", path, "string")

if utils.is_uri(path) then
return path
Expand Down Expand Up @@ -670,15 +669,12 @@ utils.get_devicons = load_once(function()
end
end)

--- TODO(clason): remove when dropping support for Nvim 0.10
--- Checks if treesitter parser for language is installed
---@param lang string
utils.has_ts_parser = function(lang)
if utils.nvim011 then
return vim.treesitter.language.add(lang)
else
utils.has_ts_parser = utils.nvim011 and vim.treesitter.language.add
or function(lang)
return pcall(vim.treesitter.language.add, lang)
end
end

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