Skip to content

Commit

Permalink
Improve modularity of cmp sources while maintaining priority
Browse files Browse the repository at this point in the history
Signed-off-by: Micah Halter <micah@balena.io>
  • Loading branch information
mehalter committed Apr 15, 2022
1 parent ae8fe46 commit 5f7d653
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
6 changes: 0 additions & 6 deletions lua/configs/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ function M.config()
completion = {
keyword_length = 1,
},
sources = {
{ name = "nvim_lsp" },
{ name = "luasnip" },
{ name = "buffer" },
{ name = "path" },
},
mapping = {
["<Up>"] = cmp.mapping.select_prev_item(),
["<Down>"] = cmp.mapping.select_next_item(),
Expand Down
9 changes: 9 additions & 0 deletions lua/core/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ local config = {
ts_autotag = true,
},

cmp = {
source_priority = {
nvim_lsp = 1000,
luasnip = 750,
buffer = 500,
path = 250,
},
},

ui = {
nui_input = true,
telescope_select = true,
Expand Down
12 changes: 12 additions & 0 deletions lua/core/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,36 @@ local astro_plugins = {
{
"saadparwaiz1/cmp_luasnip",
after = "nvim-cmp",
config = function()
require("core.utils").add_user_cmp_source "luasnip"
end,
},

-- Buffer completion source
{
"hrsh7th/cmp-buffer",
after = "nvim-cmp",
config = function()
require("core.utils").add_user_cmp_source "buffer"
end,
},

-- Path completion source
{
"hrsh7th/cmp-path",
after = "nvim-cmp",
config = function()
require("core.utils").add_user_cmp_source "path"
end,
},

-- LSP completion source
{
"hrsh7th/cmp-nvim-lsp",
after = "nvim-cmp",
config = function()
require("core.utils").add_user_cmp_source "nvim_lsp"
end,
},

-- LSP manager
Expand Down
19 changes: 19 additions & 0 deletions lua/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,25 @@ function M.toggle_term_cmd(cmd)
_user_terminals[cmd]:toggle()
end

function M.add_cmp_source(source, priority)
if type(priority) ~= "number" then
priority = 1000
end
local cmp_avail, cmp = pcall(require, "cmp")
if cmp_avail then
local config = cmp.get_config()
table.insert(config.sources, { name = source, priority = priority })
cmp.setup(config)
end
end

function M.add_user_cmp_source(source)
local priority = M.user_plugin_opts("cmp.source_priority", _user_settings.cmp.source_priority)[source]
if priority then
M.add_cmp_source(source, priority)
end
end

function M.label_plugins(plugins)
local labelled = {}
for _, plugin in ipairs(plugins) do
Expand Down
15 changes: 15 additions & 0 deletions lua/user_example/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ local config = {
},
},

-- CMP Source Priorities
-- modify here the priorities of default cmp sources
-- higher value == higher priority
-- The value can also be set to a boolean for disabling default sources:
-- false == disabled
-- true == 1000
cmp = {
source_priority = {
nvim_lsp = 1000,
luasnip = 750,
buffer = 500,
path = 250,
},
},

-- Extend LSP configuration
lsp = {
-- add to the server on_attach function
Expand Down

0 comments on commit 5f7d653

Please sign in to comment.