Skip to content

Commit

Permalink
feat(nvim): switch to blink-cmp
Browse files Browse the repository at this point in the history
Remove all nvim-cmp related plugins and switch to blink-cmp.
  • Loading branch information
aymanbagabas committed Jan 15, 2025
1 parent e99ee6b commit 8d75afa
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 179 deletions.
10 changes: 2 additions & 8 deletions modules/neovim/config/lua/user/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,7 @@ end
---Extends the object with capabilities provided by plugins.
---@return lsp.ClientCapabilities
function M.make_client_capabilities()
local has_cmp, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
local capabilities = vim.tbl_deep_extend(
"force",
{},
vim.lsp.protocol.make_client_capabilities(),
has_cmp and cmp_nvim_lsp.default_capabilities() or {}
)
local capabilities = vim.lsp.protocol.make_client_capabilities()

-- enable lsp file watch support
capabilities = vim.tbl_extend("force", capabilities, {
Expand All @@ -124,7 +118,7 @@ function M.make_client_capabilities()
},
},
})
return capabilities
return require("blink.cmp").get_lsp_capabilities(capabilities)
end

--- Set keymap
Expand Down
211 changes: 69 additions & 142 deletions modules/neovim/config/plugin/completion.lua
Original file line number Diff line number Diff line change
@@ -1,151 +1,78 @@
-- Link CmpGhostText to Comment
vim.api.nvim_set_hl(0, "CmpGhostText", { link = "Comment", default = true })

local cmp = require("cmp")
local defaults = require("cmp.config.default")()
local suggestion = require("copilot.suggestion")

