Files
darcula_nvim/nvim/lua/darcula/setup/400_nvim_cmp.lua
T

253 lines
6.3 KiB
Lua

-- The MIT License (MIT)
--
-- Copyright © 2026 Scott E. Graves <scott.e.graves@protonmail.com>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software
-- and associated documentation files (the "Software"), to deal in the Software without restriction,
-- including without limitation the rights to use, copy, modify, merge, publish, distribute,
-- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies or
-- substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
-- BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
if NV_DARCULA_ENABLE_COC then
return
end
local cmp = require("cmp")
local has_words_before = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return
col ~= 0 and
vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match(
"%s"
) == nil
end
local can_expand_snippet = function()
return
_G.MiniSnippets ~= nil and
#MiniSnippets.expand({insert = false}) > 0
end
local has_snippet_session = function()
return
_G.MiniSnippets ~= nil and MiniSnippets.session.get() ~= nil
end
local expand_snippet = function(args)
local insert =
MiniSnippets.config.expand.insert or MiniSnippets.default_insert
insert({body = args.body})
cmp.resubscribe({"TextChangedI", "TextChangedP"})
require("cmp.config").set_onetime({sources = {}})
end
local source_labels = {
buffer = "[Buffer]",
calc = "[Calc]",
mini_snippets = "[Snippet]",
nvim_lsp = "[LSP]",
path = "[Path]"
}
local kind_highlights = {
Constant = "CocSymbolConstant",
Constructor = "CocSymbolConstructor",
Function = "CocSymbolFunction",
Method = "CocSymbolMethod"
}
local format_item = function(entry, item)
local kind = item.kind
local icon, icon_hl = MiniIcons.get("lsp", kind)
local menu = item.menu or ""
local source = source_labels[entry.source.name] or ("[" .. entry.source.name .. "]")
item.kind = icon .. " " .. kind
item.kind_hl_group = kind_highlights[kind] or icon_hl
item.menu = menu == "" and source or (menu .. " " .. source)
return item
end
local bordered_window = function(opts)
local window_opts =
vim.tbl_extend(
"force",
{
border = "rounded",
winblend = 0,
winhighlight =
"Normal:Pmenu,FloatBorder:FloatBorder,CursorLine:PmenuSel,Search:PmenuMatch"
},
opts or {}
)
return
vim.tbl_extend(
"force",
cmp.config.window.bordered(window_opts),
window_opts
)
end
cmp.setup(
{
completion = {
completeopt = "menu,menuone,noselect",
keyword_length = 1
},
confirmation = {
get_commit_characters = function()
return {}
end
},
experimental = {
ghost_text =
NV_DARCULA_ENABLE_VIRTUAL_TEXT and
{hl_group = "DiagnosticVirtualTextInfo"} or
false
},
formatting = {
expandable_indicator = true,
fields = {"abbr", "menu", "kind"},
format = format_item
},
mapping = {
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-e>"] = cmp.mapping.abort(),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<CR>"] =
cmp.mapping(
function(fallback)
if not cmp.visible() then
fallback()
return
end
if cmp.get_active_entry() == nil then
cmp.abort()
return
end
cmp.confirm(
{
behavior = cmp.ConfirmBehavior.Replace,
select = false
}
)
end,
{"i", "s"}
),
["<S-Tab>"] =
cmp.mapping(
function(fallback)
if cmp.visible() then
cmp.select_prev_item({behavior = cmp.SelectBehavior.Insert})
return
end
if has_snippet_session() then
MiniSnippets.session.jump("prev")
return
end
fallback()
end,
{"i", "s"}
),
["<Tab>"] =
cmp.mapping(
function(fallback)
if cmp.visible() then
cmp.select_next_item({behavior = cmp.SelectBehavior.Insert})
return
end
if can_expand_snippet() then
vim.schedule(MiniSnippets.expand)
return
end
if has_snippet_session() then
MiniSnippets.session.jump("next")
return
end
if has_words_before() then
cmp.complete()
return
end
fallback()
end,
{"i", "s"}
)
},
matching = {
disallow_fuzzy_matching = false,
disallow_fullfuzzy_matching = false,
disallow_partial_fuzzy_matching = false,
disallow_partial_matching = false,
disallow_prefix_unmatching = false
},
performance = {
max_view_entries = 256
},
preselect = cmp.PreselectMode.None,
snippet = {
expand = expand_snippet
},
sources =
cmp.config.sources(
{
{name = "nvim_lsp", priority = 1000},
{name = "mini_snippets", priority = 900},
{name = "path", priority = 800},
{name = "calc", priority = 700}
},
{
{
name = "buffer",
priority = 500,
option = {
get_bufnrs = function()
return
vim.tbl_filter(
vim.api.nvim_buf_is_loaded,
vim.api.nvim_list_bufs()
)
end
}
}
}
),
view = {
docs = {
auto_open = true
},
entries = {
selection_order = "top_down"
}
},
window = {
completion = bordered_window(),
documentation =
bordered_window(
{
max_height = 25,
max_width = 120
}
)
}
}
)