summary refs log tree commit diff
path: root/.vim/lua/internal/completion.lua
blob: 552a45e40fad2b48c0e4f4a5c6ce3a19a9f46865 (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
vim.opt.completeopt = { 'menu', 'menuone', 'noselect' }
vim.opt.shortmess:append('c')
vim.opt.pumheight = 20 -- max height of completion window

-- re-link the highlight group for the 3rd field in the completion popup
-- which is populated with the completion source
vim.schedule(function()
  local hl = vim.api.nvim_set_hl

  hl(0, 'CmpItemMenu', { link = 'Comment', force = true })

  hl(0, 'CmpItemAbbrMatch', { bold = true, force = true })
  hl(0, 'CmpItemAbbrMatchFuzzy', { link = 'CmpItemAbbrMatch', force = true })

  hl(0, 'CmpItemKindVariable', { link = 'Normal', force = true })
  hl(0, 'CmpItemKindInterface', { link = 'CmpItemKindVariable', force = true })
  hl(0, 'CmpItemKindText', { link = 'CmpItemKindVariable', force = true })

  hl(0, 'CmpItemKindFunction', { link = 'Function', force = true })
  hl(0, 'CmpItemKindMethod', { link = 'CmpItemKindFunction', force = true })

  hl(0, 'CmpItemKindKeyword', { link = 'Keyword', force = true })
  hl(0, 'CmpItemKindProperty', { link = 'CmpItemKindKeyword', force = true })
  hl(0, 'CmpItemKindUnit', { link = 'CmpItemKindKeyword', force = true })
end)

local cmp = require('cmp')

cmp.setup({
  sources = {
    { name = 'nvim_lsp', keyword_length = 1, group_index = 1 },
    { name = 'nvim_lsp_signature_help', keyword_length = 1, group_index = 1 },
    { name = 'luasnip', keyword_length = 2, group_index = 1 },
    -- { name = 'spell' },
    { name = 'async_path', group_index = 2 },
    { name = 'buffer', keyword_length = 3, group_index = 2 },
  },

  mapping = {
    ['<c-n>'] = cmp.mapping(
      cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
      { 'i', 'c' }
    ),
    ['<c-p>'] = cmp.mapping(
      cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
      { 'i', 'c' }
    ),
    ['<tab>'] = cmp.mapping(
      cmp.mapping.confirm({
        behavior = cmp.ConfirmBehavior.Insert,
        select = true,
      }),
      { 'i', 'c' }
    ),
  },

  -- enable snippet expansion
  snippet = {
    expand = function(args)
      vim.snippet.expand(args.body)
    end,
  },

  experimental = {
    ghost_text = false,
    native_menu = false,
  },

  window = {
    completion = {
      scrollbar = true,
      -- align 'abbr' with cursor, necessary due to 'kind' appearing first
      col_offset = -3,
    },
  },

  formatting = {
    fields = { 'kind', 'abbr', 'menu' },
    expandable_indicator = true,
    format = function(entry, item)
      item.dup = 0 -- dedup items from the same source, first one wins
      item.menu = '░ ' .. entry.source.name
      return require('lspkind').cmp_format({
        mode = 'symbol',
        symbol_map = {
          -- 󰆼  󱆃
          Array = '󰨾 ',
          Boolean = '󰦍 ',
          Class = '󰙀 ',
          Color = '󰉦 ',
          Constant = '󰐀 ',
          Constructor = '󱇿 ',
          Enum = '󱃢 ',
          EnumMember = '󱃡 ',
          Event = '󱐋 ',
          -- Field = '󰽜 ',
          Field = '󰆈 ',
          File = '󰈔 ',
          Folder = '󰝰 ',
          Function = '󰊕 ',
          Interface = '󱦜 ',
          Key = '󰌋 ',
          Keyword = '󰓽 ',
          Method = '󰒓 ',
          Module = '󰏖 ',
          Namespace = '󰆧 ',
          Null = '󰎣 ',
          Object = '󰘦 ',
          Operator = '󱓉 ',
          Property = '󰐱 ',
          Package = '󰏗 ',
          Reference = '󰌹 ',
          Snippet = '󰯁 ',
          Struct = '󰙅 ',
          Text = '󰉿 ',
          TypeParameter = '󰌨 ',
          Unit = '󰠱 ',
          Value = '󰎠 ',
          Variable = '󱄑 ',
        },
      })(entry, item)
    end,
  },
})

cmp.setup.cmdline({ ':', '/', '?', '@' }, {
  sources = {
    { name = 'cmdline', group_index = 1 },
    { name = 'cmdline_history', group_index = 1 },
    { name = 'nvim_lsp_document_symbol', keyword_length = 1, group_index = 1 },
    { name = 'async_path', group_index = 1 },
    { name = 'buffer', group_index = 1 },
  },

  matching = {
    disallow_symbol_nonprefix_matching = false,
  },

  view = {
    entries = { selection_order = 'near_cursor' },
  },

  formatting = {
    fields = { 'abbr', 'menu' },
  },
})

cmp.setup.cmdline({ '/', '?' }, {
  sources = {
    { name = 'nvim_lsp_document_symbol', keyword_length = 1, group_index = 1 },
    { name = 'path', max_item_count = 12, group_index = 1 },
    { name = 'buffer', group_index = 2 },
  },
})