local opts = {
enabled = function()
-- disable completion in comments
local context = require("cmp.config.context")
-- keep command mode completion enabled when cursor is in a comment
if vim.api.nvim_get_mode().mode == "c" then
return true
else
return not context.in_treesitter_capture("comment") and not context.in_syntax_group("Comment")
end
end,
performance = {
debounce = 120,
require("blink.cmp").setup({
keymap = {
preset = "enter",
["<C-y>"] = { "select_and_accept" },
["<C-e>"] = {
function(cmp)
suggestion.dismiss() -- Dismiss copilot suggestions
return cmp.cancel()
end,
"fallback",
},
["<Tab>"] = {
"snippet_forward",
function(cmp)
if suggestion.is_visible() then
return cmp.hide()
end
end,
"fallback",
},
},
auto_brackets = {}, -- configure any filetype to auto add brackets
completion = {
completeopt = "menu,menuone,noinsert,noselect",
keyword_length = 3,
appearance = {
use_nvim_cmp_as_default = true,
nerd_font_variant = "mono",
kind_icons = require("icons").kinds,
},
preselect = cmp.PreselectMode.Item,
snippet = {
expand = function(args)
vim.snippet.expand(args.body)
end,
signature = { enabled = true },
sources = {
default = { "lsp", "path", "snippets", "buffer", "emoji" },
cmdline = {},
providers = {
lsp = {
min_keyword_length = function(ctx)
-- Always show this provider when trigger is manual
-- i.e. <C-space> is pressed.
return ctx.trigger.kind == "manual" and 0 or 1
end,
score_offset = 0,
},
path = {
min_keyword_length = 0,
},
snippets = {
min_keyword_length = 2,
},
buffer = {
min_keyword_length = 5,
max_items = 5,
},
emoji = {
module = "blink-emoji",
name = "Emoji",
score_offset = 15, -- Tune by preference
opts = { insert = true }, -- Insert emoji (default) or complete its name
},
},
},
mapping = cmp.mapping.preset.insert({
["<Tab>"] = cmp.mapping(function(fallback)
if vim.snippet.active({ direction = 1 }) then
vim.schedule(function()
vim.snippet.jump(1)
end)
elseif suggestion.is_visible() and cmp.visible() then
cmp.close()
elseif suggestion.is_visible() then
-- Ignore TextChanged events to prevent triggering
-- completion again after accepting a suggestion
local origin = vim.o.eventignore
vim.o.eventignore = "TextChangedI,TextChangedP"
suggestion.accept()
vim.defer_fn(function()
vim.o.eventignore = origin
end, 10)
-- vim.schedule(function()
-- -- We need to schedule this to close the completion menu after accepting the suggestion
-- cmp.abort()
-- end)
else
fallback()
end
end, { "i", "s" }),

["<S-Tab>"] = cmp.mapping(function(fallback)
if vim.snippet.active({ direction = -1 }) then
vim.schedule(function()
vim.snippet.jump(-1)
end)
else
fallback()
end
end, { "i", "s" }),

-- Show next Copilot suggestion
["<C-k>"] = cmp.mapping(function()
suggestion.next()
end, { "i" }),

-- Show prev Copilot suggestion
["<C-j>"] = cmp.mapping(function()
suggestion.prev()
end, { "i" }),

-- Accept next word
["<C-l>"] = cmp.mapping(function(fallback)
if suggestion.is_visible() then
suggestion.accept_word()
else
fallback()
end
end, { "i" }),
completion = {
accept = { auto_brackets = { enabled = true } },

-- Accept line
["<C-S-l>"] = cmp.mapping(function(fallback)
if suggestion.is_visible() then
suggestion.accept_line()
else
fallback()
end
end, { "i" }),
documentation = {
auto_show = true,
auto_show_delay_ms = 250,
treesitter_highlighting = true,
},

["<C-y>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}, { "i", "c" }),
["<C-n>"] = cmp.mapping.select_next_item(),
["<C-p>"] = cmp.mapping.select_prev_item(),
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping(function()
-- Dismiss Copilot
suggestion.dismiss()
cmp.abort()
end, { "i" }),
["<CR>"] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Insert }),
["<S-CR>"] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace }),
}),
sources = cmp.config.sources({
{ name = "nvim_lsp_signature_help", keyword_length = 1 },
{ name = "nvim_lsp", keyword_length = 1 },
-- { name = "copilot", keyword_length = 3 },
{
name = "snippets",
keyword_length = 2,
priority = 99,
menu = {
draw = {
columns = {
{ "kind_icon", "label", gap = 1 },
{ "kind" },
},
},
},
{ name = "path" },
}, {
{ name = "buffer", keyword_length = 3 },
}, {
{ name = "emoji", priority = 999 },
}),
formatting = {
format = function(_, item)
local icons = require("icons").kinds
if icons[item.kind] then
item.kind = icons[item.kind] .. item.kind
end
return item
end,
},
experimental = {
ghost_text = false, -- { hl_group = "CmpGhostText" },
},
sorting = defaults.sorting,
}

for _, source in ipairs(opts.sources) do
source.group_index = source.group_index or 1
end

-- clangd extensions
table.insert(opts.sorting.comparators, 1, require("clangd_extensions.cmp_scores"))

cmp.setup(opts)
})
44 changes: 37 additions & 7 deletions modules/neovim/config/plugin/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ local opts = {
hide_during_completion = false,
debounce = 75,
keymap = {
-- Use nvim-cmp
accept = false, -- "<M-l>",
accept_word = false,
accept_line = false,
next = false, -- "<M-]>",
prev = false, -- "<M-[>",
dismiss = false, -- "<C-]>",
-- We integrate with blink.cmp to hide the menu
-- when suggestions are visible.
accept = "<Tab>",
accept_word = "<C-l>",
accept_line = "<C-S-l>",
next = "<C-k>",
prev = "<C-j>",
dismiss = "<C-e>",
},
},
filetypes = {
Expand All @@ -42,3 +43,32 @@ local opts = {
}

require("copilot").setup(opts)

-- Disable default keymaps
-- vim.g.copilot_no_maps = true
--
-- vim.keymap.set("i", "<Tab>", 'copilot#Accept("")', {
-- desc = "Accept Copilot suggestion",
-- expr = true,
-- replace_keycodes = false,
-- })
--
-- vim.keymap.set("i", "<C-l>", "<Plug>(copilot-accept-word)", {
-- desc = "Accept Copilot word",
-- })
--
-- vim.keymap.set("i", "<C-S-l>", "<Plug>(copilot-accept-word)", {
-- desc = "Accept Copilot line",
-- })
--
-- vim.keymap.set("i", "<C-k>", "<Plug>(copilot-next)", {
-- desc = "Show next Copilot suggestion",
-- })
--
-- vim.keymap.set("i", "<C-j>", "<Plug>(copilot-previous)", {
-- desc = "Show prev Copilot suggestion",
-- })
--
-- vim.keymap.set("i", "<C-e>", "<Plug>(copilot-dismiss)", {
-- desc = "Dismiss Copilot",
-- })
40 changes: 29 additions & 11 deletions modules/neovim/config/plugin/noice.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local opts = {
override = {
["vim.lsp.util.convert_input_to_markdown_lines"] = true,
["vim.lsp.util.stylize_markdown"] = true,
["cmp.entry.get_documentation"] = true,
},
},
routes = {
Expand All @@ -27,14 +26,33 @@ local opts = {
},
}

require('noice').setup(opts)

vim.keymap.set('c', "<S-Enter>", function() require("noice").redirect(vim.fn.getcmdline()) end, { desc = "Redirect Cmdline" })
vim.keymap.set('n', "<leader>snl", function() require("noice").cmd("last") end, { desc = "Noice Last Message" })
vim.keymap.set('n', "<leader>snh", function() require("noice").cmd("history") end, { desc = "Noice History" })
vim.keymap.set('n', "<leader>sna", function() require("noice").cmd("all") end, { desc = "Noice All" })
vim.keymap.set('n', "<leader>snd", function() require("noice").cmd("dismiss") end, { desc = "Dismiss All" })
vim.keymap.set('n', "<leader>snt", function() require("noice").cmd("telescope") end, { desc = "Noice Telescope" })
vim.keymap.set({"i", "n", "s"}, "<c-f>", function() if not require("noice.lsp").scroll(4) then return "<c-f>" end end, { silent = true, expr = true, desc = "Scroll Forward" })
vim.keymap.set({"i", "n", "s"}, "<c-b>", function() if not require("noice.lsp").scroll(-4) then return "<c-b>" end end, { silent = true, expr = true, desc = "Scroll Backward"})
require("noice").setup(opts)

vim.keymap.set("c", "<S-Enter>", function()
require("noice").redirect(vim.fn.getcmdline())
end, { desc = "Redirect Cmdline" })
vim.keymap.set("n", "<leader>snl", function()
require("noice").cmd("last")
end, { desc = "Noice Last Message" })
vim.keymap.set("n", "<leader>snh", function()
require("noice").cmd("history")
end, { desc = "Noice History" })
vim.keymap.set("n", "<leader>sna", function()
require("noice").cmd("all")
end, { desc = "Noice All" })
vim.keymap.set("n", "<leader>snd", function()
require("noice").cmd("dismiss")
end, { desc = "Dismiss All" })
vim.keymap.set("n", "<leader>snt", function()
require("noice").cmd("telescope")
end, { desc = "Noice Telescope" })
vim.keymap.set({ "i", "n", "s" }, "<c-f>", function()
if not require("noice.lsp").scroll(4) then
return "<c-f>"
end
end, { silent = true, expr = true, desc = "Scroll Forward" })
vim.keymap.set({ "i", "n", "s" }, "<c-b>", function()
if not require("noice.lsp").scroll(-4) then
return "<c-b>"
end
end, { silent = true, expr = true, desc = "Scroll Backward" })
2 changes: 1 addition & 1 deletion modules/neovim/config/plugin/snippets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- this needs to be set before cmp.setup()

require("snippets").setup({
create_cmp_source = true,
create_cmp_source = false,
friendly_snippets = true,
global_snippets = { "all", "global" },
})
12 changes: 2 additions & 10 deletions modules/neovim/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@
SchemaStore-nvim
avante-nvim
barbecue-nvim
blink-cmp
blink-emoji-nvim
bufferline-nvim
clangd_extensions-nvim
cmp-buffer # current buffer as completion source | https://github.com/hrsh7th/cmp-buffer/
cmp-cmdline # cmp command line suggestions
cmp-cmdline-history # cmp command line history suggestions
cmp-emoji
cmp-nvim-lsp # LSP as completion source | https://github.com/hrsh7th/cmp-nvim-lsp/
cmp-nvim-lua # neovim lua API as completion source | https://github.com/hrsh7th/cmp-nvim-lua/
cmp-path # file paths as completion source | https://github.com/hrsh7th/cmp-path/
cmp_luasnip # snippets autocompletion extension for nvim-cmp | https://github.com/saadparwaiz1/cmp_luasnip/
conflict-marker-vim
# copilot-cmp
conform-nvim
copilot-lua
dashboard-nvim
Expand All @@ -44,7 +37,6 @@
neodev-nvim
noice-nvim
nui-nvim
nvim-cmp # https://github.com/hrsh7th/nvim-cmp
nvim-colorizer-lua
nvim-lspconfig
nvim-navic # Add LSP location to lualine | https://github.com/SmiteshP/nvim-navic
Expand Down

0 comments on commit 8d75afa

Please sign in to comment.