nvim: Extraction completion setup into own module

As a first step to disentangle the ide.lua module into completion,
formatting, lsp (and maybe lint?), this separates the completion module
into its own file.
This commit is contained in:
Marty Oehme 2024-06-17 10:42:12 +02:00
parent 543a149da2
commit 157fbc812c
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
5 changed files with 181 additions and 178 deletions

View file

@ -28,7 +28,7 @@
"dressing.nvim": { "branch": "master", "commit": "572314728cb1ce012e825fd66331f52c94acac12" },
"fidget.nvim": { "branch": "main", "commit": "ef99df04a1c53a453602421bc0f756997edc8289" },
"flash.nvim": { "branch": "main", "commit": "7bb4a9c75d1e20cd24185afedeaa11681829ba23" },
"friendly-snippets": { "branch": "main", "commit": "700c4a25caacbb4648c9a27972c2fe203948e0c2" },
"friendly-snippets": { "branch": "main", "commit": "682157939e57bd6a2c86277dfd4d6fbfce63dbac" },
"fwatch.nvim": { "branch": "main", "commit": "a691f7349dc66285cd75a1a698dd28bca45f2bf8" },
"git-conflict.nvim": { "branch": "main", "commit": "bfd9fe6fba9a161fc199771d85996236a0d0faad" },
"gitsigns.nvim": { "branch": "main", "commit": "0b04035bb7b3c83e999b9676e2fb46fd0aa9f910" },

View file

@ -0,0 +1,179 @@
return {
-- completion setup
{
"hrsh7th/nvim-cmp",
branch = "main",
dependencies = {
"andersevenrud/cmp-tmux",
"cbarrete/completion-vcard",
"f3fora/cmp-spell",
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-path",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-calc",
"hrsh7th/cmp-cmdline",
"hrsh7th/cmp-nvim-lua",
"hrsh7th/cmp-nvim-lsp-signature-help",
"dmitmel/cmp-digraphs",
"jc-doyle/cmp-pandoc-references",
"kdheepak/cmp-latex-symbols",
"lukas-reineke/cmp-rg",
"crispgm/cmp-beancount",
"ray-x/cmp-treesitter",
"saadparwaiz1/cmp_luasnip",
{
"L3MON4D3/LuaSnip",
dependencies = { "rafamadriz/friendly-snippets" },
},
},
config = function()
local luasnip = require("luasnip")
local cmp = require("cmp")
local has_words_before = function()
---@diagnostic disable-next-line:deprecated
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 kind_icons = {
Text = "",
Method = "",
Function = "󰊕",
Constructor = "",
Field = "",
Variable = "",
Class = "",
Interface = "",
Module = "",
Property = "",
Unit = "",
Value = "V",
Enum = "",
Keyword = "",
Snippet = "",
Color = "",
File = "",
Reference = "",
Folder = "",
EnumMember = "",
Constant = "",
Struct = "",
Event = "",
Operator = "",
TypeParameter = "",
}
cmp.setup({
window = { documentation = cmp.config.window.bordered() },
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
sources = {
{ name = "nvim_lua" },
{
name = "beancount",
option = {
account = vim.env["HOME"] .. "/documents/records/budget/main.beancount", -- TODO implement dynamically
},
},
{ name = "otter" },
{ name = "nvim_lsp" },
{ name = "nvim_lsp_signature_help" },
{ name = "luasnip", keyword_length = 2 },
{ name = "pandoc_references" },
{ name = "calc" },
{ name = "path" },
{ name = "buffer", keyword_length = 3 },
{ name = "latex_symbols" },
{ name = "spell", keyword_length = 3 },
{ name = "tmux" }, -- { name = 'rg', keyword_length = 5 },
{ name = "vCard" },
{ name = "digraphs", keyword_length = 2 },
},
mapping = cmp.mapping.preset.insert({
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<CR>"] = cmp.mapping({
i = function(fallback)
if cmp.visible() and cmp.get_active_entry() then
cmp.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = false,
})
else
fallback()
end
end,
s = cmp.mapping.confirm({ select = true }),
c = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = false,
}), -- disable selection in cmd mode
}),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
-- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
-- they way you will only jump inside the snippet region
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
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" }),
}),
formatting = {
fields = { "kind", "abbr", "menu" },
format = function(entry, vim_item)
-- Kind icons, removing kind text leaving only icon
-- vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind)
vim_item.kind = string.format("%s", kind_icons[vim_item.kind])
-- Source
vim_item.menu = ({
buffer = "",
calc = "󰃬",
digraphs = "",
latex_symbols = "𝓧",
luasnip = "",
nvim_lsp = "",
nvim_lua = "󰢱",
pandoc_references = "",
spell = "󰓆",
vCard = "󰛋",
})[entry.source.name]
return vim_item
end,
},
})
-- `/` cmdline setup.
cmp.setup.cmdline("/", {
mapping = cmp.mapping.preset.cmdline(),
sources = { { name = "buffer" } },
})
-- `:` cmdline setup.
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({ { name = "path" } }, {
{ name = "cmdline", option = { ignore_cmds = { "Man", "!" } } },
}),
})
end,
event = { "InsertEnter", "CmdlineEnter" },
},
}

