diff options
Diffstat (limited to '.config/nvim/lua/plugins/cmp.lua')
-rw-r--r-- | .config/nvim/lua/plugins/cmp.lua | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/.config/nvim/lua/plugins/cmp.lua b/.config/nvim/lua/plugins/cmp.lua new file mode 100644 index 0000000..78ff9a9 --- /dev/null +++ b/.config/nvim/lua/plugins/cmp.lua | |||
@@ -0,0 +1,114 @@ | |||
1 | -- autocompletion | ||
2 | return { | ||
3 | { | ||
4 | "hrsh7th/nvim-cmp", | ||
5 | event = "InsertEnter", | ||
6 | dependencies = { | ||
7 | "hrsh7th/cmp-nvim-lsp", | ||
8 | "hrsh7th/cmp-nvim-lua", | ||
9 | "hrsh7th/cmp-buffer", | ||
10 | "hrsh7th/cmp-path", | ||
11 | "L3MON4D3/LuaSnip", | ||
12 | "saadparwaiz1/cmp_luasnip", | ||
13 | "rafamadriz/friendly-snippets", | ||
14 | }, | ||
15 | config = function() | ||
16 | local cmp = require("cmp") | ||
17 | local luasnip = require("luasnip") | ||
18 | |||
19 | require("luasnip/loaders/from_vscode").lazy_load() | ||
20 | |||
21 | local kind_icons = { | ||
22 | Text = "", | ||
23 | Method = "m", | ||
24 | Function = "", | ||
25 | Constructor = "", | ||
26 | Field = "", | ||
27 | Variable = "", | ||
28 | Class = "", | ||
29 | Interface = "", | ||
30 | Module = "", | ||
31 | Property = "", | ||
32 | Unit = "", | ||
33 | Value = "", | ||
34 | Enum = "", | ||
35 | Keyword = "", | ||
36 | Snippet = "", | ||
37 | Color = "", | ||
38 | File = "", | ||
39 | Reference = "", | ||
40 | Folder = "", | ||
41 | EnumMember = "", | ||
42 | Constant = "", | ||
43 | Struct = "", | ||
44 | Event = "", | ||
45 | Operator = "", | ||
46 | TypeParameter = "", | ||
47 | } | ||
48 | |||
49 | cmp.setup({ | ||
50 | enabled = true, | ||
51 | snippet = { | ||
52 | expand = function(args) | ||
53 | luasnip.lsp_expand(args.body) | ||
54 | end, | ||
55 | }, | ||
56 | mapping = cmp.mapping.preset.insert({ | ||
57 | ["<C-k>"] = cmp.mapping.select_prev_item(), | ||
58 | ["<C-j>"] = cmp.mapping.select_next_item(), | ||
59 | ["<C-d>"] = cmp.mapping.scroll_docs(-4), | ||
60 | ["<C-f>"] = cmp.mapping.scroll_docs(4), | ||
61 | ["<CR>"] = cmp.mapping.confirm({ | ||
62 | behavior = cmp.ConfirmBehavior.Replace, | ||
63 | select = false, | ||
64 | }), | ||
65 | ["<Tab>"] = cmp.mapping(function(fallback) | ||
66 | if cmp.visible() then | ||
67 | cmp.select_next_item() | ||
68 | elseif luasnip.expand_or_jumpable() then | ||
69 | luasnip.expand_or_jump() | ||
70 | else | ||
71 | fallback() | ||
72 | end | ||
73 | end, { "i", "s" }), | ||
74 | ["<S-Tab>"] = cmp.mapping(function(fallback) | ||
75 | if cmp.visible() then | ||
76 | cmp.select_prev_item() | ||
77 | elseif luasnip.jumpable(-1) then | ||
78 | luasnip.jump(-1) | ||
79 | else | ||
80 | fallback() | ||
81 | end | ||
82 | end, { "i", "s" }), | ||
83 | }), | ||
84 | formatting = { | ||
85 | fields = { "kind", "abbr", "menu" }, | ||
86 | format = function(entry, vim_item) | ||
87 | -- kind icons | ||
88 | vim_item.kind = string.format("%s", kind_icons[vim_item.kind]) | ||
89 | vim_item.menu = ({ | ||
90 | nvim_lsp = "[lsp]", | ||
91 | luasnip = "[snippet]", | ||
92 | buffer = "[buffer]", | ||
93 | path = "[path]", | ||
94 | })[entry.source.name] | ||
95 | return vim_item | ||
96 | end, | ||
97 | }, | ||
98 | sources = { | ||
99 | { name = "nvim_lsp" }, | ||
100 | { name = "luasnip" }, | ||
101 | { name = "buffer" }, | ||
102 | { name = "path" }, | ||
103 | }, | ||
104 | }) | ||
105 | |||
106 | -- If you want insert `(` after select function or method item | ||
107 | local cmp_autopairs = require('nvim-autopairs.completion.cmp') | ||
108 | cmp.event:on( | ||
109 | 'confirm_done', | ||
110 | cmp_autopairs.on_confirm_done() | ||
111 | ) | ||
112 | end, | ||
113 | }, | ||
114 | } | ||