feat: built-in select implementation binds number keymaps (#104)

* feat(select/builtin): binding keymap for select

Signed-off-by: Longtao Zhang <DragonBillow@outlook.com>

* feat(select/builtin): add number prefix

Signed-off-by: Longtao Zhang <DragonBillow@outlook.com>

* style(builtin): remove debug statement

Signed-off-by: Longtao Zhang <DragonBillow@outlook.com>

* style(builtin): rename variable, solve luacheck problem

Signed-off-by: Longtao Zhang <DragonBillow@outlook.com>

---------

Signed-off-by: Longtao Zhang <DragonBillow@outlook.com>
This commit is contained in:
DragonBillow 2023-07-17 23:54:38 +08:00 committed by GitHub
parent e6eff7a5a9
commit cc69bfed36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 10 deletions

View File

@ -30,6 +30,14 @@ local function clear_callback()
_items = {} _items = {}
end end
local function close_window()
local callback = _callback
local items = _items
clear_callback()
vim.api.nvim_win_close(0, true)
return callback, items
end
M.select = function(config, items, opts, on_choice) M.select = function(config, items, opts, on_choice)
if vim.fn.hlID("DressingSelectText") ~= 0 then if vim.fn.hlID("DressingSelectText") ~= 0 then
vim.notify( vim.notify(
@ -47,10 +55,18 @@ M.select = function(config, items, opts, on_choice)
end end
local lines = {} local lines = {}
local max_width = 1 local max_width = 1
for _, item in ipairs(items) do for idx, item in ipairs(items) do
local line = opts.format_item(item) local line = "[" .. idx .. "] " .. opts.format_item(item)
max_width = math.max(max_width, vim.api.nvim_strwidth(line)) max_width = math.max(max_width, vim.api.nvim_strwidth(line))
table.insert(lines, line) table.insert(lines, line)
vim.api.nvim_buf_set_keymap(bufnr, "n", tostring(idx), "", {
callback = function()
local callback, local_items = close_window()
local target_item = local_items[idx]
callback(target_item, idx)
end,
})
end end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines) vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines)
vim.bo[bufnr].modifiable = false vim.bo[bufnr].modifiable = false
@ -94,14 +110,6 @@ M.select = function(config, items, opts, on_choice)
}) })
end end
local function close_window()
local callback = _callback
local items = _items
clear_callback()
vim.api.nvim_win_close(0, true)
return callback, items
end
M.choose = function() M.choose = function()
local cursor = vim.api.nvim_win_get_cursor(0) local cursor = vim.api.nvim_win_get_cursor(0)
local idx = cursor[1] local idx = cursor[1]