-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathinput.lua
344 lines (319 loc) · 10.3 KB
/
input.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
local global_config = require("dressing.config")
local patch = require("dressing.patch")
local util = require("dressing.util")
local M = {}
local context = {
opts = nil,
on_confirm = nil,
winid = nil,
history_idx = nil,
history_tip = nil,
start_in_insert = nil,
}
local function set_input(text)
vim.api.nvim_buf_set_lines(0, 0, -1, true, { text })
vim.api.nvim_win_set_cursor(0, { 1, vim.api.nvim_strwidth(text) })
end
local history = {}
M.history_prev = function()
if context.history_idx == nil then
if #history == 0 then
return
end
context.history_tip = vim.api.nvim_buf_get_lines(0, 0, 1, true)[1]
context.history_idx = #history
elseif context.history_idx == 1 then
return
else
context.history_idx = context.history_idx - 1
end
set_input(history[context.history_idx])
end
M.history_next = function()
if not context.history_idx then
return
elseif context.history_idx == #history then
context.history_idx = nil
set_input(context.history_tip)
else
context.history_idx = context.history_idx + 1
set_input(history[context.history_idx])
end
end
local function close_completion_window()
if vim.fn.pumvisible() == 1 then
local escape_key = vim.api.nvim_replace_termcodes("<C-e>", true, false, true)
vim.api.nvim_feedkeys(escape_key, "n", true)
end
end
local function confirm(text)
if not context.on_confirm then
return
end
close_completion_window()
local ctx = context
context = {}
if not ctx.start_in_insert then
vim.cmd("stopinsert")
end
-- We have to wait briefly for the popup window to close (if present),
-- otherwise vim gets into a very weird and bad state. I was seeing text get
-- deleted from the buffer after the input window closes.
vim.defer_fn(function()
pcall(vim.api.nvim_win_close, ctx.winid, true)
if text == "" then
text = nil
end
if text and history[#history] ~= text then
table.insert(history, text)
end
-- Defer the callback because we just closed windows and left insert mode.
-- In practice from my testing, if the user does something right now (like,
-- say, opening another input modal) it could happen improperly. I was
-- seeing my successive modals fail to enter insert mode.
vim.defer_fn(function()
ctx.on_confirm(text)
end, 5)
end, 5)
end
M.confirm = function()
local text = vim.api.nvim_buf_get_lines(0, 0, 1, true)[1]
confirm(text)
end
M.close = function()
confirm()
end
M.highlight = function()
if not context.opts then
return
end
local bufnr = vim.api.nvim_win_get_buf(context.winid)
local opts = context.opts
local text = vim.api.nvim_buf_get_lines(bufnr, 0, 1, true)[1]
local ns = vim.api.nvim_create_namespace("DressingHighlight")
local highlights = {}
if type(opts.highlight) == "function" then
highlights = opts.highlight(text)
elseif opts.highlight then
highlights = vim.fn[opts.highlight](text)
end
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
for _, highlight in ipairs(highlights) do
local start = highlight[1]
local stop = highlight[2]
local group = highlight[3]
vim.api.nvim_buf_add_highlight(bufnr, ns, group, 0, start, stop)
end
end
local function split(string, pattern)
local ret = {}
for token in string.gmatch(string, "[^" .. pattern .. "]+") do
table.insert(ret, token)
end
return ret
end
M.completefunc = function(findstart, base)
if not context.opts or not context.opts.completion then
return findstart == 1 and 0 or {}
end
if findstart == 1 then
return 0
else
local completion = context.opts.completion
local pieces = split(completion, ",")
if pieces[1] == "custom" or pieces[1] == "customlist" then
local vimfunc = pieces[2]
local ret = vim.fn[vimfunc](base, base, vim.fn.strlen(base))
if pieces[1] == "custom" then
ret = split(ret, "\n")
end
return ret
else
local ok, result = pcall(vim.fn.getcompletion, base, context.opts.completion)
if ok then
return result
else
vim.api.nvim_err_writeln(
string.format("dressing.nvim: unsupported completion method '%s'", completion)
)
return {}
end
end
end
end
_G.dressing_input_complete = M.completefunc
M.trigger_completion = function()
if vim.fn.pumvisible() == 1 then
return vim.api.nvim_replace_termcodes("<C-n>", true, false, true)
else
return vim.api.nvim_replace_termcodes("<C-x><C-u>", true, false, true)
end
end
local function create_or_update_win(config, prompt, opts)
local parent_win = 0
local winopt
local win_conf
-- If the previous window is still open and valid, we're going to update it
if context.winid and vim.api.nvim_win_is_valid(context.winid) then
win_conf = vim.api.nvim_win_get_config(context.winid)
parent_win = win_conf.win
winopt = {
relative = win_conf.relative,
win = win_conf.win,
}
else
winopt = {
relative = config.relative,
anchor = config.anchor,
border = config.border,
height = 1,
style = "minimal",
noautocmd = true,
}
end
-- First calculate the desired base width of the modal
local prefer_width = util.calculate_width(
config.relative,
config.prefer_width,
config,
parent_win
)
-- Then expand the width to fit the prompt and default value
prefer_width = math.max(prefer_width, 4 + vim.api.nvim_strwidth(prompt))
if opts.default then
prefer_width = math.max(prefer_width, 2 + vim.api.nvim_strwidth(opts.default))
end
-- Then recalculate to clamp final value to min/max
local width = util.calculate_width(config.relative, prefer_width, config, parent_win)
winopt.row = util.calculate_row(config.relative, 1, parent_win)
winopt.col = util.calculate_col(config.relative, width, parent_win)
winopt.width = width
if win_conf and config.relative == "cursor" then
-- If we're cursor-relative we should actually not adjust the row/col to
-- prevent jumping. Also remove related args.
if config.relative == "cursor" then
winopt.row = nil
winopt.col = nil
winopt.relative = nil
winopt.win = nil
end
end
winopt = config.override(winopt) or winopt
-- If the floating win was already open
if win_conf then
-- Make sure the previous on_confirm callback is called with nil
vim.schedule(context.on_confirm)
vim.api.nvim_win_set_config(context.winid, winopt)
local start_in_insert = context.start_in_insert
return context.winid, start_in_insert
else
local start_in_insert = string.sub(vim.api.nvim_get_mode().mode, 1, 1) == "i"
local bufnr = vim.api.nvim_create_buf(false, true)
local winid = vim.api.nvim_open_win(bufnr, true, winopt)
return winid, start_in_insert
end
end
setmetatable(M, {
-- use schedule_wrap to avoid a bug when vim opens
-- (see https://github.com/stevearc/dressing.nvim/issues/15)
__call = vim.schedule_wrap(function(_, opts, on_confirm)
vim.validate({
on_confirm = { on_confirm, "function", false },
})
opts = opts or {}
if type(opts) ~= "table" then
opts = { prompt = tostring(opts) }
end
local config = global_config.get_mod_config("input", opts)
if not config.enabled then
return patch.original_mods.input(opts, on_confirm)
end
if vim.fn.hlID("DressingInputText") ~= 0 then
vim.notify(
'DressingInputText highlight group is deprecated. Set winhighlight="NormalFloat:MyHighlightGroup" instead',
vim.log.levels.WARN
)
end
-- Create or update the window
local prompt = opts.prompt or config.default_prompt
local winid, start_in_insert = create_or_update_win(config, prompt, opts)
context = {
winid = winid,
on_confirm = on_confirm,
opts = opts,
history_idx = nil,
start_in_insert = start_in_insert,
}
vim.api.nvim_win_set_option(winid, "winblend", config.winblend)
vim.api.nvim_win_set_option(winid, "winhighlight", config.winhighlight)
vim.api.nvim_win_set_option(winid, "wrap", false)
local bufnr = vim.api.nvim_win_get_buf(winid)
-- Finish setting up the buffer
vim.api.nvim_buf_set_option(bufnr, "swapfile", false)
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
local keyopts = { silent = true, noremap = true }
local close_rhs = "<cmd>lua require('dressing.input').close()<CR>"
vim.api.nvim_buf_set_keymap(bufnr, "n", "<Esc>", close_rhs, keyopts)
if config.insert_only then
vim.api.nvim_buf_set_keymap(bufnr, "i", "<Esc>", close_rhs, keyopts)
end
local confirm_rhs = "<cmd>lua require('dressing.input').confirm()<CR>"
vim.api.nvim_buf_set_keymap(bufnr, "i", "<C-c>", close_rhs, keyopts)
vim.api.nvim_buf_set_keymap(bufnr, "i", "<CR>", confirm_rhs, keyopts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<CR>", confirm_rhs, keyopts)
vim.api.nvim_buf_set_keymap(
bufnr,
"i",
"<Up>",
"<cmd>lua require('dressing.input').history_prev()<CR>",
keyopts
)
vim.api.nvim_buf_set_keymap(
bufnr,
"i",
"<Down>",
"<cmd>lua require('dressing.input').history_next()<CR>",
keyopts
)
vim.api.nvim_buf_set_option(bufnr, "filetype", "DressingInput")
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { opts.default or "" })
-- Disable nvim-cmp if installed
local ok, cmp = pcall(require, "cmp")
if ok then
cmp.setup.buffer({ enabled = false })
end
util.add_title_to_win(
winid,
string.gsub(prompt, "^%s*(.-)%s*$", "%1"),
{ align = config.prompt_align }
)
vim.cmd([[
aug DressingHighlight
autocmd! * <buffer>
autocmd TextChanged <buffer> lua require('dressing.input').highlight()
autocmd TextChangedI <buffer> lua require('dressing.input').highlight()
aug END
]])
if opts.completion then
vim.api.nvim_buf_set_option(bufnr, "completefunc", "v:lua.dressing_input_complete")
vim.api.nvim_buf_set_option(bufnr, "omnifunc", "")
vim.api.nvim_buf_set_keymap(
bufnr,
"i",
"<Tab>",
[[luaeval("require('dressing.input').trigger_completion()")]],
{ expr = true }
)
end
vim.cmd([[
aug DressingCloseWin
autocmd! * <buffer>
autocmd BufLeave <buffer> ++nested ++once lua require('dressing.input').close()
aug END
]])
vim.cmd("startinsert!")
close_completion_window()
M.highlight()
end),
})
return M