robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
83ee06463b
master
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 ) 20vim . ui . select ( items , { 21prompt =N_ ( 'Change "%s" to:' ): format ( bad ), 22kind ='spell' , 23format_item =function ( s ) 24local extra =s . extra and ( ' < "' .. s . extra .. '"' ) or '' 25local score ='' 26if vim . o . verbose > 0 then 27score =s . altscore 28and ( ' (%s%d - %d)' ): format ( s . salscore and 's ' or '' , s . score , s . altscore ) 29or ( ' (%d)' ): format ( s . score ) 30end 31return ( '"%s"%s%s' ): format ( s . word , extra , score ) 32end , 33} , function ( _ , idx ) 34if not idx then 35return 36end 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". 39vim . fn . feedkeys ( vim . keycode (( '<Cmd>normal! %dz=<CR>' ): format ( idx )), 'in' ) 40end ) 41end 42 43return M