Skip to content

Commit

Permalink
refactor(cmp)!: Restructured plugin.cmp.sources.
Browse files Browse the repository at this point in the history
Signed-off-by: Guennadi Maximov C <[email protected]>
  • Loading branch information
DrKJeff16 committed Sep 2, 2024
1 parent 9335073 commit ddae185
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 88 deletions.
160 changes: 74 additions & 86 deletions lua/plugin/cmp/sources.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ local function source_curr_buf()
return tbl_keys({ [win_buf(win)] = true })
end

---@type Sources
---@diagnostic disable-next-line:missing-fields
local M = {}

---@param group_index? integer
---@param all_bufs? boolean
---@return SourceBuf
local function buffer(group_index, all_bufs)
function M.buffer(group_index, all_bufs)
all_bufs = is_bool(all_bufs) and all_bufs or true

---@type SourceBuf
Expand All @@ -57,7 +61,7 @@ end

---@param group_index? integer
---@return SourceAsyncPath
local function async_path(group_index)
function M.async_path(group_index)
---@type SourceAsyncPath
local res = {
name = 'async_path',
Expand All @@ -76,37 +80,37 @@ local function async_path(group_index)
end

---@type table<string, (cmp.SourceConfig|SourceBuf|SourceAsyncPath)[]>
local Sources = {
M.Sources = {
c = {
{ name = 'nvim_lsp', group_index = 1 },
{ name = 'nvim_lsp_signature_help', group_index = 2 },
{ name = 'vsnip', group_index = 3 },
async_path(5),
buffer(4),
M.async_path(5),
M.buffer(4),
},

lua = {
{ name = 'nvim_lsp', group_index = 1 },
{ name = 'nvim_lsp_signature_help', group_index = 2 },
{ name = 'nvim_lua', group_index = 3 },
{ name = 'vsnip', group_index = 4 },
buffer(5),
M.buffer(5),
},
}

if exists('cmp_doxygen') then
table.insert(Sources.c, { name = 'doxygen' })
table.insert(M.Sources.c, { name = 'doxygen' })
end

if exists('lazydev') then
table.insert(Sources.lua, {
table.insert(M.Sources.lua, {
name = 'lazydev',
group_index = 0,
})
end

---@type SetupSources
local ft = {
M.ft = {
{
{
'bash',
Expand All @@ -125,65 +129,65 @@ local ft = {
sources = gen_sources({
{ name = 'nvim_lsp', group_index = 1 },
{ name = 'nvim_lsp_signature_help', group_index = 2 },
async_path(3),
M.async_path(3),
{ name = 'vsnip', group_index = 4 },
buffer(5),
M.buffer(5),
}),
},
},
{
{ 'conf', 'config', 'cfg', 'confini', 'gitconfig' },
{
sources = gen_sources({
buffer(1),
async_path(2),
M.buffer(1),
M.async_path(2),
}),
},
},
{
{ 'c', 'cpp' },
{
sources = gen_sources(Sources.c),
sources = gen_sources(M.Sources.c),
},
},
['lua'] = {
sources = gen_sources(Sources.lua),
sources = gen_sources(M.Sources.lua),
},
['lisp'] = {
sources = gen_sources({
{ name = 'vlime', group_index = 1 },
buffer(2),
M.buffer(2),
}),
},
['gitcommit'] = {
sources = gen_sources({
{ name = 'conventionalcommits', group_index = 1 },
{ name = 'git', group_index = 2 },
async_path(3),
buffer(4),
M.async_path(3),
M.buffer(4),
}),
},
}

if exists('neorg') then
ft['norg'] = {
M.ft['norg'] = {
sources = gen_sources({
{ name = 'neorg', group_index = 1 },
buffer(2),
async_path(3),
M.buffer(2),
M.async_path(3),
}),
}
end

---@type SetupSources
local cmdline = {
M.cmdline = {
{
{ '/', '?' },
{
mapping = cmp.mapping.preset.cmdline(),
sources = gen_sources({
{ name = 'nvim_lsp_document_symbol', group_index = 1 },
buffer(2),
M.buffer(2),
}),
},
},
Expand All @@ -195,85 +199,69 @@ local cmdline = {
group_index = 1,
option = { treat_trailing_slash = false },
},
async_path(2),
M.async_path(2),
}),

---@diagnostic disable-next-line:missing-fields
matching = { disallow_symbol_nonprefix_matching = false },
},
}

---@type Sources
---@diagnostic disable-next-line:missing-fields
local M = {
setup = function(T)
local notify = require('user_api.util.notify').notify

if is_tbl(T) and not empty(T) then
for k, v in next, T do
if is_num(k) and is_tbl({ v[1], v[2] }, true) then
table.insert(ft, v)
elseif is_str(k) and is_tbl(v) then
ft[k] = v
else
notify(
"(plugin.cmp.sources.setup): Couldn't parse the input table value",
'error',
{
title = 'plugin.cmp.sources',
timeout = 500,
}
)
end
end
end
function M.setup(T)
local notify = require('user_api.util.notify').notify

for k, v in next, ft do
if is_tbl(T) and not empty(T) then
for k, v in next, T do
if is_num(k) and is_tbl({ v[1], v[2] }, true) then
local names = v[1]
local opts = v[2]

cmp.setup.filetype(names, opts)
table.insert(M.ft, v)
elseif is_str(k) and is_tbl(v) then
cmp.setup.filetype(k, v)
M.ft[k] = v
else
notify(
"(plugin.cmp.sources.setup): Couldn't parse the input table value",
'error',
{
title = 'plugin.cmp.sources',
timeout = 500,
}
)
notify("Couldn't parse the input table value", 'error', {
hide_from_history = false,
timeout = 800,
title = '(plugin.cmp.sources.setup)',
})
end
end
end

require('cmp_git').setup()

for k, v in next, cmdline do
if is_num(k) and is_tbl({ v[1], v[2] }, true) then
local names = v[1]
local opts = v[2]

cmp.setup.cmdline(names, opts)
elseif is_str(k) and is_tbl(v) then
cmp.setup.cmdline(k, v)
else
notify(
"(plugin.cmp.sources.setup): Couldn't parse the input table value",
'error',
{
title = 'plugin.cmp.sources',
timeout = 500,
}
)
end
for k, v in next, M.ft do
if is_num(k) and is_tbl({ v[1], v[2] }, true) then
local names = v[1]
local opts = v[2]

cmp.setup.filetype(names, opts)
elseif is_str(k) and is_tbl(v) then
cmp.setup.filetype(k, v)
else
notify("Couldn't parse the input table value", 'error', {
hide_from_history = false,
timeout = 800,
title = '(plugin.cmp.sources.setup)',
})
end
end,
end

buffer = buffer,
async_path = async_path,
}
require('cmp_git').setup()

for k, v in next, M.cmdline do
if is_num(k) and is_tbl({ v[1], v[2] }, true) then
local names = v[1]
local opts = v[2]

cmp.setup.cmdline(names, opts)
elseif is_str(k) and is_tbl(v) then
cmp.setup.cmdline(k, v)
else
notify("Couldn't parse the input table value", 'error', {
hide_from_history = false,
timeout = 800,
title = '(plugin.cmp.sources.setup)',
})
end
end
end

function M.new() return setmetatable({}, { __index = M }) end

Expand Down
7 changes: 5 additions & 2 deletions lua/user_api/types/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@
---@field new fun(): Sources
---@field __index? Sources
---@field setup fun(T: SetupSources?)
---@field buffer fun(priority: integer?): SourceBuf
---@field async_path? fun(priority: integer?): SourceAsyncPath
---@field buffer fun(group_index: integer?, all_bufs: boolean?): SourceBuf
---@field async_path? fun(group_index: integer?): SourceAsyncPath
---@field Sources table<string, (cmp.SourceConfig|SourceBuf|SourceAsyncPath)[]>
---@field ft SetupSources
---@field cmdline SetupSources

--- vim:ts=4:sts=4:sw=4:et:ai:si:sta:noci:nopi:

0 comments on commit ddae185

Please sign in to comment.