Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmp): enable the user to easily add new completion sources #3975

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 51 additions & 47 deletions lua/lvim/core/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,62 +156,66 @@ M.config = function()
fields = { "kind", "abbr", "menu" },
max_width = 0,
kind_icons = lvim.icons.kind,
source_names = {
nvim_lsp = "(LSP)",
emoji = "(Emoji)",
path = "(Path)",
calc = "(Calc)",
cmp_tabnine = "(Tabnine)",
vsnip = "(Snippet)",
luasnip = "(Snippet)",
buffer = "(Buffer)",
tmux = "(TMUX)",
copilot = "(Copilot)",
treesitter = "(TreeSitter)",
},
duplicates = {
buffer = 1,
path = 1,
nvim_lsp = 0,
luasnip = 1,
source_settings = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the naming here, and I'm still wondering if we're better off merging these into the original sources list, maybe under cmp.sources[foo].options

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylo252 I actually really like because there are already a few source settings sprinkled into the sources list, see copilot for an example:

sources = {
      {
        name = "copilot",
        -- keyword_length = 0,
        max_item_count = 3,
        trigger_characters =
-- etc.

So consolidating the rest of the settings in there make sense. However, as you can see, the sources object is a list so we can't index in as you suggested cmp.sources[foo].options. We could change it to that format i.e.

sources = {
  copilot = { 
    -- options for copilot
  }
  other_source = {
   -- other options
  }

This would give us a single place to add sources and configure them in a way that I thinks makes sense, of course we would need to update everything that access the sources list to handle the new format.

copilot = {
menu_name = "(Copilot)",
icon = lvim.icons.git.Octoface,
hl_group = "CmpItemKindCopilot",
},
emoji = {
menu_name = "(Emoji)",
icon = lvim.icons.misc.Smiley,
hl_group = "CmpItemKindEmoji",
},
cmp_tabnine = {
menu_name = "(Tabnine)",
icon = lvim.icons.misc.Robot,
hl_group = "CmpItemKindTabnine",
},
crates = {
menu_name = "(Crates)",
icon = lvim.icons.misc.Package,
hl_group = "CmpItemKindCrate",
},
ww-daniel-mora marked this conversation as resolved.
Show resolved Hide resolved
["lab.quick_data"] = {
menu_name = "(QuickData)",
icon = lvim.icons.misc.CircuitBoard,
hl_group = "CmpItemKindConstant",
},
nvim_lsp = {
menu_name = "(LSP)",
duplicates = 0,
},
path = {
menu_name = "(Path)",
duplicates = 1,
},
luasnip = {
menu_name = "(Snippet)",
duplicates = 1,
},
buffer = {
menu_name = "(Buffer)",
duplicates = 1,
},
calc = { menu_name = "(Calc)" },
vsnip = { menu_name = "(Snippet)" },
treesitter = { menu_name = "(TreeSitter)" },
tmux = { menu_name = "(TMUX)" },
},
duplicates_default = 0,
format = function(entry, vim_item)
local max_width = lvim.builtin.cmp.formatting.max_width
local source_settings = lvim.builtin.cmp.formatting.source_settings[entry.source.name]
ww-daniel-mora marked this conversation as resolved.
Show resolved Hide resolved
if max_width ~= 0 and #vim_item.abbr > max_width then
vim_item.abbr = string.sub(vim_item.abbr, 1, max_width - 1) .. lvim.icons.ui.Ellipsis
end
if lvim.use_icons then
vim_item.kind = lvim.builtin.cmp.formatting.kind_icons[vim_item.kind]

if entry.source.name == "copilot" then
vim_item.kind = lvim.icons.git.Octoface
vim_item.kind_hl_group = "CmpItemKindCopilot"
end

if entry.source.name == "cmp_tabnine" then
vim_item.kind = lvim.icons.misc.Robot
vim_item.kind_hl_group = "CmpItemKindTabnine"
end

if entry.source.name == "crates" then
vim_item.kind = lvim.icons.misc.Package
vim_item.kind_hl_group = "CmpItemKindCrate"
end

if entry.source.name == "lab.quick_data" then
vim_item.kind = lvim.icons.misc.CircuitBoard
vim_item.kind_hl_group = "CmpItemKindConstant"
end

if entry.source.name == "emoji" then
vim_item.kind = lvim.icons.misc.Smiley
vim_item.kind_hl_group = "CmpItemKindEmoji"
end
vim_item.kind = source_settings.icon or lvim.builtin.cmp.formatting.kind_icons[vim_item.kind]
ww-daniel-mora marked this conversation as resolved.
Show resolved Hide resolved
vim_item.kind_hl_group = source_settings.hl_group
end
vim_item.menu = lvim.builtin.cmp.formatting.source_names[entry.source.name]
vim_item.dup = lvim.builtin.cmp.formatting.duplicates[entry.source.name]
or lvim.builtin.cmp.formatting.duplicates_default
vim_item.menu = source_settings.menu_name
ww-daniel-mora marked this conversation as resolved.
Show resolved Hide resolved
vim_item.dup = source_settings.duplicates or lvim.builtin.cmp.formatting.duplicates_default
return vim_item
end,
},
Expand Down