if vim.g.loaded_input then return end vim.g.loaded_input = true ---@type fun(opts?: vim.ui.input.Opts, on_confirm: fun(input?: string)) ---@diagnostic disable-next-line: duplicate-set-field vim.ui.input = function(opts, on_confirm) vim.validate("opts", opts, "table") vim.validate("opts.prompt", opts.prompt, "string", true) vim.validate("opts.default", opts.default, "string", true) vim.validate("on_confirm", on_confirm, "function") local buf = vim.api.nvim_create_buf(false, true) vim.bo[buf].buftype = "prompt" vim.bo[buf].bufhidden = "wipe" vim.bo[buf].swapfile = false local promptset = vim.split(opts.prompt or "", "\n", { trimempty = false }) local promptlines local promptstr if #promptset > 1 then promptlines = vim.iter(promptset):slice(1, #promptset - 1):totable() promptstr = promptset[#promptset] else promptstr = opts.prompt end vim.fn.prompt_setprompt(buf, promptstr or "") promptlines = promptlines or {} if opts.default then table.insert(promptlines, (promptstr or "") .. opts.default) end vim.api.nvim_buf_set_lines(buf, 0, -1, false, promptlines) local offset = vim.o.winborder == "none" and 0 or -1 local win = vim.api.nvim_open_win(buf, true, { relative = "cursor", height = #promptset, width = 1, row = offset, col = offset, style = "minimal", }) vim.fn.prompt_setcallback(buf, function(text) if vim.api.nvim_win_is_valid(win) then vim.api.nvim_win_close(win, true) end on_confirm(#text > 0 and text or nil) end) vim.api.nvim_set_option_value("wrap", true, { win = win, scope = "local" }) vim.api.nvim_set_option_value("signcolumn", "no", { win = win, scope = "local" }) vim.api.nvim_set_option_value("sidescrolloff", 0, { win = win, scope = "local" }) vim.api.nvim_create_autocmd({ "CursorMovedI", "InsertEnter" }, { buffer = buf, callback = function() local maxlinewidth = vim.iter(vim.api.nvim_buf_get_lines(buf, 0, -1, false)):fold(1, function(max, l) local w = vim.api.nvim_strwidth(l) return w > max and w or max end) vim.api.nvim_win_set_width(win, maxlinewidth + 1) end, }) vim.api.nvim_cmd({ cmd = "startinsert", bang = true }, {}) end