mirror of
https://github.com/hrsh7th/cmp-buffer
synced 2025-05-06 10:09:52 +00:00
Use a table for passing options around
This commit is contained in:
parent
01c570786a
commit
ee8e9af753
@ -1,8 +1,7 @@
|
|||||||
---@class cmp_buffer.Buffer
|
---@class cmp_buffer.Buffer
|
||||||
---@field public bufnr number
|
---@field public bufnr number
|
||||||
|
---@field public opts cmp_buffer.Options
|
||||||
---@field public regex any
|
---@field public regex any
|
||||||
---@field public length number
|
|
||||||
---@field public pattern string
|
|
||||||
---@field public indexing_chunk_size number
|
---@field public indexing_chunk_size number
|
||||||
---@field public indexing_interval number
|
---@field public indexing_interval number
|
||||||
---@field public timer any|nil
|
---@field public timer any|nil
|
||||||
@ -20,10 +19,9 @@ local buffer = {}
|
|||||||
|
|
||||||
---Create new buffer object
|
---Create new buffer object
|
||||||
---@param bufnr number
|
---@param bufnr number
|
||||||
---@param length number
|
---@param opts cmp_buffer.Options
|
||||||
---@param pattern string
|
|
||||||
---@return cmp_buffer.Buffer
|
---@return cmp_buffer.Buffer
|
||||||
function buffer.new(bufnr, length, pattern)
|
function buffer.new(bufnr, opts)
|
||||||
local self = setmetatable({}, { __index = buffer })
|
local self = setmetatable({}, { __index = buffer })
|
||||||
|
|
||||||
self.bufnr = bufnr
|
self.bufnr = bufnr
|
||||||
@ -31,9 +29,8 @@ function buffer.new(bufnr, length, pattern)
|
|||||||
self.closed = false
|
self.closed = false
|
||||||
self.on_close_cb = nil
|
self.on_close_cb = nil
|
||||||
|
|
||||||
self.regex = vim.regex(pattern)
|
self.opts = opts
|
||||||
self.length = length
|
self.regex = vim.regex(self.opts.keyword_pattern)
|
||||||
self.pattern = pattern
|
|
||||||
self.indexing_chunk_size = 1000
|
self.indexing_chunk_size = 1000
|
||||||
self.indexing_interval = 200
|
self.indexing_interval = 200
|
||||||
|
|
||||||
@ -254,7 +251,7 @@ function buffer.index_line(self, linenr, line)
|
|||||||
local match_start, match_end = self.regex:match_str(remaining)
|
local match_start, match_end = self.regex:match_str(remaining)
|
||||||
if match_start and match_end then
|
if match_start and match_end then
|
||||||
local word = remaining:sub(match_start + 1, match_end)
|
local word = remaining:sub(match_start + 1, match_end)
|
||||||
if #word >= self.length then
|
if #word >= self.opts.keyword_length then
|
||||||
words[word_i] = word
|
words[word_i] = word
|
||||||
word_i = word_i + 1
|
word_i = word_i + 1
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
local buffer = require('cmp_buffer.buffer')
|
local buffer = require('cmp_buffer.buffer')
|
||||||
|
|
||||||
|
---@class cmp_buffer.Options
|
||||||
|
---@field public keyword_length number
|
||||||
|
---@field public keyword_pattern string
|
||||||
|
---@field public get_bufnrs fun(): number[]
|
||||||
|
|
||||||
|
---@type cmp_buffer.Options
|
||||||
local defaults = {
|
local defaults = {
|
||||||
keyword_length = 3,
|
keyword_length = 3,
|
||||||
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%([\-]\w*\)*\)]],
|
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%([\-]\w*\)*\)]],
|
||||||
@ -16,25 +22,27 @@ source.new = function()
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
source.get_keyword_pattern = function(_, params)
|
---@return cmp_buffer.Options
|
||||||
params.option = vim.tbl_deep_extend('keep', params.option, defaults)
|
source._validate_options = function(_, params)
|
||||||
|
local opts = vim.tbl_deep_extend('keep', params.option, defaults)
|
||||||
vim.validate({
|
vim.validate({
|
||||||
keyword_length = { params.option.keyword_length, 'number', '`opts.keyword_length` must be `number`' },
|
keyword_length = { opts.keyword_length, 'number' },
|
||||||
keyword_pattern = { params.option.keyword_pattern, 'string', '`opts.keyword_pattern` must be `string`' },
|
keyword_pattern = { opts.keyword_pattern, 'string' },
|
||||||
get_bufnrs = { params.option.get_bufnrs, 'function', '`opts.get_bufnrs` must be `function`' },
|
get_bufnrs = { opts.get_bufnrs, 'function' },
|
||||||
})
|
})
|
||||||
return params.option.keyword_pattern
|
return opts
|
||||||
|
end
|
||||||
|
|
||||||
|
source.get_keyword_pattern = function(self, params)
|
||||||
|
local opts = self:_validate_options(params)
|
||||||
|
return opts.keyword_pattern
|
||||||
end
|
end
|
||||||
|
|
||||||
source.complete = function(self, params, callback)
|
source.complete = function(self, params, callback)
|
||||||
params.option = vim.tbl_deep_extend('keep', params.option, defaults)
|
local opts = self:_validate_options(params)
|
||||||
vim.validate({
|
|
||||||
keyword_pattern = { params.option.keyword_pattern, 'string', '`opts.keyword_pattern` must be `string`' },
|
|
||||||
get_bufnrs = { params.option.get_bufnrs, 'function', '`opts.get_bufnrs` must be `function`' },
|
|
||||||
})
|
|
||||||
|
|
||||||
local processing = false
|
local processing = false
|
||||||
local bufs = self:_get_buffers(params)
|
local bufs = self:_get_buffers(opts)
|
||||||
for _, buf in ipairs(bufs) do
|
for _, buf in ipairs(bufs) do
|
||||||
if buf.timer then
|
if buf.timer then
|
||||||
processing = true
|
processing = true
|
||||||
@ -67,16 +75,12 @@ source.complete = function(self, params, callback)
|
|||||||
end, processing and 100 or 0)
|
end, processing and 100 or 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- _get_bufs
|
---@param opts cmp_buffer.Options
|
||||||
source._get_buffers = function(self, params)
|
source._get_buffers = function(self, opts)
|
||||||
local buffers = {}
|
local buffers = {}
|
||||||
for _, bufnr in ipairs(params.option.get_bufnrs()) do
|
for _, bufnr in ipairs(opts.get_bufnrs()) do
|
||||||
if not self.buffers[bufnr] then
|
if not self.buffers[bufnr] then
|
||||||
local new_buf = buffer.new(
|
local new_buf = buffer.new(bufnr, opts)
|
||||||
bufnr,
|
|
||||||
params.option.keyword_length,
|
|
||||||
params.option.keyword_pattern
|
|
||||||
)
|
|
||||||
new_buf.on_close_cb = function()
|
new_buf.on_close_cb = function()
|
||||||
self.buffers[bufnr] = nil
|
self.buffers[bufnr] = nil
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user