robin/ivy

my custom neovim config

git clone https://git.robinwobin.dev/robin/ivy

robinplugin: update input handling43612fb

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