nvim: scope now supports vim.ui.input

This commit is contained in:
dogeystamp 2024-09-14 16:56:29 -04:00
parent 35a9439ed4
commit ea7eb8c056
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38

View File

@ -1,4 +1,4 @@
-- telescope without telescope
-- telescope without telescope80
-- depends on: fzf, bat
M = {}
@ -28,7 +28,7 @@ end
---@field mode ("window" | "float")? Sets the window used to show the scope. Defaults to "window".
--- - "window": fills the entire window
--- - "float": floating window
---@field float_opts vim.api.keyset.win_config? Options if mode is set to "float".
---@field float_opts vim.api.keyset.win_config? Options to pass to `nvim_open_win` if mode is set to "float".
---Generic brand fuzzy selector
---@param choice_gen string | function Lua function or shell command that generates choices to display in fzf.
@ -112,7 +112,7 @@ end
--- - format_item (function item -> text)
--- - kind (string|nil)
---@param on_choice fun(item: any|nil, idx: integer|nil)
local function picker(items, opts, on_choice)
local function select_new(items, opts, on_choice)
vim.validate({
items = { items, 'table', false },
on_choice = { on_choice, 'function', false },
@ -151,9 +151,65 @@ local function picker(items, opts, on_choice)
)
end
--- Replacement for `vim.ui.input`.
---@param opts table? Additional options. See |input()|
--- - prompt (string|nil)
--- - default (string|nil)
---@param on_confirm function ((input|nil) -> ())
local function input_new(opts, on_confirm)
vim.validate({
opts = { opts, 'table', true },
on_confirm = { on_confirm, 'function', false },
})
opts = (opts and not vim.tbl_isempty(opts)) and opts or vim.empty_dict()
opts.on_confirm = opts.on_confirm or function() end
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_open_win(buf, true,
{
relative = "cursor",
width = 30,
height = 1,
col = 1,
row = 1,
border = "rounded",
title = opts.prompt,
})
if opts.default then
vim.api.nvim_buf_set_lines(buf, 0, 1, true, { opts.default })
end
vim.wo.number = false
vim.wo.relativenumber = false
vim.cmd [[startinsert!]]
local map_opts = { noremap = true, buffer = buf }
local function close_win()
vim.api.nvim_win_close(0, false)
vim.cmd.stopinsert()
end
vim.keymap.set({ "i", "n" }, "<Enter>",
function()
on_confirm(table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, true), "\n"))
close_win()
end,
map_opts
)
vim.keymap.set({ "i" }, "<C-c>", function ()
on_confirm(nil)
close_win()
end, map_opts)
end
---Sets up `vim.ui` hooks to use scope.
function M.setup()
vim.ui.select = picker
vim.ui.select = select_new
vim.ui.input = input_new
end
--------------------------------