Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): enable only in treesitter spell region #8

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ require('cmp').setup({
name = 'spell',
option = {
keep_all_entries = false,
enable_in_context = function()
return true
end,
},
},
},
Expand All @@ -30,9 +33,33 @@ vim.opt.spelllang = { 'en_us' }

If true, all `vim.fn.spellsuggest` results are displayed in `nvim-cmp` menu. Otherwise, they are being filtered to only include fuzzy matches.

Type: boolean
Type: boolean
Default: `false`

### `enable_in_context`

'nvim-cmp' menu is populated only when the function returns true.

For example, one can enable this source only when in a `@spell` treesitter capture. See `:help treesitter-highlight-spell`.

```lua
enable_in_context = function()
return require('cmp.config.context').in_treesitter_capture('spell')
end,
```

Type: function
Return: boolean
Default:

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

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

## Credit

- [compe-spell](https://github.com/hrsh7th/nvim-compe/blob/master/lua/compe_spell/init.lua)
Expand Down
14 changes: 11 additions & 3 deletions lua/cmp-spell/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local source = {}

local defaults = {
keep_all_entries = false,
enable_in_context = function()
return true
end,
}

function source.new()
Expand All @@ -20,6 +23,7 @@ local function validate_option(params)
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' },
})
return option
end
Expand All @@ -44,7 +48,7 @@ local function candidates(input, option)
items[offset] = {
label = input,
filterText = input,
insertText = input,
-- insertText = input,
sortText = number_to_text(input, offset, loglen),
-- If the current word is spelled correctly, preselect it.
preselect = true,
Expand All @@ -59,7 +63,7 @@ local function candidates(input, option)
label = v,
-- Using the `input` word as filterText, all suggestions are displayed in completion menu.
filterText = option.keep_all_entries and input or v,
insertText = v,
-- insertText = v,
-- To keep the order of suggestions, add the index at the end of sortText and trick the compare algorithms.
-- TODO: Add a custom compare function.
sortText = option.keep_all_entries and number_to_text(input, k + offset, loglen) or v,
Expand All @@ -73,7 +77,11 @@ function source:complete(params, callback)
local option = validate_option(params)

local input = string.sub(params.context.cursor_before_line, params.offset)
callback({ items = candidates(input, option), isIncomplete = true })
if option.enable_in_context() then
callback({ items = candidates(input, option), isIncomplete = true })
else
callback({ items = {}, isIncomplete = true })
end
end

local debug_name = 'spell'
Expand Down