Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bind keymap, add number prefix #104

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Changes from 2 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
29 changes: 19 additions & 10 deletions lua/dressing/select/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ local function clear_callback()
_items = {}
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)
if vim.fn.hlID("DressingSelectText") ~= 0 then
vim.notify(
Expand All @@ -47,10 +55,19 @@ M.select = function(config, items, opts, on_choice)
end
local lines = {}
local max_width = 1
for _, item in ipairs(items) do
local line = opts.format_item(item)
for idx, item in ipairs(items) do
local line = "[" .. idx .. "] " .. opts.format_item(item)
max_width = math.max(max_width, vim.api.nvim_strwidth(line))
table.insert(lines, line)

vim.api.nvim_buf_set_keymap(bufnr, "n", tostring(idx), "", {
callback = function()
print(0)
Copy link
Owner

Choose a reason for hiding this comment

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

Remove

local callback, items = close_window()
local item = items[idx]
callback(item, idx)
end,
})
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines)
vim.bo[bufnr].modifiable = false
Expand Down Expand Up @@ -94,14 +111,6 @@ M.select = function(config, items, opts, on_choice)
})
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()
local cursor = vim.api.nvim_win_get_cursor(0)
local idx = cursor[1]
Expand Down