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

Setup autocompletion with nvim-cmp #22

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Using [lualine](https://github.com/nvim-lualine/lualine.nvim), I built a status

### 📋 List of Plugins

- [cmp](https://github.com/hrsh7th/nvim-cmp)
- [conform](https://github.com/stevearc/conform.nvim)
- [gitsigns](https://github.com/lewis6991/gitsigns.nvim)
- [lualine](https://github.com/nvim-lualine/lualine.nvim)
Expand Down
220 changes: 220 additions & 0 deletions lua/config-plugins/cmp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
vim.api.nvim_create_autocmd("FileType", {
callback = function()
local filetypes = { "gitcommit", "markdown" }
for _, f in ipairs(filetypes) do
if vim.bo.filetype == f then
require("cmp").setup.filetype(f, {
enabled = false,
})
end
end
end,
})

vim.api.nvim_create_autocmd("CmdlineEnter", {
callback = function()
require("cmp").setup { enabled = false }
end,
})

vim.api.nvim_create_autocmd("CmdlineLeave", {
callback = function()
require("cmp").setup { enabled = true }
end,
})

return {
"hrsh7th/nvim-cmp",
event = { "BufEnter", "BufReadPre", "BufNewFile" },
dependencies = {
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-nvim-lua",
"hrsh7th/cmp-cmdline",
"hrsh7th/cmp-path",
"f3fora/cmp-spell",
"hrsh7th/cmp-nvim-lsp-signature-help",
"saadparwaiz1/cmp_luasnip",
"lukas-reineke/cmp-under-comparator",
},
config = function()
local kind_icons = {
Text = "󰉿",
Method = "󰆧",
Function = "󰊕",
Constructor = "",
Field = "󰜢",
Variable = "󰀫",
Class = "󰠱",
Interface = "",
Module = "",
Property = "󰜢",
Unit = "󰑭",
Value = "󰎠",
Enum = "",
Keyword = "󰌋",
Snippet = "",
Color = "󰏘",
File = "󰈙",
Reference = "󰈇",
Folder = "󰉋",
EnumMember = "",
Constant = "󰏿",
Struct = "󰙅",
Event = "",
Operator = "󰆕",
TypeParameter = "",
}

local cmp = require("cmp")
local cmplsp = require("cmp_nvim_lsp")
local compare = require("cmp.config.compare")
local luasnip = require("luasnip")
local types = require("cmp.types")

cmplsp.setup()

local modified_priority = {
[types.lsp.CompletionItemKind.Variable] = types.lsp.CompletionItemKind.Method,
[types.lsp.CompletionItemKind.Snippet] = 0, -- top
[types.lsp.CompletionItemKind.Keyword] = 0, -- top
[types.lsp.CompletionItemKind.Text] = 100, -- bottom
}

local function modified_kind(kind)
return modified_priority[kind] or kind
end

local buffers = {
name = "buffer",
option = {
keyword_length = 3,
get_bufnrs = function() -- from all buffers (less than 1MB)
local bufs = {}
for _, bufn in ipairs(vim.api.nvim_list_bufs()) do
local buf_size = vim.api.nvim_buf_get_offset(bufn, vim.api.nvim_buf_line_count(bufn))
if buf_size < 1024 * 1024 then
table.insert(bufs, bufn)
end
end
return bufs
end,
},
}

local spelling = {
name = "spell",
max_item_count = 5,
keyword_length = 3,
option = {
keep_all_entries = false,
enable_in_context = function()
return true
end,
},
}

cmp.setup {
preselect = false,
completion = {
completeopt = "menu,menuone,preview,noselect",
},
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
formatting = {
fields = { "abbr", "kind", "menu" },
format = function(entry, vim_item)
local kind = vim_item.kind
vim_item.kind = " " .. (kind_icons[kind] or "?") .. ""
local source = entry.source.name
vim_item.menu = "[" .. source .. "]"

return vim_item
end,
},
sorting = {
priority_weight = 1.0,
comparators = {
compare.offset,
compare.exact,
function(entry1, entry2) -- sort by length ignoring "=~"
local len1 = string.len(string.gsub(entry1.completion_item.label, "[=~()_]", ""))
local len2 = string.len(string.gsub(entry2.completion_item.label, "[=~()_]", ""))
if len1 ~= len2 then
return len1 - len2 < 0
end
end,
compare.recently_used,
function(entry1, entry2) -- sort by compare kind (Variable, Function etc)
local kind1 = modified_kind(entry1:get_kind())
local kind2 = modified_kind(entry2:get_kind())
if kind1 ~= kind2 then
return kind1 - kind2 < 0
end
end,
compare.score,
require("cmp-under-comparator").under,
compare.kind,
},
},
matching = {
disallow_fuzzy_matching = true,
disallow_fullfuzzy_matching = true,
disallow_partial_fuzzy_matching = true,
disallow_partial_matching = false,
disallow_prefix_unmatching = true,
},
min_length = 0, -- allow for `from package import _` in Python
mapping = cmp.mapping.preset.insert {
["<CR>"] = cmp.mapping.confirm { select = false }, -- no not select first item
["<C-e>"] = cmp.mapping.abort(),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
},
sources = cmp.config.sources({
{ name = "nvim_lsp", max_item_count = 5 },
{ name = "luasnip", max_item_count = 3 },
{ name = "luasnip", max_item_count = 3 },
{ name = "nvim_lua", max_item_count = 5 },
{ name = "nvim_lsp_signature_help", max_item_count = 5 },
}, {
buffers,
spelling,
}),
performance = {
max_view_entries = 20,
},
window = { documentation = cmp.config.window.bordered(), completion = cmp.config.window.bordered() },
}

-- `:` cmdline setup.
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
matching = { disallow_symbol_nonprefix_matching = false },
})
end,
}
File renamed without changes.
2 changes: 1 addition & 1 deletion lua/config-plugins/conform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ return {
formatters_by_ft = {
lua = { "stylua" },
-- Conform will run multiple formatters sequentially
python = { "ruff", "isort" },
sh = { "shellcheck" },
python = { "isort", "ruff_format" },
terraform = { "terraform_fmt" },
},
format_on_save = {
Expand Down
File renamed without changes.
41 changes: 41 additions & 0 deletions lua/config-plugins/luasnip.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
return {
"L3MON4D3/LuaSnip", -- snippet completions
event = { "BufReadPre", "BufNewFile" },
dependencies = { "rafamadriz/friendly-snippets" },
build = "make install_jsregexp",
config = function()
local ls = require("luasnip")

local lsloader = require("luasnip.loaders.from_lua")

require("luasnip.loaders.from_vscode").lazy_load()
lsloader.load { paths = "~/.config/nvim/snippets" }

local types = require("luasnip.util.types")

ls.config.set_config {
-- Keep last snippet to jump around
history = true,

-- Enable dynamic snippets
updateevents = "TextChanged,TextChangedI",
-- For cleaning up snippets whose text was deleted
delete_check_events = "TextChanged",

enable_autosnippets = true,

ext_opts = {
[types.choiceNode] = { active = { virt_text = { { "●", "Operator" } } } },
},
}

-- Extend changelog with debchangelog
ls.filetype_extend("changelog", { "debchangelog" })

vim.keymap.set("i", "<c-l>", function()
if ls.choice_active() then
ls.change_choice(1)
end
end)
end,
}
Loading