summaryrefslogtreecommitdiffstats
path: root/.config/nvim/lua/user/lsp/common.lua
blob: acc3a647f596e576fc24475a3cb2bebdf676f6eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
-- this file contains setup for snippets and common LSP options

local cmp_status, cmp = pcall(require, 'cmp')
-- vim.api.nvim_echo({{"Hello!", "Normal"}}, true, {})
if not cmp_status then
	vim.api.nvim_err_writeln(string.format("shit! error status: %s", cmp_status))
	return
end
luasnip = require 'luasnip'
luasnip.setup()

local expnoresilent = {expr = true, noremap = true, silent = true}
local noresilent = {noremap = true, silent = true}
local map = cmp.mapping

-- stolen from https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance
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 = "󰅲",
}

-- from https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip
local stolen_function = function() -- to check whether there are words before cursor
	unpack = unpack or table.unpack
	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

cmp.setup {
	snippet = {
		expand = function(args)
			-- vim.fn["vsnip#anonymous"](args.body)	-- for vsnip (i left it)
			luasnip.lsp_expand(args.body)		-- for luasnip cause i like it more
		end,
	},
	mapping = {
		["<C-n>"] = map.select_next_item(),
		["<C-p>"] = map(map.select_prev_item({ behavior = cmp.SelectBehavior.Insert })),
		["<C-Space>"] = map.complete(),
	},
	-- NOTE: ordering or this table affects completions ordering
	sources = { 	-- TODO: Add keyword_length to some of them ??????
		{ name = 'nvim_lsp' },
		{ name = 'luasnip' },
		{ name = 'buffer' },
		{ name = 'path' },
		{ name = 'calc' },
		{ name = 'nvim_lua' },
		{ name = 'nvim_lsp_signature_help' },
	},
	formatting = {
		fields = { "kind", "abbr", "menu" },
		-- the function is also half-stolen
		format = function(entry, vim_item)
			vim_item.kind = string.format("%s · |%s|", kind_icons[vim_item.kind], vim_item.kind)
			vim_item.menu = ({
				buffer = "[Buffer]",
				nvim_lsp = "[Lsp]",
				vsnip = "[Vsnip]",
				luasnip = "[Luasnip]",
				path = "[Path]",
				nvim_lua = "[Lua]",
				calc = "[󰊕(x)dx]",
				nvim_lsp_signature_help = "[Lsp-help]",
			})[entry.source.name]
			return vim_item
		end,
	},
	window = {
		completion = cmp.config.window.bordered(), 
		documentation = cmp.config.window.bordered(),
	},
	experimental = { ghost_text = true },
}