File: spelling.lua

package info (click to toggle)
neovim-which-key 3.17.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 500 kB
  • sloc: sh: 21; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,245 bytes parent folder | download
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
---@diagnostic disable: missing-fields, inject-field
---@type wk.Plugin
local M = {}

M.name = "spelling"

M.mappings = {
  {
    "z=",
    icon = { icon = "ξΎ³ ", color = "red" },
    plugin = "spelling",
    desc = "Spelling Suggestions",
  },
}

---@type table<string, any>
M.opts = {}

function M.setup(opts)
  M.opts = opts
end

function M.expand()
  -- if started with a count, let the default keybinding work
  local count = vim.v.count
  if count and count > 0 then
    return {}
  end

  local cursor_word = vim.fn.expand("<cword>")
  -- get a misspelled word from under the cursor, if not found, then use the cursor_word instead
  local bad = vim.fn.spellbadword(cursor_word)
  local word = bad[1] == "" and cursor_word or bad[1]

  ---@type string[]
  local suggestions = vim.fn.spellsuggest(word, M.opts.suggestions or 20, bad[2] == "caps" and 1 or 0)

  local items = {} ---@type wk.Plugin.item[]
  local keys = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

  for i, label in ipairs(suggestions) do
    local key = keys:sub(i, i)

    table.insert(items, {
      key = key,
      desc = label,
      action = function()
        vim.cmd("norm! " .. i .. "z=")
      end,
    })
  end
  return items
end

return M