Skip to content

Commit

Permalink
feat(): allow to not preselect correct word
Browse files Browse the repository at this point in the history
fix #12, fix #13
  • Loading branch information
f3fora committed May 7, 2024
1 parent 32a0867 commit 694a4e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
## Setup

```lua
require('cmp').setup({
require("cmp").setup({
sources = {
{
name = 'spell',
name = "spell",
option = {
keep_all_entries = false,
enable_in_context = function()
return true
end,
preselect_correct_word = true,
},
},
},
Expand All @@ -24,7 +25,7 @@ Setting `spell` (and `spelllang`) is mandatory to use `spellsuggest`.

```lua
vim.opt.spell = true
vim.opt.spelllang = { 'en_us' }
vim.opt.spelllang = { "en_us" }
```

## Options
Expand All @@ -43,7 +44,7 @@ Default: `false`
For example, one can enable this source only when in a `@spell` treesitter capture. See `:help treesitter-highlight-spell`.

```lua
enable_in_context = function()
enable_in_context = function(params)
return require('cmp.config.context').in_treesitter_capture('spell')
end,
```
Expand All @@ -53,13 +54,20 @@ Return: boolean
Default:

```lua
enable_in_context = function()
enable_in_context = function(params)
return true
end,
```

Note: this option will be removed when hrsh7th/nvim-cmp#632 is implemented.

### `preselect_correct_word`

If true and the spelling of a word is correct, the word is displayed as the first entry and preselected.

Type: boolean
Default: `true`

## Credit

- [compe-spell](https://github.com/hrsh7th/nvim-compe/blob/master/lua/compe_spell/init.lua)
Expand Down
2 changes: 1 addition & 1 deletion after/plugin/cmp-spell.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('cmp').register_source('spell', require('cmp-spell').new())
require("cmp").register_source("spell", require("cmp-spell").new())
14 changes: 8 additions & 6 deletions lua/cmp-spell/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local defaults = {
enable_in_context = function()
return true
end,
preselect_correct_word = true,
}

function source.new()
Expand All @@ -20,10 +21,11 @@ function source:get_keyword_pattern()
end

local function validate_option(params)
local option = vim.tbl_deep_extend('keep', params.option, defaults)
local option = vim.tbl_deep_extend("keep", params.option, defaults)
vim.validate({
keep_all_entries = { option.keep_all_entries, 'boolean' },
enable_in_context = { option.enable_in_context, 'function' },
keep_all_entries = { option.keep_all_entries, "boolean" },
enable_in_context = { option.enable_in_context, "function" },
preselect_correct_word = { option.preselect_correct_word, "boolean" },
})
return option
end
Expand All @@ -33,15 +35,15 @@ local function len_to_loglen(len)
end

local function number_to_text(input, number, loglen)
return string.format(input .. '%0' .. loglen .. 'd', number)
return string.format(input .. "%0" .. loglen .. "d", number)
end

local function candidates(input, option)
local items = {}
local entries = vim.fn.spellsuggest(input)
local offset
local loglen
if vim.tbl_isempty(vim.spell.check(input)) then
if option.preselect_correct_word and vim.tbl_isempty(vim.spell.check(input)) then
offset = 1
loglen = len_to_loglen(#entries + offset)

Expand Down Expand Up @@ -84,7 +86,7 @@ function source:complete(params, callback)
end
end

local debug_name = 'spell'
local debug_name = "spell"
function source:get_debug_name()
return debug_name
end
Expand Down

0 comments on commit 694a4e5

Please sign in to comment.