tangled
alpha
login
or
join now
robinwobin.dev
/
artio.nvim
3
fork
atom
minimal extui fuzzy finder for neovim
3
fork
atom
overview
issues
pulls
pipelines
fix(view): structure api calls and use `noautocmd`
robinwobin.dev
3 months ago
c52db069
cc48ad2e
+43
-30
1 changed file
expand all
collapse all
unified
split
lua
artio
view.lua
+43
-30
lua/artio/view.lua
···
24
end
25
end
26
27
-
local cmdbuff = "" ---@type string Stored cmdline used to calculate translation offset.
28
-
local promptlen = 0 -- Current length of the last line in the prompt.
29
-
local promptwidth = 0 -- Current width of the prompt in the cmdline buffer.
30
-
local promptidx = 0
31
-
--- Concatenate content chunks and set the text for the current row in the cmdline buffer.
32
-
---
33
-
---@param content CmdContent
34
-
---@param prompt string
35
-
local function set_text(content, prompt)
36
-
local lines = {} ---@type string[]
37
-
for line in (prompt .. "\n"):gmatch("(.-)\n") do
38
-
lines[#lines + 1] = vim.fn.strtrans(line)
39
-
end
40
-
41
-
promptlen = #lines[#lines]
42
-
promptwidth = vim.fn.strdisplaywidth(lines[#lines])
43
-
44
-
cmdbuff = ""
45
-
for _, chunk in ipairs(content) do
46
-
cmdbuff = cmdbuff .. chunk[2]
47
-
end
48
-
lines[#lines] = ("%s%s"):format(lines[#lines], vim.fn.strtrans(cmdbuff))
49
-
vim.api.nvim_buf_set_lines(ext.bufs.cmd, promptidx, promptidx + 1, false, lines)
50
-
end
51
-
52
---@class artio.View
53
---@field picker artio.Picker
54
---@field closed boolean
···
73
74
local prompthl_id = -1
75
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
76
--- Set the cmdline buffer text and cursor position.
77
---
78
---@param content CmdContent
···
103
self:showmatches()
104
105
self:promptpos()
106
-
set_text(content, ("%s%s%s"):format(firstc, prompt, (" "):rep(indent)))
107
self:updatecursor(pos)
108
109
local height = math.max(1, vim.api.nvim_win_text_height(ext.wins.cmd, {}).all)
···
192
193
vim.api.nvim_create_autocmd("TextChangedI", {
194
group = self.augroup,
0
195
callback = function()
196
self:update()
197
end,
···
199
200
vim.api.nvim_create_autocmd("CursorMovedI", {
201
group = self.augroup,
0
202
callback = function()
203
self:updatecursor()
204
end,
···
305
function View:clear()
306
cmdline.srow = self.picker.opts.bottom and 0 or 1
307
cmdline.erow = 0
308
-
vim.api.nvim_buf_set_lines(ext.bufs.cmd, 0, -1, false, {})
309
end
310
311
function View:promptpos()
312
promptidx = self.picker.opts.bottom and cmdline.erow or 0
0
0
0
0
0
0
313
end
314
315
local view_ns = vim.api.nvim_create_namespace("artio:view:ns")
···
325
---@param opts vim.api.keyset.set_extmark
326
---@return integer
327
function View:mark(line, col, opts)
328
-
local ok, result = pcall(vim.api.nvim_buf_set_extmark, ext.bufs.cmd, view_ns, line, col, opts)
0
0
0
329
if not ok then
330
vim.notify(("Failed to add extmark %d:%d"):format(line, col), vim.log.levels.ERROR)
331
return -1
···
412
end
413
end
414
cmdline.erow = cmdline.srow + #lines
415
-
vim.api.nvim_buf_set_lines(ext.bufs.cmd, cmdline.srow, cmdline.erow, false, lines)
416
417
for i = 1, #lines do
418
local has_icon = icons[i] and icons[i][1] and true
···
455
456
function View:hlselect()
457
if self.select_ext then
458
-
vim.api.nvim_buf_del_extmark(ext.bufs.cmd, view_ns, self.select_ext)
0
0
459
end
460
461
self:softupdatepreview()
···
24
end
25
end
26
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
27
---@class artio.View
28
---@field picker artio.Picker
29
---@field closed boolean
···
48
49
local prompthl_id = -1
50
51
+
local cmdbuff = "" ---@type string Stored cmdline used to calculate translation offset.
52
+
local promptlen = 0 -- Current length of the last line in the prompt.
53
+
local promptwidth = 0 -- Current width of the prompt in the cmdline buffer.
54
+
local promptidx = 0
55
+
--- Concatenate content chunks and set the text for the current row in the cmdline buffer.
56
+
---
57
+
---@param content CmdContent
58
+
---@param prompt string
59
+
function View:setprompttext(content, prompt)
60
+
local lines = {} ---@type string[]
61
+
for line in (prompt .. "\n"):gmatch("(.-)\n") do
62
+
lines[#lines + 1] = vim.fn.strtrans(line)
63
+
end
64
+
65
+
promptlen = #lines[#lines]
66
+
promptwidth = vim.fn.strdisplaywidth(lines[#lines])
67
+
68
+
cmdbuff = ""
69
+
for _, chunk in ipairs(content) do
70
+
cmdbuff = cmdbuff .. chunk[2]
71
+
end
72
+
lines[#lines] = ("%s%s"):format(lines[#lines], vim.fn.strtrans(cmdbuff))
73
+
self:setlines(promptidx, promptidx + 1, lines)
74
+
end
75
+
76
--- Set the cmdline buffer text and cursor position.
77
---
78
---@param content CmdContent
···
103
self:showmatches()
104
105
self:promptpos()
106
+
self:setprompttext(content, ("%s%s%s"):format(firstc, prompt, (" "):rep(indent)))
107
self:updatecursor(pos)
108
109
local height = math.max(1, vim.api.nvim_win_text_height(ext.wins.cmd, {}).all)
···
192
193
vim.api.nvim_create_autocmd("TextChangedI", {
194
group = self.augroup,
195
+
buffer = ext.bufs.cmd,
196
callback = function()
197
self:update()
198
end,
···
200
201
vim.api.nvim_create_autocmd("CursorMovedI", {
202
group = self.augroup,
203
+
buffer = ext.bufs.cmd,
204
callback = function()
205
self:updatecursor()
206
end,
···
307
function View:clear()
308
cmdline.srow = self.picker.opts.bottom and 0 or 1
309
cmdline.erow = 0
310
+
self:setlines(0, -1, {})
311
end
312
313
function View:promptpos()
314
promptidx = self.picker.opts.bottom and cmdline.erow or 0
315
+
end
316
+
317
+
function View:setlines(posstart, posend, lines)
318
+
vim._with({ noautocmd = true }, function()
319
+
vim.api.nvim_buf_set_lines(ext.bufs.cmd, posstart, posend, false, lines)
320
+
end)
321
end
322
323
local view_ns = vim.api.nvim_create_namespace("artio:view:ns")
···
333
---@param opts vim.api.keyset.set_extmark
334
---@return integer
335
function View:mark(line, col, opts)
336
+
local ok, result
337
+
vim._with({ noautocmd = true }, function()
338
+
ok, result = pcall(vim.api.nvim_buf_set_extmark, ext.bufs.cmd, view_ns, line, col, opts)
339
+
end)
340
if not ok then
341
vim.notify(("Failed to add extmark %d:%d"):format(line, col), vim.log.levels.ERROR)
342
return -1
···
423
end
424
end
425
cmdline.erow = cmdline.srow + #lines
426
+
self:setlines(cmdline.srow, cmdline.erow, lines)
427
428
for i = 1, #lines do
429
local has_icon = icons[i] and icons[i][1] and true
···
466
467
function View:hlselect()
468
if self.select_ext then
469
+
vim._with({ noautocmd = true }, function()
470
+
vim.api.nvim_buf_del_extmark(ext.bufs.cmd, view_ns, self.select_ext)
471
+
end)
472
end
473
474
self:softupdatepreview()