aboutsummaryrefslogtreecommitdiffstats
path: root/nvim/lua/plugins/nvim-cmp.lua
blob: ac588f6d6d12600d685cb4beabd603f7c955bb96 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
-- modified from https://github.com/VonHeikemen/dotfiles

local Plugins = {}

-- FIXME: plugin not loading correctly

-- Autocompletion
local cmp_plugin = {
    'hrsh7th/nvim-cmp',
    event = 'InsertEnter',
    dependencies = {
        -- Sources
        'hrsh7th/cmp-buffer',
        'hrsh7th/cmp-path',
        'saadparwaiz1/cmp_luasnip',
        'hrsh7th/cmp-nvim-lsp',
        'hrsh7th/cmp-nvim-lua',

        -- Snippets
        {'L3MON4D3/LuaSnip'},
    }
}

local user = {autocomplete = true}

table.insert(Plugins, cmp_plugin)

table.insert(Plugins, {
      'aspeddro/cmp-pandoc.nvim',
      enabled = true,
      -- event = 'InsertEnter',
      ft = {'markdown', 'pandoc', 'rmd'},
      dependencies = {
          'nvim-lua/plenary.nvim',
          'jbyuki/nabla.nvim'
      },
      opts = {
          crossref = {
              enable_nabla = true
          }
      }
})

cmp_plugin.config = function()
  user.augroup = vim.api.nvim_create_augroup('compe_cmds', {clear = true})
  vim.api.nvim_create_user_command('UserCmpEnable', user.enable_cmd, {})

  local cmp = require('cmp')
  local luasnip = require('luasnip')

  local select_opts = {behavior = cmp.SelectBehavior.Select}
  local cmp_enable = cmp.get_config().enabled

  user.config = {
    enabled = function()
      if user.autocomplete then
        return cmp_enable()
      end

      return false
    end,
    completion = {
      completeopt = 'menu,menuone',
    },
    snippet = {
      expand = function(args)
        luasnip.lsp_expand(args.body)
      end,
    },
    sources = {
      { name = 'path'},
      { name = 'nvim_lsp'},
      { name = 'buffer', keyword_length = 4},
      { name = 'luasnip'},
      { name = 'cmp_pandoc' },
      { name = 'emoji' },
    },
    window = {
      documentation = {
        border = 'rounded',
        max_height = 15,
        max_width = 50,
        zindex = 50,
      }
    },
    formatting = {
      fields = {'menu', 'abbr', 'kind'},
      format = function(entry, item)
        local menu = {
          nvim_lsp = '[LSP]',
          luasnip = '[Snip]',
          buffer = '[Buffer]',
          path = '[Path]',
          nvim_lua = '[Lua]',
          cmp_pandoc = '[Pandoc]',
          emoiji = '[Emoji]'
        }

        item.menu = menu[entry.source.name]
        return item
      end,
    },
    mapping = {
      ['<C-k>'] = cmp.mapping.scroll_docs(-5),
      ['<C-j>'] = cmp.mapping.scroll_docs(5),

      ['<Up>'] = cmp.mapping.select_prev_item(select_opts),
      ['<Down>'] = cmp.mapping.select_next_item(select_opts),

      -- TODO: test
      -- ['<M-k>'] = cmp.mapping.select_prev_item(select_opts),
      -- ['<M-j>'] = cmp.mapping.select_next_item(select_opts),

      ['<C-a>'] = cmp.mapping(function(fallback)
        if luasnip.jumpable(-1) then
          luasnip.jump(-1)
        else
          fallback()
        end
      end, {'i', 's'}),

      ['<C-d>'] = cmp.mapping(function(fallback)
        if luasnip.jumpable(1) then
          luasnip.jump(1)
        else
          fallback()
        end
      end, {'i', 's'}),

      ['<M-u>'] = cmp.mapping(function()
        if cmp.visible() then
          user.set_autocomplete(false)
          cmp.abort()
        else
          user.set_autocomplete(true)
          cmp.complete()
        end
      end),

      ['<Tab>'] = cmp.mapping(function(fallback)
        if cmp.visible() then
          cmp.confirm({select = true})
        elseif luasnip.jumpable(1) then
          luasnip.jump(1)
        elseif user.check_back_space() then
          fallback()
        else
          user.set_autocomplete(true)
          cmp.complete()
        end
      end, {'i', 's'}),

      ['<S-Tab>'] = cmp.mapping(function()
        if luasnip.jumpable(-1) then
          luasnip.jump(-1)
        else
          user.insert_tab()
        end
      end, {'i', 's'}),
    }
  }

  cmp.setup(user.config)
end

function user.set_autocomplete(new_value)
  local old_value = user.autocomplete

  if new_value == old_value then
    return
  end

  if new_value == false then
    -- restore autocomplete in the next word
    vim.api.nvim_buf_set_keymap(
      0,
      'i',
      '<space>',
      '<cmd>UserCmpEnable<cr><space>',
      {noremap = true}
    )

    -- restore when leaving insert mode
    vim.api.nvim_create_autocmd('InsertLeave', {
      group = user.augroup,
      command = 'UserCmpEnable',
      once = true,
    })
  end

  user.autocomplete = new_value
end


function user.check_back_space()
  local col = vim.fn.col('.') - 1
  if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
    return true
  else
    return false
  end
end

function user.enable_cmd()
  if user.autocomplete then
    return
  end

  pcall(vim.api.nvim_buf_del_keymap, 0, 'i', '<Space>')
  user.set_autocomplete(true)
end

function user.insert_tab()
  vim.api.nvim_feedkeys(
    vim.api.nvim_replace_termcodes('<Tab>', true, true, true),
    'n',
    true
  )
end

-- return Plugins

return {
    {
        "L3MON4D3/LuaSnip",
        version = "v2.*",
        build = "make install_jsregexp"
    },
    -- completion sources
    {
        'hrsh7th/cmp-path',
    },
    {
        'saadparwaiz1/cmp_luasnip'
    },
    { -- TODO: lazy load on filtetype
        'hrsh7th/cmp-nvim-lua'
    },
    {
        -- TODO: lazy load on lsp load
        'hrsh7th/cmp-nvim-lsp',
        dependencies= {
            'neovim/nvim-lspconfig'
        },
    },
    {
        'hrsh7th/cmp-buffer',
    },
    {
        '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' },
                    { name = 'nvim_lua' },
                    { name = 'buffer', keyword_length = 4 },
                    { name = 'emoji' }
                },
                window = {
                    -- TODO: style cmp windows
                    completion = cmp.config.window.bordered(),
                    documentation = cmp.config.window.bordered()
                },
                mapping = cmp.mapping.preset.insert({
                    ['<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'})
                })
            })
        end
    },

    -- {
    --     'kdheepak/cmp-latex-symbols',
    --     dependencies = { 'hrsh7th/nvim-cmp' }
    -- },
}