robin/laufey

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

Justin M. KeyesMerge #39488 from justinmk/uiselect83ee06463b

master
1.6 KiB43 linesraw
1local N_ = vim.fn.gettext
2
3local M = {}
4
5--- @class vim._core.spell.Suggestion
6--- @field word string The suggested replacement.
7--- @field extra? string Text replaced when wider than the bad span.
8--- @field score integer Primary score.
9--- @field altscore? integer Secondary score (only set when 'spellsuggest' contains "double" or "best").
10--- @field salscore? boolean True if the score came from sound-alike comparison (only set alongside `altscore`).
11
12--- Implements `spell_suggest()` (`z=`) via vim.ui.select().
13---
14--- async: returns immediately, the chosen suggestion is applied later
15--- by re-running `:normal! [idx]z=` from `on_choice`.
16---
17--- @param items vim._core.spell.Suggestion[]
18--- @param bad string The misspelled word being replaced.
19function M.select_suggest(items, bad)
20  vim.ui.select(items, {
21    prompt = N_('Change "%s" to:'):format(bad),
22    kind = 'spell',
23    format_item = function(s)
24      local extra = s.extra and (' < "' .. s.extra .. '"') or ''
25      local score = ''
26      if vim.o.verbose > 0 then
27        score = s.altscore
28            and (' (%s%d - %d)'):format(s.salscore and 's ' or '', s.score, s.altscore)
29          or (' (%d)'):format(s.score)
30      end
31      return ('"%s"%s%s'):format(s.word, extra, score)
32    end,
33  }, function(_, idx)
34    if not idx then
35      return
36    end
37    -- Queue ":normal! [idx]z=" as user input, so the recursive spell_suggest runs via the normal
38    -- input-dispatch loop. Using vim.schedule + vim.cmd can hang bc of "Press ENTER".
39    vim.fn.feedkeys(vim.keycode(('<Cmd>normal! %dz=<CR>'):format(idx)), 'in')
40  end)
41end
42
43return M