mirror of
https://github.com/hrsh7th/cmp-buffer
synced 2025-05-05 09:46:30 +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
|
||||
---@field public bufnr number
|
||||
---@field public opts cmp_buffer.Options
|
||||
---@field public regex any
|
||||
---@field public length number
|
||||
---@field public pattern string
|
||||
---@field public indexing_chunk_size number
|
||||
---@field public indexing_interval number
|
||||
---@field public timer any|nil
|
||||
@ -20,10 +19,9 @@ local buffer = {}
|
||||
|
||||
---Create new buffer object
|
||||
---@param bufnr number
|
||||
---@param length number
|
||||
---@param pattern string
|
||||
---@param opts cmp_buffer.Options
|
||||
---@return cmp_buffer.Buffer
|
||||
function buffer.new(bufnr, length, pattern)
|
||||
function buffer.new(bufnr, opts)
|
||||
local self = setmetatable({}, { __index = buffer })
|
||||
|
||||
self.bufnr = bufnr
|
||||
@ -31,9 +29,8 @@ function buffer.new(bufnr, length, pattern)
|
||||
self.closed = false
|
||||
self.on_close_cb = nil
|
||||
|
||||
self.regex = vim.regex(pattern)
|
||||
self.length = length
|
||||
self.pattern = pattern
|
||||
self.opts = opts
|
||||
self.regex = vim.regex(self.opts.keyword_pattern)
|
||||
self.indexing_chunk_size = 1000
|
||||
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)
|
||||
if match_start and match_end then
|
||||
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
|
||||
word_i = word_i + 1
|
||||
end
|
||||
|
@ -1,5 +1,11 @@
|
||||
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 = {
|
||||
keyword_length = 3,
|
||||
keyword_pattern = [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%([\-]\w*\)*\)]],
|
||||
@ -16,25 +22,27 @@ source.new = function()
|
||||
return self
|
||||
end
|
||||
|
||||
source.get_keyword_pattern = function(_, params)
|
||||
params.option = vim.tbl_deep_extend('keep', params.option, defaults)
|
||||
---@return cmp_buffer.Options
|
||||
source._validate_options = function(_, params)
|
||||
local opts = vim.tbl_deep_extend('keep', params.option, defaults)
|
||||
vim.validate({
|
||||
keyword_length = { params.option.keyword_length, 'number', '`opts.keyword_length` must be `number`' },
|
||||
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`' },
|
||||
keyword_length = { opts.keyword_length, 'number' },
|
||||
keyword_pattern = { opts.keyword_pattern, 'string' },
|
||||
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
|
||||
|
||||
source.complete = function(self, params, callback)
|
||||
params.option = vim.tbl_deep_extend('keep', params.option, defaults)
|
||||
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 opts = self:_validate_options(params)
|
||||
|
||||
local processing = false
|
||||
local bufs = self:_get_buffers(params)
|
||||
local bufs = self:_get_buffers(opts)
|
||||
for _, buf in ipairs(bufs) do
|
||||
if buf.timer then
|
||||
processing = true
|
||||
@ -67,16 +75,12 @@ source.complete = function(self, params, callback)
|
||||
end, processing and 100 or 0)
|
||||
end
|
||||
|
||||
--- _get_bufs
|
||||
source._get_buffers = function(self, params)
|
||||
---@param opts cmp_buffer.Options
|
||||
source._get_buffers = function(self, opts)
|
||||
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
|
||||
local new_buf = buffer.new(
|
||||
bufnr,
|
||||
params.option.keyword_length,
|
||||
params.option.keyword_pattern
|
||||
)
|
||||
local new_buf = buffer.new(bufnr, opts)
|
||||
new_buf.on_close_cb = function()
|
||||
self.buffers[bufnr] = nil
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user