View file

@ -1,144 +0,0 @@
local luasnip = require("luasnip")
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 kind_icons = {
Text = "",
Method = "",
Function = "󰊕",
Constructor = "",
Field = "",
Variable = "",
Class = "",
Interface = "",
Module = "",
Property = "",
Unit = "",
Value = "V",
Enum = "",
Keyword = "",
Snippet = "",
Color = "",
File = "",
Reference = "",
Folder = "",
EnumMember = "",
Constant = "",
Struct = "",
Event = "",
Operator = "",
TypeParameter = "",
}
cmp.setup({
window = { documentation = cmp.config.window.bordered() },
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
sources = {
{ name = "nvim_lua" },
{
name = "beancount",
option = {
account = vim.env["HOME"] .. "/documents/records/budget/main.beancount", -- TODO implement dynamically
},
},
{ name = "otter" },
{ name = "nvim_lsp" },
{ name = "nvim_lsp_signature_help" },
{ name = "luasnip", keyword_length = 2 },
{ name = "pandoc_references" },
{ name = "calc" },
{ name = "path" },
{ name = "buffer", keyword_length = 3 },
{ name = "latex_symbols" },
{ name = "spell", keyword_length = 3 },
{ name = "tmux" }, -- { name = 'rg', keyword_length = 5 },
{ name = "vCard" },
{ name = "digraphs", keyword_length = 2 },
},
mapping = cmp.mapping.preset.insert({
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<CR>"] = cmp.mapping({
i = function(fallback)
if cmp.visible() and cmp.get_active_entry() then
cmp.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = false,
})
else
fallback()
end
end,
s = cmp.mapping.confirm({ select = true }),
c = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = false,
}), -- disable selection in cmd mode
}),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
-- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
-- they way you will only jump inside the snippet region
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
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" }),
}),
formatting = {
fields = { "kind", "abbr", "menu" },
format = function(entry, vim_item)
-- Kind icons, removing kind text leaving only icon
-- vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind)
vim_item.kind = string.format("%s", kind_icons[vim_item.kind])
-- Source
vim_item.menu = ({
buffer = "",
calc = "󰃬",
digraphs = "",
latex_symbols = "𝓧",
luasnip = "",
nvim_lsp = "",
nvim_lua = "󰢱",
pandoc_references = "",
spell = "󰓆",
vCard = "󰛋",
})[entry.source.name]
return vim_item
end,
},
})
-- `/` cmdline setup.
cmp.setup.cmdline("/", {
mapping = cmp.mapping.preset.cmdline(),
sources = { { name = "buffer" } },
})
-- `:` cmdline setup.
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({ { name = "path" } }, {
{ name = "cmdline", option = { ignore_cmds = { "Man", "!" } } },
}),
})

View file

@ -45,6 +45,7 @@ local servers = {
-- TODO installed for conform/nvim-lint so should be sourced from there not here
local to_mason =
---@diagnostic disable-next-line:deprecated
{ "stylua", "shellcheck", "shfmt", "markdownlint", "bibtex-tidy", "jq", "prettier", "ruff", unpack(servers) }
require("mason-tool-installer").setup({
-- a list of all tools you want to ensure are installed upon

View file

@ -195,39 +195,6 @@ return {
end,
},
-- completion setup
{
"hrsh7th/nvim-cmp",
branch = "main",
dependencies = {
"andersevenrud/cmp-tmux",
"cbarrete/completion-vcard",
"f3fora/cmp-spell",
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-path",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-calc",
"hrsh7th/cmp-cmdline",
"hrsh7th/cmp-nvim-lua",
"hrsh7th/cmp-nvim-lsp-signature-help",
"dmitmel/cmp-digraphs",
"jc-doyle/cmp-pandoc-references",
"kdheepak/cmp-latex-symbols",
"lukas-reineke/cmp-rg",
"crispgm/cmp-beancount",
"ray-x/cmp-treesitter",
"saadparwaiz1/cmp_luasnip",
{
"L3MON4D3/LuaSnip",
dependencies = { "rafamadriz/friendly-snippets" },
},
},
config = function()
require("plugins.config.cmp")
end,
event = { "InsertEnter", "CmdlineEnter" },
},
-- useful quickfix-like buffer
{
"folke/trouble.nvim",