aboutsummaryrefslogtreecommitdiffstats
path: root/nvim/lua/plugins/nvim-cmp.lua
blob: 7db099a9afc9d2022c8353fe7896a83772b6a3e3 (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
local 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 luasnip = {
    "L3MON4D3/LuaSnip",
    version = "v2.*",
    build = "make install_jsregexp",
    keys = {
        {
            '<c-j>',
            function()
                local ls = require('luasnip')
                if ls.expand_or_jumpable() then
                    ls.expand_or_jump()
                end
            end,
            mode = { 'i', 's' },
            desc = 'Previous snippet jump'
        },
        {
            '<c-k>',
            function()
                local ls = require('luasnip')
                if ls.jumpable(-1) then
                    ls.jump(-1)
                end
            end,
            mode = { 'i', 's' },
            desc = 'Next snippet jump'
        }
    },
    lazy = true
}

local cmp_path = {
    'hrsh7th/cmp-path',
    event = 'InsertEnter'
}

local cmp_luasnip = {
    'saadparwaiz1/cmp_luasnip',
    dependencies = { "L3MON4D3/LuaSnip" },
    event = 'InsertEnter'
}

local cmp_lua = {
    'hrsh7th/cmp-nvim-lua',
    ft = {
        "lua", "vim"
    }
}

local cmp_pandoc = {
    'aspeddro/cmp-pandoc.nvim',
    -- FIXME: weird issues when using bibliographies
    enabled = false,
    dependencies = {
        'nvim-lua/plenary.nvim',
    },
    -- ft = { 'pandoc', 'markdown', 'rmd' }
    opts = {
        bibliography = {
            documentation = true,
        },
        crossref = {
            documentation = true,
            enable_nabla = false
        }
    }
}

local cmp_lsp = {
    'hrsh7th/cmp-nvim-lsp',
    event = 'LspAttach',
    dependencies = {
        'neovim/nvim-lspconfig'
    },
}

local cmp_buffer = {
    'hrsh7th/cmp-buffer',
    event = 'BufEnter'
}

local cmp = {
    'hrsh7th/nvim-cmp',
    config = function()
        local cmp = require('cmp')
        cmp.setup({
            snippet = {
                expand = function(args)
                    require('luasnip').lsp_expand(args.body)
                end
            },
            sources = {
                { name = 'nvim_lsp' },
                { name = 'luasnip' },
                { name = 'path',    keyword_length = 2 },
                { name = 'buffer',  keyword_length = 4 },
            },
            window = {
                -- completion = cmp.config.window.bordered(),
                -- documentation = cmp.config.window.bordered()
            },
            formatting = {
                -- fields = {'menu', 'abbr', 'kind'},
                format = function(entry, item)
                    -- item.kind = string.format('%s %s', icons[item.kind], item.kind)
                    item.kind = icons[item.kind] .. ' ' .. item.kind
                    local menu = {
                        nvim_lsp = '[LSP]',
                        luasnip = '[Snip]',
                        buffer = '[Buffer]',
                        path = '[Path]',
                        nvim_lua = '[Lua]',
                        cmp_pandoc = '[Pandoc]',
                        emoiji = '[Emoji]',
                    }
                    menu['vim-dadbod-completion'] = '[dadbod]'
                    item.menu = menu[entry.source.name]
                    return item
                end
            },
            mapping = cmp.mapping.preset.insert({
                -- FIXME: change to better use new Snippet defaults
                ['<C-k>'] = cmp.mapping.scroll_docs(-5),
                ['<C-j>'] = cmp.mapping.scroll_docs(5),
                ['<TAB>'] = cmp.mapping(function(fallback)
                    if cmp.visible() then
                        cmp.confirm({ select = true })
                    else
                        fallback()
                    end
                end, { 'i', 's' })
            })
        })
        cmp.setup.filetype({ 'pandoc', 'markdown', 'rmd' }, {
            sources = {
                { name = 'luasnip' },
                { name = 'path',   keyword_length = 3 },
                { name = 'buffer' },
                { name = 'emoji' },
                -- { name = 'cmp_pandoc' },
            }
        })
        cmp.setup.filetype({ "lua", "vim" }, {
            sources = {
                { name = 'luasnip' },
                { name = 'nvim_lsp' },
                { name = 'nvim_lua' },
                { name = 'buffer' },
                { name = 'path' }
            }
        })
        cmp.setup.filetype({ "sql" }, {
            sources = {
                { name = 'vim-dadbod-completion' },
                { name = 'nvim_lsp' },
                { name = 'buffer' }
            }
        })
    end
}

return {
    luasnip,
    -- completion sources
    cmp_path,
    cmp_luasnip,
    cmp_lua,
    cmp_lsp,
    cmp_buffer,
    -- cmp_pandoc,
    cmp,
}