-
Notifications
You must be signed in to change notification settings - Fork 197
/
inputs.lua
103 lines (87 loc) · 2.63 KB
/
inputs.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
local vim = vim
local Input = require("nui.input")
local popups = require("neo-tree.ui.popups")
local utils = require("neo-tree.utils")
local events = require("neo-tree.events")
local M = {}
local should_use_popup_input = function()
local nt = require("neo-tree")
return utils.get_value(nt.config, "use_popups_for_input", true, false)
end
M.show_input = function(input, callback)
local config = require("neo-tree").config
input:mount()
if input.prompt_type ~= "confirm" then
vim.schedule(function()
-- deprecate this option in next version
if config.enable_normal_mode_for_inputs then
vim.cmd("stopinsert")
end
events.fire_event(events.NEO_TREE_POPUP_INPUT_READY)
end)
end
input:map("i", "<esc>", function()
vim.cmd("stopinsert")
if not config.enable_normal_mode_for_inputs or input.prompt_type == "confirm" then
input:unmount()
end
end, { noremap = true })
input:map("n", "<esc>", function()
input:unmount()
end, { noremap = true })
input:map("n", "q", function()
input:unmount()
end, { noremap = true })
input:map("i", "<C-w>", "<C-S-w>", { noremap = true })
local event = require("nui.utils.autocmd").event
input:on({ event.BufLeave, event.BufDelete }, function()
input:unmount()
if callback then
callback()
end
end, { once = true })
end
M.input = function(message, default_value, callback, options, completion)
if should_use_popup_input() then
local popup_options = popups.popup_options(message, 10, options)
local input = Input(popup_options, {
prompt = " ",
default_value = default_value,
on_submit = callback,
})
M.show_input(input)
else
local opts = {
prompt = message .. "\n",
default = default_value,
}
if vim.opt.cmdheight:get() == 0 then
-- NOTE: I really don't know why but letters before the first '\n' is not rendered execpt in noice.nvim
-- when vim.opt.cmdheight = 0 <2023-10-24, pysan3>
opts.prompt = "Neo-tree Popup\n" .. opts.prompt
end
if completion then
opts.completion = completion
end
vim.ui.input(opts, callback)
end
end
M.confirm = function(message, callback)
if should_use_popup_input() then
local popup_options = popups.popup_options(message, 10)
local input = Input(popup_options, {
prompt = " y/n: ",
on_close = function()
callback(false)
end,
on_submit = function(value)
callback(value == "y" or value == "Y")
end,
})
input.prompt_type = "confirm"
M.show_input(input)
else
callback(vim.fn.confirm(message, "&Yes\n&No") == 1)
end
end
return M