This repository has been archived on 2022-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/home/.config/nvim/lua/modules/completion.lua

186 lines
5.9 KiB
Lua

local M = {}
function M.cmp_conf()
local cmp = require('cmp')
local luasnip = require('luasnip')
local check_back_space = function()
local col = vim.fn.col '.' - 1
return col == 0 or vim.fn.getline('.'):sub(col, col):match '%s' ~= nil
end
local t = function(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
cmp.setup {
formatting = {
format = function(entry, vim_item)
local lspkind_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 = ''
}
-- load lspkind icons
vim_item.kind = string.format('%s %s', lspkind_icons[vim_item.kind], vim_item.kind)
vim_item.menu = ({
luasnip = '[SNIP]',
path = '[PATH]',
buffer = '[BUF]',
-- calc = '[CALC]',
-- nuspell = '[SPELL]',
spell = '[SPELL]',
emoji = '[EMOJI]',
nvim_lsp = '[LSP]',
latex_symbols = '[TEX]',
tmux = '[TMUX]',
orgmode = '[ORG]'
})[entry.source.name]
return vim_item
end
},
mapping = {
['<C-Space>'] = cmp.mapping.complete(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-e>'] = cmp.mapping.close(),
-- Change choice nodes for luasnip
['<C-h>'] = cmp.mapping(function(fallback)
if luasnip.choice_active() then
vim.fn.feedkeys(t('<Plug>luasnip-prev-choice'), '')
else
fallback()
end
end, {'i', 's'}),
['<C-l>'] = cmp.mapping(function(fallback)
if luasnip.choice_active() then
vim.fn.feedkeys(t('<Plug>luasnip-next-choice'), '')
else
fallback()
end
end, {'i', 's'}),
-- supertab-like mapping
['<Tab>'] = cmp.mapping(function(fallback)
if vim.fn.pumvisible() == 1 then
vim.fn.feedkeys(t('<C-n>'), 'n')
elseif luasnip.expand_or_jumpable() then
vim.fn.feedkeys(t('<Plug>luasnip-expand-or-jump'), '')
elseif check_back_space() then
vim.fn.feedkeys(t('<Tab>'), 'n')
else
fallback()
end
end, {'i', 's'}),
['<S-Tab>'] = cmp.mapping(function(fallback)
if vim.fn.pumvisible() == 1 then
vim.fn.feedkeys(t('<C-p>'), 'n')
elseif luasnip.jumpable(-1) then
vim.fn.feedkeys(t('<Plug>luasnip-jump-prev'), '')
else
fallback()
end
end, {'i', 's'})
},
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end
},
sources = {
{name = 'luasnip'},
{name = 'path'},
{name = 'buffer'},
-- {name = 'calc'},
-- {name = 'nuspell'},
{name = 'spell'},
{name = 'emoji'},
{name = 'nvim_lsp'},
{name = 'latex_symbols'},
{name = 'tmux'},
{name = 'orgmode'}
}
}
end
function M.autopairs_conf()
require('nvim-autopairs').setup {fast_wrap = {}}
require('nvim-autopairs.completion.cmp').setup({
map_cr = true, -- map <CR> on insert mode
map_complete = true, -- it will auto insert `(` after select function or method item
auto_select = true -- automatically select the first item
})
end
function M.snippets_conf()
local types = require('luasnip.util.types')
require('luasnip').config.set_config({
updateevents = 'TextChanged, TextChangedI',
history = true,
ext_opts = {
[types.choiceNode] = {
active = {
virt_text = {{'', 'Orange'}}
}
},
[types.insertNode] = {
active = {
virt_text = {{'', 'Blue'}}
}
}
}
})
-- Loading vscode-like snippets from 'friendly-snippets'
require('luasnip/loaders/from_vscode').load()
end
function M.coq_conf()
-- To add snippets from lsp servers, change lsp.lua:
-----
-- local coq = require('coq')
-- lspconf.<server>.setup(...) --> lspconf.<server>.setup(coq.lsp_ensure_capabilities(...))
vim.g.coq_settings = {
auto_start = true,
display = {
icons = {
mode = 'none'
}
}
}
end
function M.autotag_conf()
require('nvim-treesitter.configs').setup {
autotag = {enable = true}
}
end
return M