robin/artio.nvim

minimal ui2 fuzzy finder for Neovim

git clone https://git.robinwobin.dev/robin/artio.nvim

robinfeat!: add async support8a29ed7

main
20.8 KiB821 linesraw
1local cmdline = require("vim._core.ui2.cmdline")
2local ui2 = require("vim._core.ui2")
3
4local _log = {}
5local _loglevel = vim.log.levels.ERROR
6---@private
7---@param msgwrapped { [1]: string, [2]: string? }
8---@param level? integer
9local function logadd(msgwrapped, level)
10  level = level or vim.log.levels.DEBUG
11  if level < _loglevel then
12    return
13  end
14  _log[#_log + 1] = msgwrapped
15end
16---@private
17---@param msg string
18local function logdebug(msg)
19  logadd({ msg .. "\n" })
20end
21---@private
22---@param msg string
23---@param v any
24local function logdbg(msg, v)
25  logdebug(string.format("%s: %s\n", msg, (vim.inspect(v))))
26end
27---@private
28---@param msg string
29local function logerror(msg)
30  logadd({ msg .. "\n", "ErrorMsg" }, vim.log.levels.ERROR)
31end
32
33local prompt_hl_id = vim.api.nvim_get_hl_id_by_name("ArtioPrompt")
34
35---@class artio.View
36---@field picker artio.Picker
37---@field closed boolean
38---@field opts table<'win'|'buf'|'g',table<string,any>>
39---@field marks table<string|integer, integer>
40---@field win artio.View.win
41---@field preview_win integer
42local View = {}
43View.__index = View
44
45---@param picker artio.Picker
46function View:new(picker)
47  ---@diagnostic disable-next-line: undefined-field
48  if picker.log_level then
49    ---@diagnostic disable-next-line: undefined-field
50    _loglevel = picker.log_level
51  end
52
53  return setmetatable({
54    picker = picker,
55    closed = false,
56    opts = {},
57    marks = {},
58    win = {
59      height = 1,
60    },
61  }, View)
62end
63
64---@class artio.View.win
65---@field height integer
66
67local prompthl_id = -1
68
69--- gets updated before draw
70local before_draw_tick = 0
71--- gets updated after changedtick event
72local last_draw_tick = 0
73local function get_changedtick()
74  return vim.api.nvim_buf_get_changedtick(ui2.bufs.cmd)
75end
76
77local cmdbuff = "" ---@type string Stored cmdline used to calculate translation offset.
78local promptlen = 0 -- Current length of the last line in the prompt.
79local promptidx = 0
80--- Concatenate content chunks and set the text for the current row in the cmdline buffer.
81---
82---@param content CmdContent
83---@param prompt string
84function View:setprompttext(content, prompt)
85  local lines = {} ---@type string[]
86  for line in (prompt .. "\n"):gmatch("(.-)\n") do
87    lines[#lines + 1] = vim.fn.strtrans(line)
88  end
89
90  local promptstr = lines[#lines]
91  promptlen = #lines[#lines]
92
93  cmdbuff = ""
94  for _, chunk in ipairs(content) do
95    cmdbuff = cmdbuff .. chunk[2]
96  end
97  lines[#lines] = ("%s%s"):format(promptstr, vim.fn.strtrans(cmdbuff))
98
99  self:promptpos()
100  self:setlines(promptidx, promptidx + 1, lines)
101  if vim.fn.prompt_getprompt(ui2.bufs.cmd) ~= promptstr then
102    vim.fn.prompt_setprompt(ui2.bufs.cmd, promptstr)
103  end
104  vim.schedule(function()
105    local ok, result = pcall(vim.api.nvim_buf_set_mark, ui2.bufs.cmd, ":", promptidx + 1, promptlen, {})
106    if not ok then
107      logerror(("Failed to set mark %d:%d\n\t%s"):format(promptidx, promptlen, result))
108      return
109    end
110  end)
111end
112
113--- Set the cmdline buffer text and cursor position.
114---
115---@param content CmdContent
116---@param pos? integer
117---@param firstc string
118---@param prompt string
119---@param indent integer
120---@param level integer
121---@param hl_id integer
122function View:show(content, pos, firstc, prompt, indent, level, hl_id)
123  cmdline.level, cmdline.indent = level, indent
124  if cmdline.highlighter and cmdline.highlighter.active then
125    cmdline.highlighter.active[ui2.bufs.cmd] = nil
126  end
127  if ui2.msg.cmd.msg_row ~= -1 then
128    ui2.msg.msg_clear()
129  end
130  ui2.msg.virt.last = { {}, {}, {}, {} }
131
132  self:clear()
133  prompthl_id = hl_id
134
135  local cmd_text = ""
136  for _, chunk in ipairs(content) do
137    cmd_text = cmd_text .. chunk[2]
138  end
139
140  self:showmatches()
141
142  self:setprompttext(content, ("%s%s%s"):format(firstc, prompt, (" "):rep(indent)))
143  self:updatecursor(pos)
144
145  self:updatewinheight()
146
147  self:drawprompt()
148  self:hlselect()
149end
150
151--- Set the 'cmdheight' and cmdline window height. Reposition message windows.
152---
153---@param win integer Cmdline window in the current tabpage.
154---@param hide boolean Whether to hide or show the window.
155---@param height integer (Text)height of the cmdline window.
156function View:win_config(win, hide, height)
157  if ui2.cmdheight == 0 and vim.api.nvim_win_get_config(win).hide ~= hide then
158    vim.api.nvim_win_set_config(win, { hide = hide, height = not hide and height or nil })
159  elseif vim.api.nvim_win_get_height(win) ~= height then
160    vim.api.nvim_win_set_height(win, height)
161  end
162
163  if not hide and self.picker.win.hidestatusline then
164    height = 0
165  end
166
167  if vim.o.cmdheight ~= height then
168    -- Avoid moving the cursor with 'splitkeep' = "screen", and altering the user
169    -- configured value with noautocmd.
170    vim._with({ noautocmd = true, o = { splitkeep = "screen" } }, function()
171      vim.o.cmdheight = height
172    end)
173    ui2.msg.set_pos()
174  end
175
176  if self.preview_win and vim.api.nvim_win_is_valid(self.preview_win) then
177    vim.api.nvim_win_set_config(self.preview_win, self:previewconfig())
178  end
179end
180
181---@param predicted? integer The predicted height of the cmdline window
182function View:updatewinheight(predicted)
183  local height = math.max(1, predicted or vim.api.nvim_win_text_height(ui2.wins.cmd, {}).all)
184  height = math.min(height, self.win.height)
185  self:win_config(ui2.wins.cmd, false, height)
186end
187
188function View:saveview()
189  self.save = vim.fn.winsaveview()
190  self.prevwin = vim.api.nvim_get_current_win()
191end
192
193function View:restoreview()
194  vim.api.nvim_set_current_win(self.prevwin)
195  vim.fn.winrestview(self.save)
196end
197
198local ext_winhl = "Search:,CurSearch:,IncSearch:"
199
200---@param restore? boolean
201function View:setopts(restore)
202  local opts = {
203    win = {
204      eventignorewin = "all,-FileType,-InsertCharPre,-TextChangedI,-CursorMovedI",
205      winhighlight = "Normal:ArtioNormal," .. ext_winhl,
206      signcolumn = "no",
207      wrap = false,
208    },
209    buf = {
210      filetype = "artio-picker",
211      buftype = "prompt",
212      autocomplete = false,
213    },
214    g = {
215      showmode = false,
216      showcmd = false,
217    },
218  }
219
220  for level, o in pairs(opts) do
221    self.opts[level] = self.opts[level] or {}
222    local props = {
223      scope = level == "g" and "global" or "local",
224      buf = level == "buf" and ui2.bufs.cmd or nil,
225      win = level == "win" and ui2.wins.cmd or nil,
226    }
227
228    for name, value in pairs(o) do
229      if restore then
230        vim.api.nvim_set_option_value(name, self.opts[level][name], props)
231      else
232        self.opts[level][name] = vim.api.nvim_get_option_value(name, props)
233        vim.api.nvim_set_option_value(name, value, props)
234      end
235    end
236  end
237end
238
239local maxlistheight = 1 -- Max height of the matches list (`self.win.height - 1`)
240
241function View:on_resized()
242  logdebug("on_resized")
243
244  if self.picker.win.height > 1 then
245    self.win.height = self.picker.win.height
246  else
247    self.win.height = vim.o.lines * self.picker.win.height
248  end
249  self.win.height = math.max(math.ceil(self.win.height), 1)
250
251  maxlistheight = math.max(self.win.height - 1, 1)
252end
253
254function View:open()
255  if not self.picker then
256    return
257  end
258  _log = nil
259  _log = {}
260
261  vim.schedule(function()
262    self.augroup = vim.api.nvim_create_augroup("@artio.view", { clear = true })
263
264    vim.api.nvim_create_autocmd("CmdlineLeave", {
265      group = self.augroup,
266      once = true,
267      callback = function()
268        self:close()
269      end,
270    })
271
272    vim.api.nvim_create_autocmd("ModeChanged", {
273      group = self.augroup,
274      callback = function(ev)
275        if string.match(ev.match, "^i:") then
276          self:close()
277        end
278      end,
279    })
280
281    vim.api.nvim_create_autocmd({ "VimResized", "WinEnter" }, {
282      group = self.augroup,
283      callback = function()
284        self:on_resized()
285      end,
286    })
287
288    vim.api.nvim_create_autocmd("WinEnter", {
289      group = self.augroup,
290      callback = function()
291        self:update(true)
292      end,
293    })
294
295    vim.api.nvim_create_autocmd("TextChangedI", {
296      group = self.augroup,
297      buffer = ui2.bufs.cmd,
298      callback = function()
299        self:update()
300      end,
301    })
302
303    vim.api.nvim_create_autocmd("CursorMovedI", {
304      group = self.augroup,
305      buffer = ui2.bufs.cmd,
306      callback = function()
307        self:updatecursor()
308      end,
309    })
310  end)
311
312  cmdline.prompt = false
313  cmdline.srow = 0
314  cmdline.indent = 1
315  cmdline.level = 1
316
317  self:saveview()
318
319  -- initial render
320  self:trigger_show()
321
322  vim._with({ noautocmd = true }, function()
323    vim.api.nvim_set_current_win(ui2.wins.cmd)
324  end)
325
326  self:setopts()
327
328  -- start insert *before* registering events
329  self:updatecursor(#cmdbuff)
330  vim._with({ noautocmd = true }, function()
331    vim.cmd.startinsert({ bang = true })
332  end)
333
334  -- trigger after registering events
335  vim.schedule(function()
336    vim._with({ win = ui2.wins.cmd, wo = { eventignorewin = "" } }, function()
337      vim.api.nvim_exec_autocmds("WinEnter", {})
338    end)
339  end)
340end
341
342function View:close()
343  if self.closed then
344    return
345  end
346  self:closepreview()
347  vim.schedule(function()
348    pcall(vim.api.nvim_del_augroup_by_id, self.augroup)
349    pcall(vim.api.nvim_buf_detach, ui2.bufs.cmd)
350
351    vim.cmd.stopinsert()
352
353    -- prepare state
354    self:setopts(true)
355
356    -- reset state
357    self:clear()
358    cmdline.srow = 0
359    cmdline.erow = 0
360
361    -- restore ui
362    self:hide()
363    self:restoreview()
364    vim.cmd.redraw()
365
366    self.closed = true
367
368    self.picker:close()
369
370    vim.api.nvim_echo(_log, true, {})
371  end)
372end
373
374function View:hide()
375  vim.fn.clearmatches(ui2.wins.cmd) -- Clear matchparen highlights.
376  vim.api.nvim_win_set_cursor(ui2.wins.cmd, { 1, 0 })
377  vim.api.nvim_buf_set_lines(ui2.bufs.cmd, 0, -1, false, {})
378
379  local clear = vim.schedule_wrap(function(was_prompt)
380    -- Avoid clearing prompt window when it is re-entered before the next event
381    -- loop iteration. E.g. when a non-choice confirm button is pressed.
382    if was_prompt and not cmdline.prompt then
383      pcall(function()
384        vim.api.nvim_buf_set_lines(ui2.bufs.cmd, 0, -1, false, {})
385        vim.api.nvim_buf_set_lines(ui2.bufs.dialog, 0, -1, false, {})
386        vim.api.nvim_win_set_config(ui2.wins.dialog, { hide = true })
387        vim.on_key(nil, ui2.msg.dialog_on_key)
388      end)
389    end
390    -- Messages emitted as a result of a typed command are treated specially:
391    -- remember if the cmdline was used this event loop iteration.
392    -- NOTE: Message event callbacks are themselves scheduled, so delay two iterations.
393    vim.schedule(function()
394      cmdline.level = -1
395    end)
396  end)
397  clear(cmdline.prompt)
398
399  cmdline.prompt, cmdline.level = false, 0
400  self:win_config(ui2.wins.cmd, true, ui2.cmdheight)
401end
402
403function View:trigger_show()
404  logdebug("trigger_show")
405  local input
406  if self.picker.live then
407    input = self.picker.liveinput
408  else
409    input = self.picker.input
410  end
411  self:show({ { 0, input } }, -1, "", self.picker.prompttext, cmdline.indent, cmdline.level, prompt_hl_id)
412end
413
414---@param force? boolean
415function View:update(force)
416  if not force and before_draw_tick < last_draw_tick and before_draw_tick == get_changedtick() - 1 then
417    logdebug("update (skip-redraw)")
418    return self:drawprompt()
419  end
420
421  logdebug("update")
422
423  local text = vim.api.nvim_get_current_line()
424  text = text:sub(promptlen + 1)
425
426  if self.picker.live then
427    self.picker.liveinput = text
428  else
429    self.picker.input = text
430  end
431
432  vim.schedule(coroutine.wrap(function()
433    logdebug("getmatches")
434    self.picker:getmatches()
435
436    if self.closed then
437      return
438    end
439
440    vim.schedule_wrap(self.trigger_show)(self)
441  end))
442end
443
444local curpos = { 0, 0 } -- Last drawn cursor position. absolute
445---@param pos? integer relative to prompt
446function View:updatecursor(pos)
447  logdebug("updatecursor")
448
449  self:promptpos()
450
451  if not pos or pos < 0 then
452    local cursorpos = vim.api.nvim_win_get_cursor(ui2.wins.cmd)
453    pos = cursorpos[2] - promptlen
454  end
455
456  -- set cursor pos to *at least* the prompt length
457  curpos[2] = math.max(curpos[2], promptlen)
458
459  if curpos[1] == promptidx + 1 and curpos[2] == promptlen + pos then
460    return
461  end
462
463  if pos < 0 then
464    -- reset to last known position
465    pos = curpos[2] - promptlen
466  end
467
468  curpos[1], curpos[2] = promptidx + 1, promptlen + pos
469
470  vim._with({ noautocmd = true }, function()
471    local ok, _ = pcall(vim.api.nvim_win_set_cursor, ui2.wins.cmd, curpos)
472    if not ok then
473      logerror(("Failed to set cursor %d:%d"):format(curpos[1], curpos[2]))
474    end
475  end)
476end
477
478local srow = 0
479
480function View:clear()
481  srow = self.picker.opts.bottom and 0 or 1
482  cmdline.erow = srow
483  self:setlines(0, -1, {})
484end
485
486function View:promptpos()
487  promptidx = self.picker.opts.bottom and cmdline.erow or 0
488end
489
490function View:setlines(posstart, posend, lines)
491  -- update winheight to prevent wrong scroll when increasing from 1
492  local diff = #lines - (posend - posstart)
493  if diff ~= 0 then
494    local height = vim.api.nvim_win_text_height(ui2.wins.cmd, {}).all
495    local predicted = height + diff
496    self:updatewinheight(predicted)
497  end
498
499  before_draw_tick = get_changedtick()
500  vim.api.nvim_buf_set_lines(ui2.bufs.cmd, posstart, posend, false, lines)
501  last_draw_tick = get_changedtick()
502end
503
504local view_ns = vim.api.nvim_create_namespace("@artio.view.ns")
505local ext_priority = {
506  prompt = 1,
507  info = 2,
508  select = 4,
509  marker = 8,
510  hl = 16,
511  icon = 32,
512  match = 64,
513}
514
515---@param id? string|integer
516---@param line integer 0-based
517---@param col integer 0-based
518---@param opts vim.api.keyset.set_extmark
519---@return integer
520function View:mark(id, line, col, opts)
521  if id and self.marks[id] then
522    vim._with({ noautocmd = true }, function()
523      vim.api.nvim_buf_del_extmark(ui2.bufs.cmd, view_ns, self.marks[id])
524    end)
525    self.marks[id] = nil
526  end
527
528  opts.hl_mode = "combine"
529  opts.invalidate = true
530
531  local ok, result
532  vim._with({ noautocmd = true }, function()
533    ok, result = pcall(vim.api.nvim_buf_set_extmark, ui2.bufs.cmd, view_ns, line, col, opts)
534  end)
535  if not ok then
536    logerror(("Failed to add extmark %d:%d\n\t%s"):format(line, col, result))
537    return -1
538  end
539
540  if id and result >= 0 then
541    self.marks[id] = result
542  end
543
544  return result
545end
546
547---@param p artio.Picker
548---@param info 'index'|'list'|string
549---@return string
550local function getpromptinfo(p, info)
551  if info == "index" then
552    return ("[%d]"):format(p.idx)
553  elseif info == "list" then
554    return ("(%d/%d)"):format(#p.matches, p.items and #p.items or 0)
555  end
556  return ""
557end
558
559function View:drawprompt()
560  logdebug("drawprompt")
561
562  self:promptpos()
563  if promptlen > 0 and prompthl_id > 0 then
564    self:mark("prompthl", promptidx, 0, { hl_group = prompthl_id, end_col = promptlen, priority = ext_priority.prompt })
565    self:mark("promptinfo", promptidx, 0, {
566      virt_text = {
567        {
568          table.concat(
569            vim
570              .iter(self.picker.opts.infolist)
571              :map(function(info)
572                return getpromptinfo(self.picker, info)
573              end)
574              :totable(),
575            " "
576          ),
577          "InfoText",
578        },
579      },
580      virt_text_pos = "eol_right_align",
581      priority = ext_priority.info,
582    })
583  end
584end
585
586local offset = 0
587
588function View:updateoffset()
589  self.picker:fix()
590  if self.picker.idx == 0 then
591    offset = 0
592    return
593  end
594
595  local _offset = self.picker.idx - maxlistheight
596  if _offset > offset then
597    offset = _offset
598  elseif self.picker.idx <= offset then
599    offset = self.picker.idx - 1
600  end
601
602  offset = math.min(math.max(0, offset), math.max(0, #self.picker.matches - maxlistheight))
603end
604
605local icon_pad = 2
606
607function View:showmatches()
608  local indent = vim.fn.strdisplaywidth(self.picker.opts.pointer) + 1
609  local prefix = (" "):rep(indent)
610  local icon_pad_str = (" "):rep(icon_pad)
611
612  self:updateoffset()
613
614  local lines = {} ---@type string[]
615  local hls = {}
616  local icons = {} ---@type ([string, string]|false)[]
617  local custom_hls = {} ---@type (artio.Picker.hl[]|false)[]
618  local marks = {} ---@type boolean[]
619  for i = 1 + offset, math.min(#self.picker.matches, maxlistheight + offset) do
620    local match = self.picker.matches[i]
621    local item = self.picker.items[match[1]]
622
623    local icon, icon_hl = item.icon, item.icon_hl
624    if not (icon and icon_hl) and vim.is_callable(self.picker.get_icon) then
625      icon, icon_hl = self.picker.get_icon(item)
626      item.icon, item.icon_hl = icon, icon_hl
627    end
628    icons[#icons + 1] = icon and { icon, icon_hl } or false
629    icon = icon and ("%s%s"):format(item.icon, icon_pad_str) or ""
630
631    local hl = item.hls
632    if not hl and vim.is_callable(self.picker.hl_item) then
633      hl = self.picker.hl_item(item)
634      item.hls = hl
635    end
636    custom_hls[#custom_hls + 1] = hl or false
637
638    marks[#marks + 1] = self.picker.marked[item.id] or false
639
640    lines[#lines + 1] = ("%s%s%s"):format(prefix, icon, item.text)
641    hls[#hls + 1] = match[2]
642  end
643
644  if not self.picker.opts.shrink then
645    for _ = 1, (maxlistheight - #lines) do
646      lines[#lines + 1] = ""
647    end
648  end
649  self:setlines(srow, cmdline.erow, lines)
650  cmdline.erow = srow + #lines
651
652  for i = 1, #lines do
653    local has_icon = icons[i] and icons[i][1] and true
654    local icon_indent = has_icon and (#icons[i][1] + icon_pad) or 0
655
656    if has_icon and icons[i][2] then
657      self:mark(nil, srow + i - 1, indent, {
658        end_col = indent + icon_indent,
659        hl_group = icons[i][2],
660        priority = ext_priority.icon,
661      })
662    end
663
664    local line_hls = custom_hls[i]
665    if line_hls then
666      for j = 1, #line_hls do
667        local hl = line_hls[j]
668        self:mark(nil, srow + i - 1, indent + icon_indent + hl[1][1], {
669          end_col = indent + icon_indent + hl[1][2],
670          hl_group = hl[2],
671          priority = ext_priority.hl,
672        })
673      end
674    end
675
676    if marks[i] then
677      self:mark(nil, srow + i - 1, indent - 1, {
678        virt_text = { { self.picker.opts.marker, "ArtioMark" } },
679        virt_text_pos = "overlay",
680        priority = ext_priority.marker,
681      })
682      self:mark(nil, srow + i - 1, 0, {
683        hl_group = "ArtioMarkLine",
684        hl_eol = true,
685        end_row = srow + i,
686        end_col = 0,
687
688        priority = ext_priority.marker,
689      })
690    end
691
692    if hls[i] then
693      for j = 1, #hls[i] do
694        local col = indent + icon_indent + hls[i][j]
695        self:mark(nil, srow + i - 1, col, {
696          hl_group = "ArtioMatch",
697          end_col = col + 1,
698          priority = ext_priority.match,
699        })
700      end
701    end
702  end
703end
704
705function View:hlselect()
706  self:softupdatepreview()
707
708  self.picker:fix()
709  local idx = self.picker.idx
710  if idx == 0 then
711    return
712  end
713
714  self:updateoffset()
715  local row = math.max(0, math.min(srow + (idx - offset), cmdline.erow) - 1)
716
717  self:mark("hlselect", row, 0, {
718    virt_text = { { self.picker.opts.pointer, "ArtioPointer" } },
719    virt_text_pos = "overlay",
720
721    hl_group = "ArtioSel",
722    hl_eol = true,
723    end_row = row + 1,
724    end_col = 0,
725
726    priority = ext_priority.select,
727  })
728end
729
730function View:togglepreview()
731  if self.preview_win then
732    self:closepreview()
733    return
734  end
735
736  self:updatepreview()
737end
738
739---@return {buf?:integer, pos?:[integer,integer], pos_end?:[integer,integer]}?
740function View:openpreview()
741  if self.picker.idx == 0 then
742    return
743  end
744
745  local match = self.picker.matches[self.picker.idx]
746  local item = self.picker.items[match[1]]
747
748  if not item or not (self.picker.preview_item and vim.is_callable(self.picker.preview_item)) then
749    return
750  end
751
752  return self.picker.preview_item(item.v)
753end
754
755function View:previewconfig()
756  local previewopts = self.picker.win.preview_opts
757    and vim.is_callable(self.picker.win.preview_opts)
758    and self.picker.win.preview_opts(self)
759  local cmdheight = vim.api.nvim_win_get_height(ui2.wins.cmd)
760
761  local winborder = previewopts and previewopts.border or vim.o.winborder
762  return vim.tbl_extend("force", {
763    relative = "editor",
764    focusable = false,
765    width = vim.o.columns,
766    height = self.win.height,
767    col = 0,
768    row = vim.o.lines
769      - (self.win.height + cmdheight)
770      - ((winborder == "none" or winborder == "") and 0 or 2)
771      - (self.picker.win.hidestatusline and 0 or 1),
772  }, previewopts or {})
773end
774
775function View:updatepreview()
776  local pr = self:openpreview()
777  if not pr or not pr.buf then
778    return
779  end
780
781  if not self.preview_win then
782    self.preview_win = vim.api.nvim_open_win(pr.buf, false, self:previewconfig())
783  else
784    vim.api.nvim_win_set_buf(self.preview_win, pr.buf)
785  end
786
787  vim._with({ win = self.preview_win, noautocmd = true }, function()
788    vim.api.nvim_set_option_value("previewwindow", true, { scope = "local" })
789    vim.api.nvim_set_option_value("eventignorewin", "all,-FileType", { scope = "local" })
790
791    local sameline = pr.pos ~= nil and pr.pos_end == nil or pr.pos_end[1] == pr.pos[1]
792    vim.api.nvim_set_option_value("cursorline", sameline, { scope = "local" })
793
794    if pr.pos then
795      vim.api.nvim_win_set_cursor(self.preview_win, pr.pos)
796    end
797  end)
798end
799
800function View:softupdatepreview()
801  if self.picker.idx == 0 then
802    self:closepreview()
803  end
804
805  if not self.preview_win then
806    return
807  end
808
809  self:updatepreview()
810end
811
812function View:closepreview()
813  if not self.preview_win then
814    return
815  end
816
817  vim.api.nvim_win_close(self.preview_win, true)
818  self.preview_win = nil
819end
820
821return View