馃 my neovim config:)
1if vim.g.loaded_input then
2 return
3end
4
5vim.g.loaded_input = true
6
7---@type fun(opts?: vim.ui.input.Opts, on_confirm: fun(input?: string))
8---@diagnostic disable-next-line: duplicate-set-field
9vim.ui.input = function(opts, on_confirm)
10 vim.validate("opts", opts, "table")
11 vim.validate("opts.prompt", opts.prompt, "string", true)
12 vim.validate("opts.default", opts.default, "string", true)
13 vim.validate("on_confirm", on_confirm, "function")
14
15 local buf = vim.api.nvim_create_buf(false, true)
16
17 vim.bo[buf].buftype = "prompt"
18 vim.bo[buf].bufhidden = "wipe"
19 vim.bo[buf].swapfile = false
20
21 local promptset = vim.split(opts.prompt or "", "\n", { trimempty = false })
22 local promptlines
23 local promptstr
24 if #promptset > 1 then
25 promptlines = vim.iter(promptset):slice(1, #promptset - 1):totable()
26 promptstr = promptset[#promptset]
27 else
28 promptstr = opts.prompt
29 end
30
31 vim.fn.prompt_setprompt(buf, promptstr or "")
32 promptlines = promptlines or {}
33 if opts.default then
34 table.insert(promptlines, (promptstr or "") .. opts.default)
35 end
36 vim.api.nvim_buf_set_lines(buf, 0, -1, false, promptlines)
37 local offset = vim.o.winborder == "none" and 0 or -1
38 local win = vim.api.nvim_open_win(buf, true, {
39 relative = "cursor",
40 height = #promptset,
41 width = 1,
42 row = offset,
43 col = offset,
44 style = "minimal",
45 })
46
47 vim.fn.prompt_setcallback(buf, function(text)
48 if vim.api.nvim_win_is_valid(win) then
49 vim.api.nvim_win_close(win, true)
50 end
51 on_confirm(#text > 0 and text or nil)
52 end)
53
54 vim.api.nvim_set_option_value("wrap", true, { win = win, scope = "local" })
55 vim.api.nvim_set_option_value("signcolumn", "no", { win = win, scope = "local" })
56 vim.api.nvim_set_option_value("sidescrolloff", 0, { win = win, scope = "local" })
57 vim.api.nvim_create_autocmd({ "CursorMovedI", "InsertEnter" }, {
58 buffer = buf,
59 callback = function()
60 local maxlinewidth = vim.iter(vim.api.nvim_buf_get_lines(buf, 0, -1, false)):fold(1, function(max, l)
61 local w = vim.api.nvim_strwidth(l)
62 return w > max and w or max
63 end)
64 vim.api.nvim_win_set_width(win, maxlinewidth + 1)
65 end,
66 })
67
68 vim.api.nvim_cmd({ cmd = "startinsert", bang = true }, {})
69end