Skip to content

Commit

Permalink
feat(): enable only in treesitter spell region
Browse files Browse the repository at this point in the history
  • Loading branch information
f3fora committed Oct 3, 2022
1 parent 5c32dd5 commit dddefa8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require('cmp').setup({
name = 'spell',
option = {
keep_all_entries = false,
enable_in_spell_captures = false,
},
},
},
Expand All @@ -33,6 +34,15 @@ If true, all `vim.fn.spellsuggest` results are displayed in `nvim-cmp` menu. Oth
Type: boolean
Default: `false`

### `enable_in_spell_captures`

Depends on [`cmp.config.context.in_treesitter_capture`](hrsh7th/nvim-cmp) and thus on [`nvim-treesitter.ts_utils`](nvim-treesitter/nvim-treesitter).

If true, `nvim-cmp` menu is populated only in `@spell` captures. See `:h treesitter-highlight-spell`.

Type: boolean
Default: `false`

## Credit

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

local defaults = {
keep_all_entries = false,
enable_only_in_spell_captures = false,
}

function source.new()
return setmetatable({}, { __index = source })
local self = setmetatable({}, { __index = source })
self.in_treesitter_capture = require('cmp.config.context').in_treesitter_capture
return self
end

function source:is_available()
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_only_in_spell_captures = { option.enable_only_in_spell_captures, 'boolean' },
})
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_only_in_spell_captures and not self.in_treesitter_capture('spell') then
callback({ items = {}, isIncomplete = true })
else
callback({ items = candidates(input, option), isIncomplete = true })
end
end

local debug_name = 'spell'
Expand Down

0 comments on commit dddefa8

Please sign in to comment.